Belle II Software development
EventSampler.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/expreco/EventSampler.h"
10
11#include <unistd.h>
12
13using namespace Belle2;
14using namespace std;
15
16// constructor/destructor
17
18EventSampler::EventSampler(const vector<string>& nodes, int port, const string& rbufname, int interval) : m_interval(interval)
19{
20 // Attach to output RingBuffer
21 m_rbuf = new RingBuffer(rbufname.c_str());
22
23 // Open EvtSocket
24 for (vector<string>::const_iterator it = nodes.begin(); it != nodes.end(); ++it) {
25 const string& nodename = *it;
26 printf("EventSampler : connecting to %s (port %d)\n", nodename.c_str(), port);
27 EvtSocketSend* sock = new EvtSocketSend(nodename.c_str(), port);
28 if (sock == NULL) {
29 printf("EventSampler : error to connect to %s\n",
30 nodename.c_str());
31 } else {
32 m_socklist.push_back(sock);
33 }
34 fflush(stdout);
35 }
36 printf("EventSampler : init : socklist = %zu\n", m_socklist.size());
37 fflush(stdout);
38}
39
40EventSampler::~EventSampler()
41{
42 for (EvtSocketSend* sock : m_socklist) {
43 delete sock;
44 }
45 m_socklist.clear();
46 delete m_rbuf;
47}
48
49int EventSampler::server()
50{
51 // printf ( "EventSampler : server started\n" );
52 fflush(stdout);
53 int nsample = 0;
54 for (;;) {
55 for (vector<EvtSocketSend*>::iterator it = m_socklist.begin();
56 it != m_socklist.end(); ++it) {
57 // Receive an event from connected socket
58 EvtSocketSend* sock = *it;
59 // printf ( "EventSampler : receiving event from sock %s\n",
60 // (sock->sock())->node() );
61 EvtMessage* msg = sock->recv();
62 // printf ( "Event Sampler : got event : %d\n", msg->size() );
63 // fflush ( stdout );
64 if (msg == NULL) {
65 printf("EventSampler : Error to receive data\n");
66 return -1;
67 }
68 // Put the message in ring buffer. If full, just skip the event.
69 m_rbuf->insq(reinterpret_cast<int*>(msg->buffer()), msg->paddedSize());
70 delete msg;
71 nsample++;
72 if (nsample % 1000 == 0)
73 printf("EventSampler : %d events sampled and queued\n", nsample);
74 usleep(m_interval);
75 }
76 }
77}
78
int paddedSize() const
Same as size(), but as size of an integer array.
Definition EvtMessage.cc:99
char * buffer()
Get buffer address.
Definition EvtMessage.cc:76
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition RingBuffer.h:39
STL iterator class.
Abstract base class for different kinds of events.
STL namespace.