DABC (Data Acquisition Backbone Core)  2.9.9
Iterator.cxx
Go to the documentation of this file.
1 // $Id: Iterator.cxx 3283 2015-07-21 07:21:30Z 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 #include "mbs/Iterator.h"
17 
18 #include "dabc/logging.h"
19 
21  fFirstEvent(false),
22  fEvPtr(),
23  fSubPtr(),
24  fRawPtr()
25 {
26 }
27 
29  fFirstEvent(false),
30  fEvPtr(),
31  fSubPtr(),
32  fRawPtr()
33 {
34  Reset(buf);
35 }
36 
38  fFirstEvent(src.fFirstEvent),
39  fEvPtr(src.fEvPtr),
40  fSubPtr(src.fSubPtr),
41  fRawPtr(src.fRawPtr)
42 {
43 }
44 
46 {
47  fFirstEvent = src.fFirstEvent;
48  fEvPtr = src.fEvPtr;
49  fSubPtr = src.fSubPtr;
50  fRawPtr = src.fRawPtr;
51 
52  return *this;
53 }
54 
56 {
57  Close();
58 
59  if (buf.null()) return false;
60 
61  if (buf.GetTypeId() != mbt_MbsEvents) {
62  EOUT("Only buffer format mbt_MbsEvents is supported");
63  return false;
64  }
65 
66  fEvPtr = buf;
67  fFirstEvent = true;
68 
69  return true;
70 }
71 
73 {
74  fEvPtr.reset();
75  fSubPtr.reset();
76  fRawPtr.reset();
77  fFirstEvent = false;
78 }
79 
81 {
82  if (fEvPtr.null()) return true;
83 
84  return fEvPtr.fullsize() <= evnt()->FullSize();
85 }
86 
87 
89 {
90  if (fEvPtr.null()) return false;
91 
92  if (fFirstEvent)
93  fFirstEvent = false;
94  else
95  fEvPtr.shift(evnt()->FullSize());
96 
97  if (fEvPtr.fullsize() < sizeof(EventHeader)) {
98  fEvPtr.reset();
99  return false;
100  }
101 
102  if (fEvPtr.rawsize() < sizeof(EventHeader)) {
103  EOUT("Raw size less than event header - not supported !!!!");
104 
105  fEvPtr.reset();
106  return false;
107  }
108 
109  if (fEvPtr.fullsize() < evnt()->FullSize()) {
110  EOUT("Error in MBS format - declared event size %u bigger than actual portion in the buffer %u",
111  (unsigned) evnt()->FullSize(), (unsigned) fEvPtr.fullsize());
112  fEvPtr.reset();
113  return false;
114  }
115 
116  fSubPtr.reset();
117  fRawPtr.reset();
118 
119  return true;
120 }
121 
123 {
124  if (evnt()==0) return 0;
125  unsigned res = evnt()->FullSize();
126  return res < fEvPtr.fullsize() ? res : fEvPtr.fullsize();
127 
128 }
129 
131 {
132  dabc::Pointer res;
133  AssignEventPointer(res);
134  return res;
135 }
136 
138 {
139  dabc::Pointer res;
140  AssignEventPointer(res);
141  if (!res.null()) res.shift(sizeof(mbs::EventHeader));
142  return res;
143 }
144 
145 
147 {
148  if (fEvPtr.null()) {
149  ptr.reset();
150  return false;
151  }
152  ptr.reset(fEvPtr, 0, evnt()->FullSize());
153  return true;
154 }
155 
156 
158 {
159  if (fSubPtr.null()) {
160  if (fEvPtr.null()) return false;
161  if (evnt()->FullSize() < sizeof(EventHeader)) {
162  EOUT("Mbs format error - event fullsize %u too small", evnt()->FullSize());
163  return false;
164  }
165  fSubPtr.reset(fEvPtr, 0, evnt()->FullSize());
166  fSubPtr.shift(sizeof(EventHeader));
167  } else
168  fSubPtr.shift(subevnt()->FullSize());
169 
170  if (fSubPtr.fullsize() < sizeof(SubeventHeader)) {
171  fSubPtr.reset();
172  return false;
173  }
174 
175  if (subevnt()->FullSize() < sizeof(SubeventHeader)) {
176  EOUT("Mbs format error - subevent fullsize too small");
177  fSubPtr.reset();
178  return false;
179  }
180 
181  fRawPtr.reset(fSubPtr, 0, subevnt()->FullSize());
182  fRawPtr.shift(sizeof(SubeventHeader));
183 
184  return true;
185 }
186 
188 {
189  ReadIterator iter(buf);
190  unsigned cnt(0);
191  while (iter.NextEvent()) cnt++;
192  return cnt;
193 }
194 
195 // ===================================================================
196 
198  fBuffer(),
199  fEvPtr(),
200  fSubPtr(),
201  fFullSize(0)
202 {
203 }
204 
206  fBuffer(),
207  fEvPtr(),
208  fSubPtr(),
209  fFullSize(0)
210 {
211  Reset(buf);
212 }
213 
215 {
216  Close();
217 }
218 
220 {
221  fBuffer.Release();
222  fEvPtr.reset();
223  fSubPtr.reset();
224  fFullSize = 0;
225 
226  if (buf.GetTotalSize() < sizeof(EventHeader) + sizeof(SubeventHeader)) {
227  EOUT("Buffer too small for just empty MBS event");
228  return false;
229  }
230 
231  fBuffer << buf;
232  fBuffer.SetTypeId(mbt_MbsEvents);
233  fEvPtr = fBuffer;
234  return true;
235 }
236 
238 {
239  if (!fSubData.null()) FinishSubEvent();
240  if (!fSubPtr.null()) FinishEvent();
241  unsigned full_size = fEvPtr.distance_to_ownbuf();
242  if (full_size != fFullSize)
243  EOUT("Sizes mismatch");
244 
245  fEvPtr.reset();
246 
247  if ((fFullSize>0) && (fBuffer.GetTotalSize() >= fFullSize))
248  fBuffer.SetTotalSize(fFullSize);
249  fFullSize = 0;
250 
251  dabc::Buffer res;
252  res << fBuffer;
253  return res;
254 }
255 
257 {
258  if (newbuf.null()) return Close();
259 
260  if (!IsAnyIncompleteData()) {
261  dabc::Buffer oldbuf = Close();
262  Reset(newbuf);
263  return oldbuf;
264  }
265 
266 
267  if (!fEvPtr.is_same_buf(fSubPtr)) EOUT("Place 111");
268 
269  unsigned subptr_shift = fEvPtr.distance_to(fSubPtr);
270  unsigned copy_size = subptr_shift;
271  unsigned subdata_shift(0);
272 
273  if (!fSubData.null()) {
274  if (!fSubData.is_same_buf(fSubPtr)) EOUT("Place 222");
275  subdata_shift = fSubPtr.distance_to(fSubData);
276  copy_size += subdata_shift;
277  }
278 
279  // this is copy of uncompleted data
280  dabc::Pointer(newbuf).copyfrom(fEvPtr, copy_size);
281 
282  // forgot about event as it was
283  DiscardEvent();
284 
285  // close iterator and fill ready data into the buffer
286  dabc::Buffer oldbuf = Close();
287 
288  Reset(newbuf);
289  fSubPtr.reset(fEvPtr, subptr_shift);
290  if (subdata_shift>0) fSubData.reset(fSubPtr, subdata_shift);
291 
292  dabc::Buffer res;
293  res << fBuffer;
294  return res;
295 }
296 
297 
298 bool mbs::WriteIterator::IsPlaceForEvent(uint32_t subeventssize, bool add_subev_header)
299 {
300  return fEvPtr.fullsize() >= (sizeof(EventHeader) + subeventssize + (add_subev_header ? sizeof(SubeventHeader) : 0));
301 }
302 
303 
304 bool mbs::WriteIterator::NewEvent(EventNumType event_number, uint32_t minsubeventssize)
305 {
306 
307  if (!fSubPtr.null()) {
308  EOUT("Previous event not closed");
309  return false;
310  }
311 
312  if (fEvPtr.null()) {
313  EOUT("No any place for new event");
314  return false;
315  }
316 
317  if (fEvPtr.fullsize() < (sizeof(EventHeader) + minsubeventssize)) {
318  EOUT("Too few place for the new event");
319  return false;
320  }
321 
322  fSubPtr.reset(fEvPtr, sizeof(EventHeader));
323 
324  evnt()->Init(event_number);
325 
326  return true;
327 }
328 
329 bool mbs::WriteIterator::NewSubevent(uint32_t minrawsize, uint8_t crate, uint16_t procid, uint8_t control)
330 {
331  if (fSubPtr.null()) return false;
332 
333  if (!fSubData.null()) {
334  EOUT("Previous subevent not yet finished!!!");
335  return false;
336  }
337 
338  if (fSubPtr.fullsize() < (sizeof(SubeventHeader) + minrawsize)) return false;
339 
340  subevnt()->Init(crate, procid, control);
341 
342  fSubData.reset(fSubPtr, sizeof(SubeventHeader));
343 
344  return true;
345 }
346 
348 {
349  if (fSubPtr.null()) return false;
350 
351  if (!fSubData.null()) {
352  EOUT("Previous subevent not yet finished!!!");
353  return false;
354  }
355 
356  subevnt()->InitFull(fullid);
357 
358  fSubData.reset(fSubPtr, sizeof(SubeventHeader));
359 
360  return true;
361 }
362 
363 
364 
366 {
367  return len <= fSubData.fullsize();
368 }
369 
370 
371 bool mbs::WriteIterator::AddRawData(const void* src, unsigned len)
372 {
373  if (fSubData.null() || (len > fSubPtr.fullsize())) return false;
374  fSubData.copyfrom_shift(src, len);
375  return true;
376 }
377 
378 bool mbs::WriteIterator::FinishSubEvent(uint32_t rawdatasz)
379 {
380 // DOUT0("Subptr %s subdata %s", DBOOL(fSubPtr.null()), DBOOL(fSubData.null()));
381 
382  if (fSubPtr.null()) return false;
383 
384  unsigned filled_rawsize = 0;
385  if (fSubData.null()) {
386  // maximum size was filled in such case
387  filled_rawsize = maxrawdatasize();
388  } else {
389  if (!fSubData.is_same_buf(fSubPtr)) EOUT("Place 333");
390  filled_rawsize = fSubPtr.distance_to(fSubData) - sizeof(SubeventHeader);
391  }
392 
393  if (rawdatasz==0) rawdatasz = filled_rawsize;
394 
395  if (rawdatasz > maxrawdatasize()) {
396  EOUT("So big raw data size %u not possible, maximum is %u", rawdatasz, maxrawdatasize());
397  rawdatasz = maxrawdatasize();
398  }
399 
400  subevnt()->SetRawDataSize(rawdatasz);
401 
402  fSubPtr.shift(subevnt()->FullSize());
403 
404  fSubData.reset();
405 
406  return true;
407 }
408 
410 {
411  if (fSubPtr.null()) return false;
412 
413  if (fSubPtr.fullsize() < source.fullsize()) return false;
414 
415  fSubPtr.copyfrom(source);
416 
417  // we expect here, that subevent has correct length
418  fSubPtr.shift(source.fullsize());
419 
420  return true;
421 }
422 
424 {
425  return AddSubevent( dabc::Pointer(sub, sub->FullSize()) );
426 }
427 
429 {
430  if (fEvPtr.null()) return false;
431 
432 
433  dabc::BufferSize_t fullsize = fEvPtr.fullsize();
434 
435  if (!fSubPtr.null()) {
436  if (!fEvPtr.is_same_buf(fSubPtr)) EOUT("Place 444");
437  fullsize = fEvPtr.distance_to(fSubPtr);
438  }
439 
440  evnt()->SetFullSize(fullsize);
441  fEvPtr.shift(fullsize);
442 
443  fSubPtr.reset();
444  fFullSize += fullsize;
445 
446  return true;
447 }
448 
450 {
451  fSubData.reset();
452  fSubPtr.reset();
453 }
454 
455 
457 {
458  if (!fSubPtr.null()) {
459  EOUT("Previous event not closed");
460  return false;
461  }
462 
463  if (fEvPtr.null()) {
464  EOUT("No any place for new event");
465  return false;
466  }
467 
468  if (fEvPtr.fullsize() < ptr.fullsize()) {
469  EOUT("Too few place for the new event");
470  return false;
471  }
472 
473  fEvPtr.copyfrom(ptr);
474 
475  fSubPtr.reset(fEvPtr, ptr.fullsize());
476 
477  if (finish) return FinishEvent();
478 
479  return true;
480 }
481 
Reference on memory from memory pool.
Definition: Buffer.h:135
unsigned GetTypeId() const
Definition: Buffer.h:152
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
Definition: Buffer.cxx:91
void SetTypeId(unsigned tid)
Definition: Buffer.h:151
Manipulator with dabc::Buffer class.
Definition: Pointer.h:34
BufferSize_t shift(BufferSize_t sz)
Definition: Pointer.h:153
void reset(void *buf, BufferSize_t sz)
Definition: Pointer.h:99
BufferSize_t copyfrom(const Pointer &src, BufferSize_t sz=0)
Returns actual size copied.
Definition: Pointer.cxx:54
bool null() const
Definition: Pointer.h:147
BufferSize_t fullsize() const
Definition: Pointer.h:149
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Read iterator for HADAQ events/subevents.
Definition: Iterator.h:39
bool NextEvent()
Used for ready HLD events.
Definition: Iterator.cxx:127
Read iterator for MBS events/subevents.
Definition: Iterator.h:40
unsigned GetEventSize() const
Definition: Iterator.cxx:122
bool Reset()
Reset iterator - forget pointer on buffer.
Definition: Iterator.h:63
dabc::Pointer fEvPtr
Definition: Iterator.h:43
dabc::Pointer fRawPtr
Definition: Iterator.h:45
dabc::Pointer GetSubeventsPointer()
Returns pointer, which assign only on subevents.
Definition: Iterator.cxx:137
static unsigned NumEvents(const dabc::Buffer &buf)
Definition: Iterator.cxx:187
bool AssignEventPointer(dabc::Pointer &ptr)
Definition: Iterator.cxx:146
ReadIterator & operator=(const ReadIterator &src)
Definition: Iterator.cxx:45
bool IsLastEvent() const
Returns true if it is last event in the buffer.
Definition: Iterator.cxx:80
dabc::Pointer GetEventPointer()
Returns pointer, which assign on complete event, including header.
Definition: Iterator.cxx:130
dabc::Pointer fSubPtr
Definition: Iterator.h:44
bool Reset(dabc::Buffer &buf)
Definition: Iterator.cxx:219
bool AddSubevent(const dabc::Pointer &source)
Definition: Iterator.cxx:409
bool FinishSubEvent(uint32_t rawdatasz=0)
Definition: Iterator.cxx:378
bool AddRawData(const void *src, unsigned len)
Definition: Iterator.cxx:371
dabc::Buffer CloseAndTransfer(dabc::Buffer &newbuf)
Method could be used to close current buffer and transfer all uncompleted parts to new buffer.
Definition: Iterator.cxx:256
bool NewSubevent(uint32_t minrawsize=0, uint8_t crate=0, uint16_t procid=0, uint8_t control=0)
Definition: Iterator.cxx:329
void DiscardEvent()
Ignore all data, which were add with last NewEvent call.
Definition: Iterator.cxx:449
bool IsPlaceForRawData(unsigned len) const
Definition: Iterator.cxx:365
bool NewSubevent2(uint32_t fullid)
Definition: Iterator.cxx:347
bool CopyEventFrom(const dabc::Pointer &ptr, bool finish=true)
Method to copy full event from the other place.
Definition: Iterator.cxx:456
bool NewEvent(EventNumType event_number=0, uint32_t subeventsize=0)
Definition: Iterator.cxx:304
dabc::Buffer Close()
Definition: Iterator.cxx:237
bool IsPlaceForEvent(uint32_t subeventsize, bool add_subev_header=false)
Definition: Iterator.cxx:298
unsigned fullid
Definition: hldprint.cxx:995
#define EOUT(args ...)
Definition: logging.h:150
uint32_t BufferSize_t
Definition: Buffer.h:32
@ mbt_MbsEvents
Definition: MbsTypeDefs.h:348
uint32_t EventNumType
Definition: MbsTypeDefs.h:94
uint32_t FullSize() const
Definition: LmdTypeDefs.h:55
MBS subevent
Definition: MbsTypeDefs.h:40