Belle II Software  release-08-01-10
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 
15 using namespace Belle2;
16 using namespace std;
17 
18 static EventServerCallback* s_eventserver = NULL;
19 
20 //-----------------------------------------------------------------
21 // Rbuf-Read Thread Interface
22 //-----------------------------------------------------------------
23 void* RunEventServerLogger(void*)
24 {
25  s_eventserver->EventServerLogger();
26  return NULL;
27 }
28 
29 
30 
31 
32 EventServerCallback::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 
42 EventServerCallback::~EventServerCallback() noexcept
43 {
44 
45 }
46 
47 void EventServerCallback::load(const DBObject&)
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 
83 void EventServerCallback::start()
84 {
85 }
86 
87 void EventServerCallback::stop()
88 {
89 }
90 
91 void EventServerCallback::abort()
92 {
93  // Kill processes
94  int status;
95  if (m_pid_server != 0) {
96  kill(m_pid_server, SIGINT);
97  waitpid(m_pid_server, &status, 0);
98  LogFile::info("killed event server (pid=%d)", m_pid_server);
99  }
100 
101  pthread_cancel(m_logthread);
102 
103 }
104 
105 void EventServerCallback::recover(const DBObject&)
106 {
107 
108 }
109 
110 void EventServerCallback::EventServerLogger()
111 {
112  while (true) {
113  int st = m_proc->CheckOutput();
114  if (st < 0) {
115  perror("EventServerLogger::server");
116  // exit ( -1 );
117  } else if (st > 0) {
118  m_log->ProcessLog(m_proc->GetFd());
119  }
120  }
121 }
122 
123 
124 
Abstract base class for different kinds of events.