Belle II Software development
EventServerCallback.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/expreco/EventServerCallback.h"
9
10#include <unistd.h>
11
12#include <sys/stat.h>
13#include <sys/wait.h>
14
15using namespace Belle2;
16using namespace std;
17
18static EventServerCallback* s_eventserver = NULL;
19
20//-----------------------------------------------------------------
21// Rbuf-Read Thread Interface
22//-----------------------------------------------------------------
23void* RunEventServerLogger(void*)
24{
25 s_eventserver->EventServerLogger();
26 return NULL;
27}
28
29
30
31
32EventServerCallback::EventServerCallback()
33{
34 // Conf file
35
36 // 0. Initialize configuration manager
37 m_conf = new RFConf(getenv("ERECO_CONFFILE"));
38
39 s_eventserver = this;
40}
41
42EventServerCallback::~EventServerCallback() noexcept
43{
44
45}
46
47void EventServerCallback::load(const DBObject&, const std::string&)
48{
49
50 // 1. Set execution directory
51 char* nodename = m_conf->getconf("eventsampler", "server", "nodename");
52 string execdir = string(m_conf->getconf("system", "execdir_base")) + "/eventserver";
53 printf("execdir = %s\n", execdir.c_str());
54
55 mkdir(execdir.c_str(), 0755);
56 chdir(execdir.c_str());
57
58 // 2. Initialize process manager
59 m_proc = new RFProcessManager(nodename);
60
61 // 3. Initialize log manager
62 m_log = new RFLogManager(nodename);
63
64
65 // 4. EventServer
66 char* server = m_conf->getconf("eventsampler", "server", "script");
67 char* rbuf = m_conf->getconf("eventsampler", "ringbufout");
68 char* port = m_conf->getconf("eventsampler", "server", "port");
69 m_pid_server = m_proc->Execute(server, rbuf, port);
70
71 sleep(2);
72
73 // 6. Start Logger
74 pthread_attr_t thread_attr;
75 pthread_attr_init(&thread_attr);
76 // pthread_attr_setschedpolicy(&thread_attr , SCHED_FIFO);
77 // pthread_attr_setdetachstate(&thread_attr , PTHREAD_CREATE_DETACHED);
78 // pthread_t thr_input;
79 pthread_create(&m_logthread, NULL, RunEventServerLogger, NULL);
80
81}
82
83void EventServerCallback::abort()
84{
85 // Kill processes
86 int status;
87 if (m_pid_server != 0) {
88 kill(m_pid_server, SIGINT);
89 waitpid(m_pid_server, &status, 0);
90 LogFile::info("killed event server (pid=%d)", m_pid_server);
91 }
92
93 pthread_cancel(m_logthread);
94
95}
96
97void EventServerCallback::EventServerLogger()
98{
99 while (true) {
100 int st = m_proc->CheckOutput();
101 if (st < 0) {
102 perror("EventServerLogger::server");
103 // exit ( -1 );
104 } else if (st > 0) {
105 m_log->ProcessLog(m_proc->GetFd());
106 }
107 }
108}
109
110
111
int Execute(char *script, int nargs, char **args)
Abstract base class for different kinds of events.
STL namespace.