DABC (Data Acquisition Backbone Core)  2.9.9
ModuleItem.h
Go to the documentation of this file.
1 // $Id: ModuleItem.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_ModuleItem
17 #define DABC_ModuleItem
18 
19 #ifndef DABC_Worker
20 #include "dabc/Worker.h"
21 #endif
22 
23 namespace dabc {
24 
25  class Module;
26  class ModuleAsync;
27  class ModuleSync;
28 
30  mitInpPort, // input port
31  mitOutPort, // output port
32  mitPool, // pool handle
33  mitParam, // parameter
34  mitTimer, // timer
35  mitConnTimer, // timer to reconnect ports
36  mitUser // item to produce/receive user-specific events
37  };
38 
48  evntPortError, // produce when port on other side disconnects with error
49  evntConnStart, // event produce when other side of connection is stared
50  evntConnStop, // event produce when other side of connection is stopped
51  evntModuleLast, // last event id, used by Module itself
53  };
54 
56  moduleitemMinId = 1, // minimum possible id of item
57  moduleitemMaxId = 65534, // maximum possible id of item
58  moduleitemAnyId = 65535 // special id to identify any item (used in WaitForEvent)
59  };
60 
68  class ModuleItem : public Worker {
69  friend class Module;
70  friend class ModuleAsync;
71  friend class ModuleSync;
72 
73  protected:
74 
75  int fItemType; // kind of the item
76  unsigned fItemId; // sequence id of the item in complete items list
77  unsigned fSubId; // sequence number of input/output/pool/timer port, used by module
78 
79  ModuleItem(int typ, Reference parent, const std::string &name);
80 
81  virtual bool ItemNeedThread() const { return false; }
82 
83  void SetItemId(unsigned id) { fItemId = id; }
84  void SetItemSubId(unsigned id) { fSubId = id; SetWorkerCfgId((int)id); }
85 
86  void SetItemPriority(int pri = -1) { SetWorkerPriority((pri < 0) ? 1 : pri); }
87 
88  virtual void DoStart() {}
89  virtual void DoStop() {}
90 
92  virtual void DoCleanup() {}
93 
95  void StopModule();
96 
98  void StartModule();
99 
100  public:
101  virtual ~ModuleItem();
102 
103  inline int GetType() const { return fItemType; }
104  inline unsigned ItemId() const { return fItemId; }
105  inline unsigned ItemSubId() const { return fSubId; }
106  };
107 
108 
109  // _____________________________________________________________________
110 
116  class ModuleItemRef : public WorkerRef {
118 
119  WorkerRef GetModule() const;
120 
121  unsigned ItemId() const { return GetObject() ? GetObject()->ItemId() : 0; }
122 
123  unsigned ItemSubId() const { return GetObject() ? GetObject()->ItemSubId() : 0; }
124 
125  std::string InfoParName() const;
126  };
127 
128  // ==================================================================================
129 
149  class Timer : public ModuleItem {
150 
151  friend class Module;
152 
153  protected:
154  bool fSysTimer;
155  double fPeriod; // period of timer events
156  long fCounter; // number of generated events
157  bool fActive; // is timer active
158  bool fSynhron; // indicate if timer tries to keep number of events per second
159  double fInaccuracy; // accumulated inaccuracy of timer in synchron mode
160  WorkerRef fTimerSrc; // source for the timer
161 
162  virtual bool ItemNeedThread() const { return !IsSysTimer(); }
163 
164  bool IsSysTimer() const { return fSysTimer; }
165 
166  void SingleShoot(double delay) { ActivateTimeout(delay); }
167 
168  virtual ~Timer();
169 
170  double GetPeriod() const { return fPeriod; }
171  void SetPeriod(double v) { fPeriod = v; }
172 
173  bool IsSynchron() const { return fSynhron; }
174  void SetSynchron(bool on = true) { fSynhron = on; }
175 
176  long GetCounter() const { return fCounter; }
177 
178  virtual void DoStart();
179  virtual void DoStop();
180  virtual void DoCleanup() { fTimerSrc.Release(); }
181 
182  virtual double ProcessTimeout(double last_diff);
183 
184  private:
185  Timer(Reference parent, bool systimer, const std::string &name, double timeout = -1., bool synchron = false);
186 
187  };
188 
189  // ==================================================================================
190 
197  class ConnTimer : public ModuleItem {
198  friend class Module;
199 
200  protected:
201 
202  std::string fPortName;
203  bool fErrorFlag;
204 
205  virtual bool ItemNeedThread() const { return true; }
206 
207  virtual double ProcessTimeout(double last_diff);
208 
209  void Activate(double period) { ActivateTimeout(period); }
210 
211  private:
212 
213  ConnTimer(Reference parent, const std::string &name, const std::string &portname);
214  };
215 
216 }
217 
218 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Special timer to reestablish port connections in the module.
Definition: ModuleItem.h:197
bool fErrorFlag
indicate why reconnection was started
Definition: ModuleItem.h:203
ConnTimer(Reference parent, const std::string &name, const std::string &portname)
Definition: ModuleItem.cxx:149
std::string fPortName
port name, which should be reconnected
Definition: ModuleItem.h:202
virtual bool ItemNeedThread() const
Definition: ModuleItem.h:205
void Activate(double period)
Definition: ModuleItem.h:209
virtual double ProcessTimeout(double last_diff)
Definition: ModuleItem.cxx:156
Base class for user-derived code, implementing event-processing.
Definition: ModuleAsync.h:32
Reference on dabc::ModuleItem class
Definition: ModuleItem.h:116
WorkerRef GetModule() const
Definition: ModuleItem.cxx:66
unsigned ItemSubId() const
Definition: ModuleItem.h:123
unsigned ItemId() const
Definition: ModuleItem.h:121
std::string InfoParName() const
Definition: ModuleItem.cxx:79
Base class for module items like ports, timers, pool handles.
Definition: ModuleItem.h:68
unsigned fItemId
Definition: ModuleItem.h:76
void StartModule()
Starts module, should be called from module thread.
Definition: ModuleItem.cxx:57
int GetType() const
Definition: ModuleItem.h:103
virtual ~ModuleItem()
Definition: ModuleItem.cxx:38
void SetItemPriority(int pri=-1)
Definition: ModuleItem.h:86
virtual bool ItemNeedThread() const
Definition: ModuleItem.h:81
void SetItemSubId(unsigned id)
Definition: ModuleItem.h:84
ModuleItem(int typ, Reference parent, const std::string &name)
Definition: ModuleItem.cxx:20
virtual void DoCleanup()
Called when module object is cleaned up - should release all references if any.
Definition: ModuleItem.h:92
unsigned fSubId
Definition: ModuleItem.h:77
void SetItemId(unsigned id)
Definition: ModuleItem.h:83
virtual void DoStop()
Definition: ModuleItem.h:89
unsigned ItemId() const
Definition: ModuleItem.h:104
unsigned ItemSubId() const
Definition: ModuleItem.h:105
void StopModule()
Stops module, should be called from module thread.
Definition: ModuleItem.cxx:50
virtual void DoStart()
Definition: ModuleItem.h:88
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
Reference on the arbitrary object
Definition: Reference.h:73
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Provides timer event to the module.
Definition: ModuleItem.h:149
void SetSynchron(bool on=true)
Definition: ModuleItem.h:174
double fPeriod
indicate that timer uses module timeouts
Definition: ModuleItem.h:155
virtual void DoStop()
Definition: ModuleItem.cxx:113
void SetPeriod(double v)
Definition: ModuleItem.h:171
double fInaccuracy
Definition: ModuleItem.h:159
Timer(Reference parent, bool systimer, const std::string &name, double timeout=-1., bool synchron=false)
Definition: ModuleItem.cxx:88
WorkerRef fTimerSrc
Definition: ModuleItem.h:160
virtual double ProcessTimeout(double last_diff)
Definition: ModuleItem.cxx:122
bool fActive
Definition: ModuleItem.h:157
bool IsSynchron() const
Definition: ModuleItem.h:173
virtual ~Timer()
Definition: ModuleItem.cxx:100
long GetCounter() const
Definition: ModuleItem.h:176
virtual void DoStart()
Definition: ModuleItem.cxx:104
double GetPeriod() const
Definition: ModuleItem.h:170
virtual bool ItemNeedThread() const
Definition: ModuleItem.h:162
bool fSysTimer
Definition: ModuleItem.h:154
bool fSynhron
Definition: ModuleItem.h:158
bool IsSysTimer() const
Definition: ModuleItem.h:164
void SingleShoot(double delay)
Definition: ModuleItem.h:166
long fCounter
Definition: ModuleItem.h:156
virtual void DoCleanup()
Called when module object is cleaned up - should release all references if any.
Definition: ModuleItem.h:180
Reference on dabc::Worker
Definition: Worker.h:466
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
void SetWorkerCfgId(int id=-1)
Set identifier which can be used in XML configuration ${}# formula It means that correspondent to the...
Definition: Worker.h:190
@ evntFirstSystem
Definition: Worker.h:202
@ evntFirstUser
Definition: Worker.h:203
void SetWorkerPriority(int nq)
Definition: Worker.h:259
Event manipulation API.
Definition: api.h:23
EModuleItemType
Definition: ModuleItem.h:29
@ mitOutPort
Definition: ModuleItem.h:31
@ mitTimer
Definition: ModuleItem.h:34
@ mitConnTimer
Definition: ModuleItem.h:35
@ mitParam
Definition: ModuleItem.h:33
@ mitUser
Definition: ModuleItem.h:36
@ mitPool
Definition: ModuleItem.h:32
@ mitInpPort
Definition: ModuleItem.h:30
EModuleEvents
Definition: ModuleItem.h:39
@ evntInputReinj
Definition: ModuleItem.h:43
@ evntConnStop
Definition: ModuleItem.h:50
@ evntPortDisconnect
Definition: ModuleItem.h:47
@ evntUser
Definition: ModuleItem.h:52
@ evntTimeout
Definition: ModuleItem.h:45
@ evntInput
Definition: ModuleItem.h:41
@ evntPortConnect
Definition: ModuleItem.h:46
@ evntConnStart
Definition: ModuleItem.h:49
@ evntOutputReinj
Definition: ModuleItem.h:44
@ evntModuleLast
Definition: ModuleItem.h:51
@ evntPortError
Definition: ModuleItem.h:48
@ evntOutput
Definition: ModuleItem.h:42
@ evntModuleNone
Definition: ModuleItem.h:40
EModelItemConsts
Definition: ModuleItem.h:55
@ moduleitemAnyId
Definition: ModuleItem.h:58
@ moduleitemMinId
Definition: ModuleItem.h:56
@ moduleitemMaxId
Definition: ModuleItem.h:57