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