DABC (Data Acquisition Backbone Core)  2.9.9
ModuleSync.h
Go to the documentation of this file.
1 // $Id: ModuleSync.h 3862 2018-05-11 10:06:18Z 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_ModuleSync
17 #define DABC_ModuleSync
18 
19 #ifndef DABC_Module
20 #include "dabc/Module.h"
21 #endif
22 
23 
24 namespace dabc {
25 
60  class ModuleSync : public Module {
61  private:
64 
67 
71 
74 
78 
80  uint16_t fWaitId;
81 
83  bool fWaitRes;
84 
87 
89  virtual void DoWorkerMainLoop();
90 
92  virtual void DoWorkerAfterMainLoop();
93 
94  /* Internal - central method for blocking functionality.
95  * Block main loop and waits for dedicated event.
96  * Keep processing of other events or commands.
97  * Used in framework to rise exceptions when module should be stopped */
98  bool WaitItemEvent(double& tmout,
99  ModuleItem* item = 0,
100  uint16_t *resevid = 0,
101  ModuleItem** resitem = 0);
102 
104  void AsyncProcessCommands();
105 
107  virtual int PreviewCommand(Command cmd);
108 
110  virtual void ProcessItemEvent(ModuleItem* item, uint16_t evid);
111 
112  protected:
113 
116  void StopUntilRestart();
117 
119  virtual void ObjectCleanup();
120 
122  ModuleSync(const std::string &name, Command cmd = nullptr);
123 
125  virtual ~ModuleSync();
126 
139  bool ModuleWorking(double timeout = 0.);
140 
143  virtual void MainLoop() = 0;
144 
146  uint16_t WaitEvent(double timeout = -1);
147 
149  bool WaitConnect(const std::string &name, double timeout = -1);
150 
152  bool WaitInput(unsigned indx, unsigned minqueuesize = 1, double timeout = -1);
153 
155  bool CanRecv(unsigned indx = 0) const
156  { return indx < fInputs.size() ? fInputs[indx]->CanRecv() : false; }
157 
161  Buffer Recv(unsigned indx = 0, double timeout = -1);
162 
164  bool Send(unsigned indx, Buffer& buf, double timeout = -1);
165 
167  bool Send(Buffer& buf, double timeout = -1)
168  { return Send(0, buf, timeout); }
169 
172  Buffer RecvFromAny(unsigned* port = 0, double timeout = -1);
173 
175  Buffer TakeBuffer(unsigned pool = 0, double timeout = -1);
176 
178  void SetTmoutExcept(bool on = true) { fTmoutExcept = on; }
179 
181  bool IsTmoutExcept() const { return fTmoutExcept; }
182 
184  void SetDisconnectExcept(bool on = true) { fDisconnectExcept = on; }
185 
187  bool IsDisconnectExcept() const { return fDisconnectExcept; }
188 
190  void SetSyncCommands(bool on = true) { fSyncCommands = on; }
191 
193  bool IsSyncCommands() const { return fSyncCommands; }
194 
195  public:
196 
198  virtual const char* ClassName() const { return "ModuleSync"; }
199 
200  };
201 }
202 
203 #endif
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
Queue of commands
Definition: CommandsQueue.h:39
Base class for module items like ports, timers, pool handles.
Definition: ModuleItem.h:68
Base class for user-derived code, implementing main loop.
Definition: ModuleSync.h:60
bool WaitItemEvent(double &tmout, ModuleItem *item=0, uint16_t *resevid=0, ModuleItem **resitem=0)
Definition: ModuleSync.cxx:291
Buffer RecvFromAny(unsigned *port=0, double timeout=-1)
Receive buffer from any input port.
Definition: ModuleSync.cxx:126
void SetSyncCommands(bool on=true)
Set if commands should be executed synchronous in main loop.
Definition: ModuleSync.h:190
Buffer TakeBuffer(unsigned pool=0, double timeout=-1)
Take buffer from memory pool.
Definition: ModuleSync.cxx:112
bool ModuleWorking(double timeout=0.)
Returns true when module in running state.
Definition: ModuleSync.cxx:176
void SetTmoutExcept(bool on=true)
Set if exception should be generated when any operation is timeout.
Definition: ModuleSync.h:178
bool WaitConnect(const std::string &name, double timeout=-1)
Waits for connection for specified port.
Definition: ModuleSync.cxx:46
virtual void DoWorkerMainLoop()
Internal - entrance function for main loop execution.
Definition: ModuleSync.cxx:334
virtual void ProcessItemEvent(ModuleItem *item, uint16_t evid)
Internal - central method of events processing.
Definition: ModuleSync.cxx:274
bool WaitInput(unsigned indx, unsigned minqueuesize=1, double timeout=-1)
Waits until input port has specified number of buffers in the queue.
Definition: ModuleSync.cxx:157
bool IsTmoutExcept() const
Returns true if timeout exception generation is configured.
Definition: ModuleSync.h:181
uint16_t fWaitId
Internal variable, used in wait event.
Definition: ModuleSync.h:80
bool fTmoutExcept
Flag indicates if timeout exception should be generated.
Definition: ModuleSync.h:63
bool Send(unsigned indx, Buffer &buf, double timeout=-1)
Send buffer via specified output port.
Definition: ModuleSync.cxx:67
virtual void DoWorkerAfterMainLoop()
Internal - function executed after leaving main loop.
Definition: ModuleSync.cxx:350
bool IsDisconnectExcept() const
Returns true if disconnect exception should be generated by module.
Definition: ModuleSync.h:187
ModuleItem * fWaitItem
Current item, waiting for the event.
Definition: ModuleSync.h:77
void SetDisconnectExcept(bool on=true)
Set if disconnect exception should be generated by module.
Definition: ModuleSync.h:184
bool IsSyncCommands() const
Returns true if commands should be executed synchronous in main loop.
Definition: ModuleSync.h:193
virtual const char * ClassName() const
Returns class name.
Definition: ModuleSync.h:198
virtual ~ModuleSync()
Destructor.
Definition: ModuleSync.cxx:31
bool fSyncCommands
Flag indicates if commands will be executed synchronously.
Definition: ModuleSync.h:70
virtual void ObjectCleanup()
Internal DABC method.
Definition: ModuleSync.cxx:248
virtual void MainLoop()=0
Main execution function of SyncModule.
void AsyncProcessCommands()
Internal - process commands which are submitted to sync queue.
Definition: ModuleSync.cxx:260
bool fDisconnectExcept
Flag indicates if disconnect exception should be generated.
Definition: ModuleSync.h:66
bool CanRecv(unsigned indx=0) const
Method return true if receiving from specified port can be done.
Definition: ModuleSync.h:155
virtual int PreviewCommand(Command cmd)
Internal - preview command before execution.
Definition: ModuleSync.cxx:195
uint16_t WaitEvent(double timeout=-1)
Waits for any event.
Definition: ModuleSync.cxx:186
CommandsQueue * fNewCommands
List of commands, submitted to the module.
Definition: ModuleSync.h:73
bool Send(Buffer &buf, double timeout=-1)
Send buffer via first output port.
Definition: ModuleSync.h:167
Buffer Recv(unsigned indx=0, double timeout=-1)
Receive buffer from input port.
Definition: ModuleSync.cxx:91
bool fInsideMainLoop
Flag indicates if main loop is executing.
Definition: ModuleSync.h:86
bool fWaitRes
Result of event waiting.
Definition: ModuleSync.h:83
void StopUntilRestart()
Call this method from main loop if one want suspend of module execution until new Start of the module...
Definition: ModuleSync.cxx:234
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
std::vector< InputPort * > fInputs
array of input ports
Definition: Module.h:56
friend class ModuleSync
Definition: Module.h:48
Event manipulation API.
Definition: api.h:23