Belle II Software  release-08-01-10
BinData.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include "daq/storage/BinData.h"
9 
10 #include "daq/slc/base/IOException.h"
11 
12 #include <cstdio>
13 
14 using namespace Belle2;
15 
16 const unsigned int BinData::TRAILER_MAGIC = 0x7FFF0007;
17 
18 BinData::BinData(void* buf)
19 {
20  if (buf != NULL) {
21  setBuffer(buf);
22  m_header->nword_in_header = sizeof(BinHeader) / 4;
23  m_header->nword = (sizeof(BinHeader) + sizeof(BinTrailer)) / 4;
24  m_header->nevent_nboard = 0;
25  m_header->exp_run = 0;
26  m_header->event_number = 0;
27  m_header->node_id = 0;
28  }
29 }
30 
31 BinData::~BinData() noexcept
32 {
33 
34 }
35 
36 void BinData::setBuffer(void* buf)
37 {
38  if (buf != NULL) {
39  m_buf = (int*)buf;
40  m_header = (BinHeader*)buf;
41  m_body = (unsigned int*)(((char*)buf) + sizeof(BinHeader));
42  } else {
43  m_buf = NULL;
44  }
45 }
46 
47 unsigned int BinData::recvEvent(TCPSocket& socket)
48 {
49  unsigned int count = socket.read(m_header, sizeof(BinHeader));
50  if (m_header->nword_in_header * 4 != sizeof(BinHeader)) {
51  throw (IOException("unexpected header size %d words",
52  m_header->nword_in_header));
53  }
54  const int nbytes_remains = getByteSize() - sizeof(BinHeader);
55  count += socket.read(m_body, nbytes_remains);
56  m_trailer = (BinTrailer*)(((char*)m_body) + nbytes_remains - sizeof(BinTrailer));
57  if (m_trailer->magic != TRAILER_MAGIC) {
58  throw (IOException("unexpected header trailer magic %08x",
59  m_trailer->magic));
60  }
61  return count;
62 }
63 
64 unsigned int BinData::sendEvent(TCPSocket& socket) const
65 {
66  return socket.write(m_buf, getByteSize());
67 }
68 
69 void BinData::print() noexcept
70 {
71  printf("headerwords=%d, nwords=%d, nboard=%d, nevent=%d, evtno=%d, expno=%d, runno=%d, subno=%d, nodeid=%d, trailer=%04x\n",
72  getHeaderWordSize(),
73  getWordSize(),
74  // getBodyWordSize(),
75  getNBoard(),
76  getNEvent(),
77  getEventNumber(),
78  getExpNumber(),
79  getRunNumber(),
80  getSubNumber(),
81  getNodeId(),
82  getTrailerMagic());
83 }
Abstract base class for different kinds of events.