DABC (Data Acquisition Backbone Core)  2.9.9
DataIO.h
Go to the documentation of this file.
1 // $Id: DataIO.h 4575 2020-08-04 12:44:29Z 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_DataIO
17 #define DABC_DataIO
18 
19 #ifndef DABC_Worker
20 #include "dabc/Worker.h"
21 #endif
22 
23 #ifndef DABC_Url
24 #include "dabc/Url.h"
25 #endif
26 
27 namespace dabc {
28 
29  class Buffer;
30  class InputTransport;
31 
33  di_ValidSize = 0xFFFFFFF0, // last valid size for buffer
34  di_None = 0xFFFFFFF1, // invalid return code
35  di_Repeat = 0xFFFFFFF2, // no data in input, try as soon as possible
36  di_RepeatTimeOut = 0xFFFFFFF3, // no data in input, try with timeout
37  di_EndOfStream = 0xFFFFFFF4, // no more data in input is expected, object can be destroyed
38  di_Ok = 0xFFFFFFF5, // normal code
39  di_CallBack = 0xFFFFFFF6, // data source want to work via callback
40  di_Error = 0xFFFFFFF7, // error
41  di_SkipBuffer = 0xFFFFFFF8, // when doing complete, buffer cannot be filled
42  di_DfltBufSize = 0xFFFFFFF9, // default buffer size means that any size provided by memory pool will be accepted
43  di_NeedMoreBuf = 0xFFFFFFFA, // when input needs more buffer for it work, DataInput can initiate callback at any time
44  di_HasEnoughBuf = 0xFFFFFFFB, // input indicates that enough buffers are accumulated and transport should wait for callback
45  di_MoreBufReady = 0xFFFFFFFC, // when input has more bufs ready for complete
46  di_QueueBufReady = 0xFFFFFFFD // when data input can provide next buffer
47  };
48 
61  class DataInput {
62  friend class InputTransport;
63 
64  protected:
65 
68  virtual WorkerAddon* Read_GetAddon() { return 0; }
69 
70  public:
71 
72  virtual ~DataInput() {}
73 
82  virtual bool Read_Init(const WorkerRef& wrk, const Command& cmd) { return true; }
83 
84 
95  virtual unsigned Read_Size() { return di_EndOfStream; }
96 
103  virtual unsigned Read_Start(Buffer& buf) { return di_Ok; }
104 
114  virtual unsigned Read_Complete(Buffer& buf) { return di_EndOfStream; }
115 
122  virtual double Read_Timeout() { return 0.1; }
123 
124 
128  virtual bool Read_Stat(dabc::Command cmd) { return false; }
129 
135  Buffer ReadBuffer();
136 
137  };
138 
139  // _________________________________________________________________
140 
142  do_Ok, // operation is ok
143  do_Skip, // if returned, buffer must be skipped
144  do_Repeat, // repeat operation as soon as possible
145  do_RepeatTimeOut, // repeat operation after timeout
146  do_CallBack, // operation will be called back
147  do_Error, // operation error, object must be cleaned
148  do_Close // output is closed, one should destroy it
149  };
150 
151 
158  class DataOutput {
159 
160  friend class OutputTransport;
161 
162  protected:
163  std::string fInfoName; // parameter name for info settings
164 
165  DataOutput(const dabc::Url& url);
166 
169  virtual WorkerAddon* Write_GetAddon() { return 0; }
170 
171  void ShowInfo(int lvl, const std::string &info);
172 
173  public:
174 
175  virtual ~DataOutput() {}
176 
178  void SetInfoParName(const std::string &name);
179 
181  virtual std::string ProvideInfo() { return std::string(); }
182 
186  virtual bool Write_Init() { return true; }
187 
196  virtual unsigned Write_Check() { return do_Ok; }
197 
204  virtual unsigned Write_Buffer(Buffer& buf) { return do_Ok; }
205 
211  virtual unsigned Write_Complete() { return do_Ok; }
212 
215  virtual double Write_Timeout() { return 0.1; }
216 
218  virtual void Write_Flush() {}
219 
221  bool WriteBuffer(Buffer& buf);
222 
224  virtual bool Write_Stat(dabc::Command cmd) { return false; }
225 
227  virtual bool Write_Retry() { return false; }
228 
230  virtual bool Write_Restart(dabc::Command cmd) { return false; }
231 
232  };
233 
234  // ===========================================================
235 
236  class FileInterface;
237 
246  class FileInput : public DataInput {
247  protected:
248  std::string fFileName;
251  std::string fCurrentName;
252  bool fLoop;
253  double fReduce;
254 
255  bool InitFilesList();
256  bool TakeNextFileName();
257  const std::string &CurrentFileName() const { return fCurrentName; }
258  void ClearCurrentFileName() { fCurrentName.clear(); }
259 
260  FileInput(const dabc::Url& url);
261 
262  public:
263 
264  virtual ~FileInput();
265 
266  void SetIO(dabc::FileInterface* io);
267 
268  virtual bool Read_Init(const WorkerRef& wrk, const Command& cmd);
269 
270  virtual bool Read_Stat(dabc::Command cmd);
271  };
272 
273  // ============================================================================
274 
283  class FileOutput : public DataOutput {
284  protected:
285 
286  std::string fFileName;
288  std::string fFileExtens;
289 
291 
293  std::string fCurrentFileName;
295 
299 
300  void ProduceNewFileName();
301  const std::string &CurrentFileName() const { return fCurrentFileName; }
302 
304  bool CheckBufferForNextFile(unsigned sz);
305 
306  void AccountBuffer(unsigned sz, int numev = 0);
307 
308  FileOutput(const dabc::Url& url, const std::string &ext = "");
309 
310  std::string ProduceFileName(const std::string &suffix);
311 
312  int GetSizeLimitMB() const { return fSizeLimitMB; }
313 
314  public:
315 
316  virtual ~FileOutput();
317 
318  void SetIO(dabc::FileInterface* io);
319 
320  virtual std::string ProvideInfo();
321 
322  virtual bool Write_Init();
323 
324  virtual bool Write_Stat(dabc::Command cmd);
325  };
326 
327 }
328 
329 #endif
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
Interface for implementing any kind of data input.
Definition: DataIO.h:61
virtual ~DataInput()
Definition: DataIO.h:72
virtual unsigned Read_Start(Buffer &buf)
Prepare buffer for reading (if required)
Definition: DataIO.h:103
virtual double Read_Timeout()
Provide timeout value.
Definition: DataIO.h:122
virtual unsigned Read_Size()
Defines required buffer size for next operation.
Definition: DataIO.h:95
virtual unsigned Read_Complete(Buffer &buf)
Complete reading of the buffer from source,.
Definition: DataIO.h:114
Buffer ReadBuffer()
Reads complete buffer.
Definition: DataIO.cxx:21
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
Definition: DataIO.h:82
virtual WorkerAddon * Read_GetAddon()
Returns addon, provided by data input If specified, supposed that I/O object is double-deriver from D...
Definition: DataIO.h:68
virtual bool Read_Stat(dabc::Command cmd)
Provide timeout value.
Definition: DataIO.h:128
Interface for implementing any kind of data output.
Definition: DataIO.h:158
virtual void Write_Flush()
Flush output object, called when buffer with EOL type is appeared.
Definition: DataIO.h:218
virtual bool Write_Stat(dabc::Command cmd)
Fill different statistic parameters into provided command.
Definition: DataIO.h:224
DataOutput(const dabc::Url &url)
Definition: DataIO.cxx:50
void ShowInfo(int lvl, const std::string &info)
Definition: DataIO.cxx:61
bool WriteBuffer(Buffer &buf)
Write buffer to the output.
Definition: DataIO.cxx:79
std::string fInfoName
Definition: DataIO.h:163
virtual unsigned Write_Buffer(Buffer &buf)
Start writing of buffer to output.
Definition: DataIO.h:204
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
Definition: DataIO.h:186
void SetInfoParName(const std::string &name)
Methods set parameter name, which could be used for debug output.
Definition: DataIO.cxx:55
virtual unsigned Write_Check()
Check if output can be done.
Definition: DataIO.h:196
virtual ~DataOutput()
Definition: DataIO.h:175
virtual unsigned Write_Complete()
Complete writing of the buffer.
Definition: DataIO.h:211
virtual std::string ProvideInfo()
Method can be used to get debug info about output.
Definition: DataIO.h:181
virtual WorkerAddon * Write_GetAddon()
Returns addon, provided by data output If specified, supposed that I/O object is double-derived from ...
Definition: DataIO.h:169
virtual bool Write_Restart(dabc::Command cmd)
Method used to restart output - like recreate new output file.
Definition: DataIO.h:230
virtual bool Write_Retry()
Returns true if output object can be reinitialized for recover error.
Definition: DataIO.h:227
virtual double Write_Timeout()
Timeout in seconds for write operation.
Definition: DataIO.h:215
Interface for implementing file inputs.
Definition: DataIO.h:246
bool fLoop
read file(s) in endless loop
Definition: DataIO.h:252
bool TakeNextFileName()
Definition: DataIO.cxx:155
virtual ~FileInput()
Definition: DataIO.cxx:105
std::string fFileName
Definition: DataIO.h:248
void ClearCurrentFileName()
Definition: DataIO.h:258
std::string fCurrentName
Definition: DataIO.h:251
double fReduce
factor to reduce buffer size when reading
Definition: DataIO.h:253
const std::string & CurrentFileName() const
Definition: DataIO.h:257
dabc::Reference fFilesList
Definition: DataIO.h:249
virtual bool Read_Init(const WorkerRef &wrk, const Command &cmd)
Initialize data input, using port and command.
Definition: DataIO.cxx:139
void SetIO(dabc::FileInterface *io)
Definition: DataIO.cxx:115
bool InitFilesList()
Definition: DataIO.cxx:125
dabc::FileInterface * fIO
Definition: DataIO.h:250
virtual bool Read_Stat(dabc::Command cmd)
Provide timeout value.
Definition: DataIO.cxx:167
FileInput(const dabc::Url &url)
Definition: DataIO.cxx:92
Defines and implements basic POSIX file interface.
Definition: BinaryFile.h:33
Interface for implementing file outputs.
Definition: DataIO.h:283
bool CheckBufferForNextFile(unsigned sz)
Return true if new file should be started.
Definition: DataIO.cxx:277
void AccountBuffer(unsigned sz, int numev=0)
Definition: DataIO.cxx:286
dabc::FileInterface * fIO
Definition: DataIO.h:290
std::string ProduceFileName(const std::string &suffix)
Definition: DataIO.cxx:252
long fTotalNumBufs
Definition: DataIO.h:297
long fCurrentFileSize
Definition: DataIO.h:294
int fSizeLimitMB
Definition: DataIO.h:287
int GetSizeLimitMB() const
Definition: DataIO.h:312
long fTotalNumEvents
Definition: DataIO.h:298
std::string fFileExtens
Definition: DataIO.h:288
std::string fFileName
Definition: DataIO.h:286
virtual bool Write_Init()
This is generic virtual method to initialize output before real work is started.
Definition: DataIO.cxx:211
const std::string & CurrentFileName() const
Definition: DataIO.h:301
long fTotalFileSize
Definition: DataIO.h:296
std::string fCurrentFileName
Definition: DataIO.h:293
void SetIO(dabc::FileInterface *io)
Definition: DataIO.cxx:200
virtual std::string ProvideInfo()
Method can be used to get debug info about output.
Definition: DataIO.cxx:295
void ProduceNewFileName()
Definition: DataIO.cxx:270
virtual ~FileOutput()
Definition: DataIO.cxx:192
virtual bool Write_Stat(dabc::Command cmd)
Fill different statistic parameters into provided command.
Definition: DataIO.cxx:322
FileOutput(const dabc::Url &url, const std::string &ext="")
Definition: DataIO.cxx:177
int fCurrentFileNumber
Definition: DataIO.h:292
Base class for output transport implementations.
Reference on the arbitrary object
Definition: Reference.h:73
Uniform Resource Locator interpreter.
Definition: Url.h:33
Generic addon for dabc::Worker.
Definition: Worker.h:49
Reference on dabc::Worker
Definition: Worker.h:466
Event manipulation API.
Definition: api.h:23
DataOutputCodes
Definition: DataIO.h:141
@ do_RepeatTimeOut
Definition: DataIO.h:145
@ do_CallBack
Definition: DataIO.h:146
@ do_Repeat
Definition: DataIO.h:144
@ do_Ok
Definition: DataIO.h:142
@ do_Skip
Definition: DataIO.h:143
@ do_Close
Definition: DataIO.h:148
@ do_Error
Definition: DataIO.h:147
DataInputCodes
Definition: DataIO.h:32
@ di_Ok
Definition: DataIO.h:38
@ di_QueueBufReady
Definition: DataIO.h:46
@ di_HasEnoughBuf
Definition: DataIO.h:44
@ di_ValidSize
Definition: DataIO.h:33
@ di_CallBack
Definition: DataIO.h:39
@ di_NeedMoreBuf
Definition: DataIO.h:43
@ di_MoreBufReady
Definition: DataIO.h:45
@ di_None
Definition: DataIO.h:34
@ di_DfltBufSize
Definition: DataIO.h:42
@ di_RepeatTimeOut
Definition: DataIO.h:36
@ di_SkipBuffer
Definition: DataIO.h:41
@ di_Error
Definition: DataIO.h:40
@ di_EndOfStream
Definition: DataIO.h:37
@ di_Repeat
Definition: DataIO.h:35