Belle II Software development
EventServer.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/EventServer.h"
10#include <framework/pcore/MsgHandler.h>
11
12#include <unistd.h>
13
14using namespace Belle2;
15using namespace std;
16
17// constructor/destructor
18
19EventServer::EventServer(string rbufname, int port) : m_port(port)
20{
21 // Attach to RingBuffer
22 m_rbuf = new RingBuffer(rbufname.c_str());
23
24 // Open EvtSocket
25 m_sock = new EvtSocketRecv(m_port, false);
26 m_man = new EvtSocketManager(m_sock);
27}
28
29EventServer::~EventServer()
30{
31 delete m_rbuf;
32 delete m_sock;
33 delete m_man;
34}
35
36int EventServer::server()
37{
38 SocketIO sio;
39 MsgHandler msghdl(0);
40 char* evtbuffer = new char[MAXEVTSIZE];
41
42 // vector<int> recvsock;
43 int loop_counter [[maybe_unused]] = 0;
44 while (m_force_exit == 0) {
45 // Pick up a event from RingBuffer (non blocking)
46 int size = m_rbuf->remq((int*)evtbuffer);
47 // if ( size > 0 )
48 // printf ( "From RB : event size = %d\n", size );
49
50 // Check connection request
51 int exam_stat = m_man->examine();
52 // printf("Examine: exam_stat = %d\n", exam_stat);
53 if (exam_stat == 0) {
54 printf("Initial connection request detected!\n");
55 // int fd = recvsock[;
56 } else if (exam_stat == 1 && size > 0) { //
57 // printf ( "Event data data ready on socket\n" );
58 vector<int>& recvsock = m_man->connected_socket_list();
59 // printf ( "size of recvsock = %d\n", recvsock.size() );
60 for (vector<int>::iterator it = recvsock.begin();
61 it != recvsock.end(); ++it) {
62 int fd = *it;
63 if (m_man->connected(fd, true)) {
64 int is = sio.put(fd, evtbuffer, size * 4);
65 // printf ( "data put on socket : %d\n", is );
66 if (is <= 0) {
67 printf("EventServer: fd %d disconnected\n", fd);
68 m_man->remove(fd);
69 break;
70 }
71 }
72 }
73 }
74 usleep(1000);
75 loop_counter++;
76 }
77 return 0;
78}
79
A class to encode/decode an EvtMessage.
Definition: MsgHandler.h:103
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
int remq(int *buf)
Pick up a buffer from the RingBuffer.
Definition: RingBuffer.cc:308
Abstract base class for different kinds of events.
STL namespace.