Belle II Software  release-08-01-10
RevSock2Rb.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 
9 #include "daq/rfarm/event/RevSock2Rb.h"
10 
11 #include <unistd.h>
12 
13 using namespace std;
14 using namespace Belle2;
15 
16 RevSock2Rb::RevSock2Rb(string rbuf, string src, int port, string shmname, int id)
17 {
18  // m_rbuf = new RingBuffer(rbuf.c_str(), RBUFSIZE);
19  m_rbuf = new RingBuffer(rbuf.c_str());
20  m_sock = new REvtSocketRecv(src, port);
21  m_evtbuf = new char[MAXEVTSIZE];
22  m_flow = new RFFlowStat((char*)shmname.c_str(), id, m_rbuf);
23 
24 }
25 
26 RevSock2Rb::~RevSock2Rb(void)
27 {
28  delete m_sock;
29  delete m_rbuf;
30 }
31 
32 int RevSock2Rb::ReceiveEvent(void)
33 {
34  // Get a record from socket
35  EvtMessage* msg = m_sock->recv();
36  if (msg == NULL) {
37  return 0;
38  }
39  // B2INFO ( "Rx: got an event from Socket, size=" << msg->size() );
40  if (msg->type() == MSG_TERMINATE) {
41  return 0;
42  // Flag End Of File !!!!!
43  // return msg->type(); // EOF
44  }
45  m_flow->log(msg->size());
46 
47  // Put the message in ring buffer
48  int stat = 0;
49  for (;;) {
50  stat = m_rbuf->insq((int*)msg->buffer(), msg->paddedSize());
51  if (stat >= 0) break;
52  // usleep(100);
53  usleep(20);
54  }
55  delete msg;
56  return stat;
57 }
58 
59 int RevSock2Rb::Reconnect(int ntimes)
60 {
61  return (m_sock->sock())->reconnect(ntimes);
62 }
Class to manage streamed object.
Definition: EvtMessage.h:59
int paddedSize() const
Same as size(), but as size of an integer array.
Definition: EvtMessage.cc:99
ERecordType type() const
Get record type.
Definition: EvtMessage.cc:114
char * buffer()
Get buffer address.
Definition: EvtMessage.cc:76
int size() const
Get size of message including headers.
Definition: EvtMessage.cc:94
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
Abstract base class for different kinds of events.