DABC (Data Acquisition Backbone Core)  2.9.9
ModuleAsync.h
Go to the documentation of this file.
1 // $Id: ModuleAsync.h 4210 2019-02-12 19:55:03Z 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_ModuleAsync
17 #define DABC_ModuleAsync
18 
19 #ifndef DABC_Module
20 #include "dabc/Module.h"
21 #endif
22 
23 namespace dabc {
24 
32  class ModuleAsync : public Module {
33  private:
36  virtual bool DoStart();
37 
41  virtual void ProcessItemEvent(ModuleItem* item, uint16_t evid);
42 
43  protected:
46  ModuleAsync(const std::string &name, Command cmd = nullptr) : Module(name, cmd) { }
47 
49  virtual ~ModuleAsync();
50 
52  bool CanSend(unsigned indx = 0) const
53  { return indx < fOutputs.size() ? fOutputs[indx]->CanSend() : false; }
54 
59  unsigned NumCanSend(unsigned indx = 0) const
60  { return indx < fOutputs.size() ? fOutputs[indx]->NumCanSend() : false; }
61 
64  void ActivateOutput(unsigned port = 0);
65 
67  bool Send(unsigned indx, Buffer& buf)
68  {
69  if (indx < fOutputs.size())
70  return fOutputs[indx]->Send(buf);
71  buf.Release();
72  return false;
73  }
74 
76  inline bool Send(Buffer& buf)
77  { return Send(0, buf); }
78 
80  bool CanRecv(unsigned indx = 0) const
81  { return indx < fInputs.size() ? fInputs[indx]->CanRecv() : false; }
82 
84  unsigned NumCanRecv(unsigned indx = 0) const
85  { return indx < fInputs.size() ? fInputs[indx]->NumCanRecv() : 0; }
86 
88  BufferSize_t TotalSizeCanRecv(unsigned indx = 0) const
89  { return indx < fInputs.size() ? fInputs[indx]->TotalSizeCanRecv() : 0; }
90 
92  Buffer Recv(unsigned indx = 0)
93  { return indx < fInputs.size() ? fInputs[indx]->Recv() : Buffer(); }
94 
96  bool RecvQueueFull(unsigned port = 0);
97 
99  void SignalRecvWhenFull(unsigned port = 0);
100 
103  void ActivateInput(unsigned port = 0);
104 
106  Buffer RecvQueueItem(unsigned port = 0, unsigned nbuf = 0);
107 
109  bool SkipInputBuffers(unsigned indx = 0, unsigned nbuf = 1)
110  { return indx < fInputs.size() ? fInputs[indx]->SkipBuffers(nbuf) : false; }
111 
113  bool CanTakeBuffer(unsigned pool = 0)
114  { return pool < fPools.size() ? fPools[pool]->CanTakeBuffer() : false; }
115 
118  unsigned NumCanTake(unsigned pool = 0) const
119  { return pool < fPools.size() ? fPools[pool]->NumCanTake() : 0; }
120 
122  Buffer TakeBuffer(unsigned pool = 0)
123  { return (pool < fPools.size()) ? fPools[pool]->TakeBuffer() : ((pool==0) ? TakeDfltBuffer() : Buffer()); }
124 
126  Buffer PoolQueueItem(unsigned pool = 0, unsigned nbuf = 0);
127 
130  void ActivatePool(unsigned pool);
131 
132  // here is a list of methods,
133  // which can be reimplemented in user code
134 
135  // Level 1: Or one can redefine one or several following methods to
136  // react on specific events only
137 
142  virtual void ProcessInputEvent(unsigned port);
143 
144 
147  virtual void ProcessOutputEvent(unsigned port);
148 
149 
152  virtual void ProcessPoolEvent(unsigned pool);
153 
158  virtual void ProcessConnectEvent(const std::string &name, bool on) {}
159 
162  virtual void ProcessTimerEvent(unsigned timer) {}
163 
166  virtual void ProcessUserEvent(unsigned item) {}
167 
168 
169  // Level 2: One could process each buffer
170  // at this level user gets process calls for each new buffer
171 
175  virtual bool ProcessRecv(unsigned port = 0) { return false; }
176 
180  virtual bool ProcessSend(unsigned port = 0) { return false; }
181 
185  virtual bool ProcessBuffer(unsigned pool = 0) { return false; }
186 
187  public:
188 
190  virtual const char* ClassName() const { return "ModuleAsync"; }
191 
192  };
193 
194  // ____________________________________________________________
195 
201  class ModuleAsyncRef : public ModuleRef {
203  };
204 
205 };
206 
207 #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
Reference on dabc::ModuleAsync class
Definition: ModuleAsync.h:201
Base class for user-derived code, implementing event-processing.
Definition: ModuleAsync.h:32
BufferSize_t TotalSizeCanRecv(unsigned indx=0) const
Method return total size of buffers can be received via the port.
Definition: ModuleAsync.h:88
unsigned NumCanTake(unsigned pool=0) const
Method return number of buffers which could be taken from data queue assigned with the pool.
Definition: ModuleAsync.h:118
void ActivatePool(unsigned pool)
Produces event for specified pool handle.
ModuleAsync(const std::string &name, Command cmd=nullptr)
Constructor of ModuleAsync class.
Definition: ModuleAsync.h:46
virtual void ProcessPoolEvent(unsigned pool)
Method called by framework when pool event is produced.
bool RecvQueueFull(unsigned port=0)
Returns true if receive queue is full and block input.
Definition: ModuleAsync.cxx:23
Buffer RecvQueueItem(unsigned port=0, unsigned nbuf=0)
Returns buffer from receive queue of the input port.
Definition: ModuleAsync.cxx:35
virtual void ProcessOutputEvent(unsigned port)
Method called by framework when output event is produced.
bool CanRecv(unsigned indx=0) const
Method return true if recv from specified port can be done.
Definition: ModuleAsync.h:80
void ActivateInput(unsigned port=0)
Produces event for specified input port Should be used when processing was stopped due to return fals...
void SignalRecvWhenFull(unsigned port=0)
Let input signal only when queue is full.
Definition: ModuleAsync.cxx:29
bool SkipInputBuffers(unsigned indx=0, unsigned nbuf=1)
Remove items in input queue.
Definition: ModuleAsync.h:109
virtual ~ModuleAsync()
Destructor of ModuleAsync class.
Definition: ModuleAsync.cxx:19
virtual const char * ClassName() const
Returns class name of the object.
Definition: ModuleAsync.h:190
virtual void ProcessItemEvent(ModuleItem *item, uint16_t evid)
Generic event processing method [internal].
Definition: ModuleAsync.cxx:49
bool Send(unsigned indx, Buffer &buf)
Methods to send buffer via specified output port.
Definition: ModuleAsync.h:67
bool CanTakeBuffer(unsigned pool=0)
Returns true if memory pool can provide buffer.
Definition: ModuleAsync.h:113
virtual bool DoStart()
Activate module [internal].
Definition: ModuleAsync.cxx:85
unsigned NumCanSend(unsigned indx=0) const
Method return number of send operation can be done for specified port.
Definition: ModuleAsync.h:59
virtual void ProcessConnectEvent(const std::string &name, bool on)
Method called by framework when connection state of the item is changed.
Definition: ModuleAsync.h:158
Buffer PoolQueueItem(unsigned pool=0, unsigned nbuf=0)
Returns buffer from queue assigned with the pool.
Definition: ModuleAsync.cxx:42
virtual bool ProcessSend(unsigned port=0)
Method called by framework when at least one buffer can be send to output port.
Definition: ModuleAsync.h:180
bool Send(Buffer &buf)
Send buffer over first output port.
Definition: ModuleAsync.h:76
virtual bool ProcessBuffer(unsigned pool=0)
Method called by framework when at least one buffer available in pool handle.
Definition: ModuleAsync.h:185
void ActivateOutput(unsigned port=0)
Produces event for specified output port.
virtual void ProcessUserEvent(unsigned item)
Method called by framework when custom user event is produced.
Definition: ModuleAsync.h:166
virtual void ProcessTimerEvent(unsigned timer)
Method called by framework when timer event is produced.
Definition: ModuleAsync.h:162
unsigned NumCanRecv(unsigned indx=0) const
Method return number of buffers which can be received via the port.
Definition: ModuleAsync.h:84
virtual void ProcessInputEvent(unsigned port)
Method called by framework when input event is produced.
Buffer TakeBuffer(unsigned pool=0)
Take buffer from memory pool.
Definition: ModuleAsync.h:122
Buffer Recv(unsigned indx=0)
Methods receives buffers from the port.
Definition: ModuleAsync.h:92
virtual bool ProcessRecv(unsigned port=0)
Method called by framework when at least one buffer available in input port.
Definition: ModuleAsync.h:175
bool CanSend(unsigned indx=0) const
Method return true if send over specified port can be done.
Definition: ModuleAsync.h:52
Base class for module items like ports, timers, pool handles.
Definition: ModuleItem.h:68
Reference on dabc::Module class
Definition: Module.h:275
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
Buffer TakeDfltBuffer()
Definition: Module.cxx:258
std::vector< OutputPort * > fOutputs
array of output ports
Definition: Module.h:57
std::vector< InputPort * > fInputs
array of input ports
Definition: Module.h:56
std::vector< PoolHandle * > fPools
array of pools
Definition: Module.h:58
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
Event manipulation API.
Definition: api.h:23
uint32_t BufferSize_t
Definition: Buffer.h:32