DABC (Data Acquisition Backbone Core)  2.9.9
Module.h
Go to the documentation of this file.
1 // $Id: Module.h 4471 2020-04-15 13:19:26Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Module
17 #define DABC_Module
18 
19 #ifndef DABC_Worker
20 #include "dabc/Worker.h"
21 #endif
22 
23 #ifndef DABC_Port
24 #include "dabc/Port.h"
25 #endif
26 
27 
28 namespace dabc {
29 
30  class Manager;
31  class ModuleSync;
32  class ModuleAsync;
33  class ModuleRef;
34 
42  class Module : public Worker {
43 
44  friend class Manager;
45  friend class ModuleItem;
46  friend class Timer;
47  friend class ConnTimer;
48  friend class ModuleSync;
49  friend class ModuleAsync;
50  friend class ModuleRef;
51 
52  protected:
53 
54  bool fRunState;
55  std::vector<ModuleItem*> fItems;
56  std::vector<InputPort*> fInputs;
57  std::vector<OutputPort*> fOutputs;
58  std::vector<PoolHandle*> fPools;
59  std::vector<Timer*> fTimers;
60  std::vector<ModuleItem*> fUsers;
61  unsigned fSysTimerIndex;
62  bool fAutoStop;
64  std::string fInfoParName;
65  std::string fPublishPars;
66 
67  private:
68 
69  Module(const std::string &name, Command cmd);
70  virtual ~Module();
71 
72  void AddModuleItem(ModuleItem* item);
73  void RemoveModuleItem(ModuleItem* item);
74 
76 
77  inline PoolHandle* Pool(unsigned n = 0) const { return n < fPools.size() ? fPools[n] : 0; }
78  inline OutputPort* Output(unsigned n = 0) const { return n < fOutputs.size() ? fOutputs[n] : 0; }
79  inline InputPort* Input(unsigned n = 0) const { return n < fInputs.size() ? fInputs[n] : 0; }
80 
81 
82  protected:
83 
87  virtual void ObjectCleanup();
88 
89  virtual bool DoStop();
90  virtual bool DoStart();
91 
92  virtual void OnThreadAssigned();
93  virtual int PreviewCommand(Command cmd);
94 
95  virtual void SetModulePriority(int pri = -1);
96 
97  virtual void ProcessEvent(const EventId&);
98 
99  virtual double ProcessTimeout(double last_diff);
100 
101  virtual bool Find(ConfigIO &cfg);
102 
103  virtual void BuildFieldsMap(RecordFieldsMap* cont);
104 
106  bool Start();
107 
109  inline bool IsRunning() const { return fRunState; }
110 
112  bool Stop();
113 
115  void SetAutoStop(bool on = true) { fAutoStop = on; }
116 
117 
122  unsigned CreatePoolHandle(const std::string &poolname, unsigned queue = 10);
123 
124  unsigned CreateInput(const std::string &name, unsigned queue = 10);
125  unsigned CreateOutput(const std::string &name, unsigned queue = 10);
126 
127  unsigned CreateTimer(const std::string &name, double period_sec = -1., bool synchron = false);
128 
129  unsigned CreateUserItem(const std::string &name);
130 
131 
133  void EnsurePorts(unsigned numinp = 0, unsigned numout = 0, const std::string &poolname = "");
134 
137  bool BindPorts(const std::string &inpname, const std::string &outname);
138 
139 
140  // these are new methods, which should be protected
141  unsigned NumOutputs() const { return fOutputs.size(); }
142  bool IsValidOutput(unsigned indx = 0) const { return indx < fOutputs.size(); }
143  bool IsOutputConnected(unsigned indx = 0) const { return indx < fOutputs.size() ? fOutputs[indx]->IsConnected() : false; }
144  unsigned OutputQueueCapacity(unsigned indx = 0) const { return indx < fOutputs.size() ? fOutputs[indx]->QueueCapacity() : 0; }
145  unsigned FindOutput(const std::string &name) const;
146  std::string OutputName(unsigned indx = 0, bool fullname = false) const;
147 
148  unsigned NumInputs() const { return fInputs.size(); }
149  bool IsValidInput(unsigned indx = 0) const { return indx < fInputs.size(); }
150  bool IsInputConnected(unsigned indx = 0) const { return indx < fInputs.size() ? fInputs[indx]->IsConnected() : false; }
151  bool InputQueueFull(unsigned indx = 0) const { return indx < fInputs.size() ? fInputs[indx]->QueueFull() : false; }
152  unsigned InputQueueCapacity(unsigned indx = 0) const { return indx < fInputs.size() ? fInputs[indx]->QueueCapacity() : 0; }
153  unsigned FindInput(const std::string &name) const;
154  std::string InputName(unsigned indx = 0, bool fullname = false) const;
155 
156  unsigned NumPools() const { return fPools.size(); }
157  bool IsValidPool(unsigned indx = 0) const { return indx < fPools.size(); }
158  bool IsPoolConnected(unsigned indx = 0) const { return indx < fPools.size() ? fPools[indx]->IsConnected() : false; }
159  unsigned FindPool(const std::string &name) const;
160  std::string PoolName(unsigned indx = 0, bool fullname = false) const;
162  bool IsAutoPool(unsigned indx = 0) const { return indx<fPools.size() ? fPools[indx]->QueueCapacity() > 0 : false; }
163 
164 
165  PortRef FindPort(const std::string &name) const;
166  bool IsPortConnected(const std::string &name) const;
167 
169  bool DisconnectPort(const std::string &name, bool witherr = false);
170 
172  void DisconnectAllPorts(bool witherr = false);
173 
175  bool SubmitCommandToTransport(const std::string &portname, Command cmd);
176 
177  unsigned PortQueueCapacity(const std::string &name) const { return FindPort(name).QueueCapacity(); }
178  bool SetPortSignaling(const std::string &name, Port::EventsProducing signal);
179  bool SetPortRatemeter(const std::string &name, const Parameter& ref);
180  bool SetPortLoopLength(const std::string &name, unsigned cnt);
181 
182  unsigned FindTimer(const std::string &name);
183  bool IsValidTimer(unsigned indx) const { return indx < fTimers.size(); }
184  unsigned NumTimers() const { return fTimers.size(); }
185  std::string TimerName(unsigned n = 0, bool fullname = false) const;
186 
187  void ShootTimer(unsigned indx, double delay_sec = 0.)
188  {
189  if (indx < fTimers.size()) {
190  if (indx == fSysTimerIndex) ActivateTimeout(delay_sec);
191  else fTimers[indx]->SingleShoot(delay_sec);
192  }
193  }
194 
195  void ShootTimer(const std::string &name, double delay_sec = 0.)
196  { ShootTimer(FindTimer(name), delay_sec); }
197 
198  ModuleItem* GetItem(unsigned id) const { return id<fItems.size() ? fItems[id] : 0; }
199 
200  unsigned FindUserItem(const std::string &name);
201  bool IsValidUserItem(unsigned indx) const { return indx < fUsers.size(); }
202  std::string UserItemName(unsigned indx = 0, bool fullname = false) const;
203  EventId ConstructUserItemEvent(unsigned indx = 0)
204  { return EventId(evntUser, 0, indx < fUsers.size() ? fUsers[indx]->ItemId() : 0); }
205 
206 
207  virtual Parameter CreatePar(const std::string &name, const std::string &kind = "");
208 
209  void SetInfoParName(const std::string &name);
210 
211 
212  // TODO: move to respective module classes
213  bool CanSendToAllOutputs(bool exclude_disconnected = true) const;
214  void SendToAllOutputs(Buffer& buf);
215 
216 
217  void ProduceInputEvent(unsigned indx = 0, unsigned cnt = 1);
218  void ProduceOutputEvent(unsigned indx = 0, unsigned cnt = 1);
219  void ProducePoolEvent(unsigned indx = 0, unsigned cnt = 1);
220  void ProduceUserItemEvent(unsigned indx = 0, unsigned cnt = 1);
221 
222 
223  // =================== can be reimplemented in derived classes ===============
224 
225 
226  // user must redefine method when it want to execute commands.
227  // If method return true or false (cmd_true or cmd_false),
228  // command considered as executed and will be replied
229  // Any other arguments (cmd_postponed) means that execution
230  // will be postponed in the module itself
231  // User must call Module::ExecuteCommand() to enable processing of standard commands
232  virtual int ExecuteCommand(Command cmd) { return dabc::Worker::ExecuteCommand(cmd); }
233 
234 
235  // This is place of user code when command completion is arrived
236  // User can implement any analysis of command data in this method
237  // If returns true, command object will be delete automatically,
238  // otherwise ownership delegated to user.
239  // In that case command must be at some point replied - means user should call cmd.Reply(res) method.
240  virtual bool ReplyCommand(Command cmd) { return dabc::Worker::ReplyCommand(cmd); }
241 
242 
243  // these two methods called before start and after stop of module
244  // disregard of module has its own mainloop or not
245  virtual void BeforeModuleStart() {}
246  virtual void AfterModuleStop() {}
249  virtual void ModuleCleanup() {}
250 
251  // generic users event processing function
252  virtual void ProcessItemEvent(ModuleItem* item, uint16_t evid) {}
253 
254  virtual void ProcessConnectionActivated(const std::string &name, bool on) {}
255 
256  public:
257 
258  virtual const char* ClassName() const { return "Module"; }
259 
260  std::string GetInfoParName() const;
261 
262  };
263 
264  // ___________________________________________________________________
265 
266 
275  class ModuleRef : public WorkerRef {
276 
278 
279  friend class Manager;
280 
281  protected:
282 
283  public:
284 
285  bool IsRunning() const { return GetObject() ? GetObject()->IsRunning() : false; }
286 
287  bool Start() { return Execute("StartModule"); }
288 
289  bool Stop() { return Execute("StopModule"); }
290 
291  bool CheckConnected() { return Execute("CheckConnected"); }
292 
294  bool IsInputConnected(unsigned ninp);
295 
297  bool IsOutputConnected(unsigned ninp);
298 
300  unsigned NumInputs();
301 
303  unsigned NumOutputs();
304 
306  PortRef FindPort(const std::string &name);
307 
309  bool IsPortConnected(const std::string &name);
310 
312  std::string InputName(unsigned n = 0, bool itemname = true);
313 
315  std::string OutputName(unsigned n = 0, bool itemname = true);
316 
319  bool ConnectPoolHandles();
320 
323  std::string InfoParName() const
324  { return null() ? std::string() : GetObject()->GetInfoParName(); }
325 
326  };
327 
328 };
329 
330 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
Interface class between xml configuration and dabc objects.
Definition: ConfigIO.h:38
Special timer to reestablish port connections in the module.
Definition: ModuleItem.h:197
Input port.
Definition: Port.h:244
Manager of everything in DABC
Definition: Manager.h:291
Base class for user-derived code, implementing event-processing.
Definition: ModuleAsync.h:32
Base class for module items like ports, timers, pool handles.
Definition: ModuleItem.h:68
Reference on dabc::Module class
Definition: Module.h:275
bool Stop()
Definition: Module.h:289
bool IsRunning() const
Definition: Module.h:285
std::string OutputName(unsigned n=0, bool itemname=true)
Return item name of the output, can be used in connect command.
Definition: Module.cxx:1066
unsigned NumOutputs()
Returns number of outputs in the module.
Definition: Module.cxx:1049
bool IsInputConnected(unsigned ninp)
Returns true if specified input is connected.
Definition: Module.cxx:979
bool Start()
Definition: Module.h:287
bool IsPortConnected(const std::string &name)
Returns true if port with specified name is connected - thread safe.
Definition: Module.cxx:1036
unsigned NumInputs()
Returns number of inputs in the module.
Definition: Module.cxx:1043
PortRef FindPort(const std::string &name)
Return reference on the port.
Definition: Module.cxx:1031
bool IsOutputConnected(unsigned ninp)
Returns true if specified output is connected.
Definition: Module.cxx:989
bool ConnectPoolHandles()
Method called by manager to establish connection to pools TODO: while used from devices,...
Definition: Module.cxx:999
bool CheckConnected()
Definition: Module.h:291
std::string InputName(unsigned n=0, bool itemname=true)
Return item name of the input, can be used in connect command.
Definition: Module.cxx:1056
std::string InfoParName() const
Returns info parameter name, used to provide different kind of log/debug information.
Definition: Module.h:323
Base class for user-derived code, implementing main loop.
Definition: ModuleSync.h:60
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
bool SubmitCommandToTransport(const std::string &portname, Command cmd)
Submits command to transport, assigned with the port.
Definition: Module.cxx:301
bool SetPortSignaling(const std::string &name, Port::EventsProducing signal)
Definition: Module.cxx:791
unsigned FindOutput(const std::string &name) const
Definition: Module.cxx:157
std::vector< ModuleItem * > fItems
map for fast search of module items
Definition: Module.h:55
virtual void ObjectCleanup()
Inherited method, called during module destroy.
Definition: Module.cxx:536
unsigned OutputQueueCapacity(unsigned indx=0) const
Definition: Module.h:144
virtual bool Find(ConfigIO &cfg)
Method to locate object in xml file.
Definition: Module.cxx:516
virtual void AfterModuleStop()
Definition: Module.h:246
unsigned NumPools() const
Definition: Module.h:156
unsigned FindPool(const std::string &name) const
Definition: Module.cxx:173
void RemoveModuleItem(ModuleItem *item)
Definition: Module.cxx:651
void ProduceOutputEvent(unsigned indx=0, unsigned cnt=1)
Definition: Module.cxx:779
ModuleItem * GetItem(unsigned id) const
Definition: Module.h:198
std::vector< Timer * > fTimers
array of timers
Definition: Module.h:59
virtual Parameter CreatePar(const std::string &name, const std::string &kind="")
Definition: Module.cxx:129
bool IsRunning() const
Returns true if module if running.
Definition: Module.h:109
bool IsPortConnected(const std::string &name) const
Definition: Module.cxx:785
bool IsValidInput(unsigned indx=0) const
Definition: Module.h:149
virtual void BuildFieldsMap(RecordFieldsMap *cont)
Fill fields map, which is relevant for the object Objects hierarchy produced from dabc::Manager.
Definition: Module.cxx:530
virtual bool DoStop()
Definition: Module.cxx:586
PoolHandle * Pool(unsigned n=0) const
Definition: Module.h:77
virtual bool DoStart()
Definition: Module.cxx:563
bool IsOutputConnected(unsigned indx=0) const
Definition: Module.h:143
void SetAutoStop(bool on=true)
If set, module will be automatically stopped when all i/o ports are disconnected.
Definition: Module.h:115
OutputPort * Output(unsigned n=0) const
Definition: Module.h:78
bool IsValidOutput(unsigned indx=0) const
Definition: Module.h:142
Buffer TakeDfltBuffer()
Definition: Module.cxx:258
std::string OutputName(unsigned indx=0, bool fullname=false) const
Definition: Module.cxx:181
bool InputQueueFull(unsigned indx=0) const
Definition: Module.h:151
virtual void OnThreadAssigned()
Definition: Module.cxx:79
bool CanSendToAllOutputs(bool exclude_disconnected=true) const
Definition: Module.cxx:932
std::string PoolName(unsigned indx=0, bool fullname=false) const
Definition: Module.cxx:195
virtual void ProcessEvent(const EventId &)
Definition: Module.cxx:822
bool IsValidPool(unsigned indx=0) const
Definition: Module.h:157
bool fRunState
true if module in the running state
Definition: Module.h:54
bool BindPorts(const std::string &inpname, const std::string &outname)
Bind input and output ports that both will share same connection.
Definition: Module.cxx:750
std::string InputName(unsigned indx=0, bool fullname=false) const
Definition: Module.cxx:188
bool IsInputConnected(unsigned indx=0) const
Definition: Module.h:150
void ProduceUserItemEvent(unsigned indx=0, unsigned cnt=1)
Definition: Module.cxx:234
virtual void ProcessConnectionActivated(const std::string &name, bool on)
Definition: Module.h:254
dabc::Reference fDfltPool
direct reference on memory pool, used when no pool handles are not created
Definition: Module.h:63
bool IsPoolConnected(unsigned indx=0) const
Definition: Module.h:158
unsigned FindInput(const std::string &name) const
Definition: Module.cxx:165
unsigned FindUserItem(const std::string &name)
Definition: Module.cxx:219
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Module.h:258
unsigned NumTimers() const
Definition: Module.h:184
unsigned CreatePoolHandle(const std::string &poolname, unsigned queue=10)
Creates handle for memory pool, which preserves reference on memory pool and provides fast access to ...
Definition: Module.cxx:607
virtual void SetModulePriority(int pri=-1)
Definition: Module.cxx:284
void SetInfoParName(const std::string &name)
Definition: Module.cxx:143
unsigned NumOutputs() const
Definition: Module.h:141
unsigned CreateUserItem(const std::string &name)
Definition: Module.cxx:203
bool SetPortLoopLength(const std::string &name, unsigned cnt)
Definition: Module.cxx:812
void AddModuleItem(ModuleItem *item)
Definition: Module.cxx:629
EventId ConstructUserItemEvent(unsigned indx=0)
Definition: Module.h:203
std::string GetInfoParName() const
Definition: Module.cxx:150
void ShootTimer(const std::string &name, double delay_sec=0.)
Definition: Module.h:195
bool fAutoStop
module will automatically stop when all i/o ports will be disconnected
Definition: Module.h:62
virtual bool ReplyCommand(Command cmd)
Reimplement this method to react on command reply Return true if command can be destroyed by framewor...
Definition: Module.h:240
unsigned NumInputs() const
Definition: Module.h:148
unsigned fSysTimerIndex
index of timer, which will be used with module itself
Definition: Module.h:61
virtual int PreviewCommand(Command cmd)
This method called before command will be executed.
Definition: Module.cxx:311
virtual double ProcessTimeout(double last_diff)
Definition: Module.cxx:554
void ShootTimer(unsigned indx, double delay_sec=0.)
Definition: Module.h:187
std::string UserItemName(unsigned indx=0, bool fullname=false) const
Definition: Module.cxx:227
unsigned CreateInput(const std::string &name, unsigned queue=10)
Definition: Module.cxx:704
void DisconnectAllPorts(bool witherr=false)
Method disconnects all module ports, should be called only from Module thread.
Definition: Module.cxx:292
bool IsAutoPool(unsigned indx=0) const
Returns true when handle automatically delivers buffers via the connection.
Definition: Module.h:162
void ProduceInputEvent(unsigned indx=0, unsigned cnt=1)
Definition: Module.cxx:764
unsigned CreateOutput(const std::string &name, unsigned queue=10)
Definition: Module.cxx:727
std::vector< OutputPort * > fOutputs
array of output ports
Definition: Module.h:57
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Module.h:232
virtual void ModuleCleanup()
Method, which can be reimplemented by user and should cleanup all references on buffers and other obj...
Definition: Module.h:249
std::vector< ModuleItem * > fUsers
array of user items
Definition: Module.h:60
std::string fPublishPars
path where module pars will be published
Definition: Module.h:65
void ProducePoolEvent(unsigned indx=0, unsigned cnt=1)
Definition: Module.cxx:772
unsigned CreateTimer(const std::string &name, double period_sec=-1., bool synchron=false)
Definition: Module.cxx:109
std::string fInfoParName
full name of parameter, used as info
Definition: Module.h:64
PortRef FindPort(const std::string &name) const
Definition: Module.cxx:699
virtual void ProcessItemEvent(ModuleItem *item, uint16_t evid)
Definition: Module.h:252
bool Stop()
Stops execution of the module code.
Definition: Module.cxx:249
std::string TimerName(unsigned n=0, bool fullname=false) const
Definition: Module.cxx:93
bool DisconnectPort(const std::string &name, bool witherr=false)
Disconnect port from transport.
Definition: Module.cxx:269
bool Start()
Starts execution of the module code.
Definition: Module.cxx:240
void EnsurePorts(unsigned numinp=0, unsigned numout=0, const std::string &poolname="")
Method ensure that at least specified number of input and output ports will be created.
Definition: Module.cxx:66
bool SetPortRatemeter(const std::string &name, const Parameter &ref)
Definition: Module.cxx:802
unsigned InputQueueCapacity(unsigned indx=0) const
Definition: Module.h:152
std::vector< InputPort * > fInputs
array of input ports
Definition: Module.h:56
virtual void BeforeModuleStart()
Definition: Module.h:245
virtual ~Module()
Definition: Module.cxx:59
InputPort * Input(unsigned n=0) const
Definition: Module.h:79
std::vector< PoolHandle * > fPools
array of pools
Definition: Module.h:58
void SendToAllOutputs(Buffer &buf)
Definition: Module.cxx:942
unsigned PortQueueCapacity(const std::string &name) const
Definition: Module.h:177
unsigned FindTimer(const std::string &name)
Definition: Module.cxx:100
bool IsValidUserItem(unsigned indx) const
Definition: Module.h:201
Module(const std::string &name, Command cmd)
Definition: Module.cxx:24
bool IsValidTimer(unsigned indx) const
Definition: Module.h:183
Output port.
Definition: Port.h:301
Parameter class
Definition: Parameter.h:163
Handle for pool connection.
Definition: Port.h:347
Reference on the dabc::Port class
Definition: Port.h:195
unsigned QueueCapacity() const
Returns queue capacity of the port.
Definition: Port.h:205
EventsProducing
Definition: Port.h:58
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Provides timer event to the module.
Definition: ModuleItem.h:149
Reference on dabc::Worker
Definition: Worker.h:466
bool Execute(Command cmd, double tmout=-1.)
Definition: Worker.cxx:1147
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
bool ActivateTimeout(double tmout_sec)
Method used to produce timeout events in the worker.
Definition: Worker.cxx:385
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Worker.cxx:851
virtual bool ReplyCommand(Command cmd)
Reimplement this method to react on command reply Return true if command can be destroyed by framewor...
Definition: Worker.cxx:856
Event manipulation API.
Definition: api.h:23
@ evntUser
Definition: ModuleItem.h:52
Event structure, exchanged between DABC threads.
Definition: Thread.h:70