Belle II Software development
RFNSM.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/rfarm/manager/RFNSM.h"
10
11extern "C" {
12#include <nsm2/nsm2.h>
13#include <nsm2/nsmlib2.h>
14#include <nsm2/belle2nsm.h>
15}
16
17#include <iostream>
18
19#define RFNSMOUT stdout
20
21using namespace std;
22using namespace Belle2;
23
24//extern "C" void nsmlib_debuglevel(int);
25
26// Global variable to contain server instance
27RFServerBase* g_nsmserver = 0;
28
29RFNSM_Status* RFNSM_Status::s_instance = 0;
30
31NSMcontext* RFNSM::g_context = 0;
32
33// Constructor / Destructor
34RFNSM::RFNSM(char* nodename, RFServerBase* server)
35{
36
37 // Set debug level
38 // nsmlib_debuglevel ( 1 );
39
40 // Initialize NSM
41 if (!(g_context = b2nsm_init2(nodename, 0, 0, 0, 0))) {
42 //if (!(g_context = b2nsm_init(nodename))) {
43 fprintf(RFNSMOUT, "RFNSM : %s initialization failure, %s\n",
44 nodename, b2nsm_strerror());
45 g_context = 0;
46 }
47 nsmlib_usesig(g_context, 0);
48 m_nodename = nodename;
49
50 // Redirect log message to standard output
51 b2nsm_logging(RFNSMOUT);
52
53 // Set debug
54 nsmlib_debuglevel(0);
55
56 // Register server
57 g_nsmserver = server;
58
59 // Hook server functions to NSM
60 // if (b2nsm_callback("RF_CONFIGURE", m_Configure) < 0) {
61 if (b2nsm_callback("RC_LOAD", m_Configure) < 0) {
62 fprintf(stderr, "RFNSM : %s hooking CONFIGURE failed, %s\n",
63 m_nodename.c_str(), b2nsm_strerror());
64 }
65 // if (b2nsm_callback("RF_UNCONFIGURE", m_UnConfigure) < 0) {
66 if (b2nsm_callback("RC_ABORT", m_UnConfigure) < 0) {
67 fprintf(stderr, "RFNSM : %s hooking UNCONFIGURE failed, %s\n",
68 m_nodename.c_str(), b2nsm_strerror());
69 }
70 // if (b2nsm_callback("RF_START", m_Start) < 0) {
71 if (b2nsm_callback("RC_START", m_Start) < 0) {
72 fprintf(stderr, "RFNSM : %s hooking START failed, %s\n",
73 m_nodename.c_str(), b2nsm_strerror());
74 }
75 // if (b2nsm_callback("RF_STOP", m_Stop) < 0) {
76 if (b2nsm_callback("RC_STOP", m_Stop) < 0) {
77 fprintf(stderr, "RFNSM : %s hooking STOP failed, %s\n",
78 m_nodename.c_str(), b2nsm_strerror());
79 }
80 // if (b2nsm_callback("RF_PAUSE", m_Pause) < 0) {
81 if (b2nsm_callback("RC_PAUSE", m_Pause) < 0) {
82 fprintf(stderr, "RFNSM : %s hooking PAUSE failed, %s\n",
83 m_nodename.c_str(), b2nsm_strerror());
84 }
85 // if (b2nsm_callback("RF_RESUME", m_Resume) < 0) {
86 if (b2nsm_callback("RC_RESUME", m_Resume) < 0) {
87 fprintf(stderr, "RFNSM : %s hooking RESUME failed, %s\n",
88 m_nodename.c_str(), b2nsm_strerror());
89 }
90 // if (b2nsm_callback("RF_RESTART", m_Restart) < 0) {
91 if (b2nsm_callback("RC_RECOVER", m_Restart) < 0) {
92 fprintf(stderr, "RFNSM : %s hooking RESTART failed, %s\n",
93 m_nodename.c_str(), b2nsm_strerror());
94 }
95
96 // Status function
97 if (b2nsm_callback("RC_STATUS", m_Status) < 0) {
98 fprintf(stderr, "RFNSM : %s hooking STATUS failed, %s\n",
99 m_nodename.c_str(), b2nsm_strerror());
100 }
101
102 // Hook communication functions
103 if (b2nsm_callback("OK", m_OK) < 0) {
104 fprintf(stderr, "RFNSM : %s hooking OK failed, %s\n",
105 m_nodename.c_str(), b2nsm_strerror());
106 }
107 if (b2nsm_callback("ERROR", m_ERROR) < 0) {
108 fprintf(stderr, "RFNSM : %s hooking ERROR failed, %s\n",
109 m_nodename.c_str(), b2nsm_strerror());
110 }
111
112 // Node status = Unconfigured
113 RFNSM_Status::Instance().set_state(RFSTATE_UNCONFIGURED);
114}
115
116RFNSM::~RFNSM()
117{
118}
119
120
121void RFNSM::AllocMem(char* format)
122{
123 printf("AllocMem : format file = %s\n", format);
124 // Allocate shared memory
125 m_info = (RfNodeInfo*)b2nsm_allocmem(m_nodename.c_str(), format,
126 1, 3);
127 if (!m_info) {
128 fprintf(RFNSMOUT, "RFNSM : %s allocmem failure, %s\n",
129 m_nodename.c_str(), b2nsm_strerror());
130 }
131
132 m_formatfile = string(format);
133
134}
135
136// Wrapper functions to be hooked to NSM
137void RFNSM::m_Configure(NSMmsg* msg, NSMcontext* ctx)
138{
139 fflush(stdout);
140 int nsmstate = RFNSM_Status::Instance().get_state();
141 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
142 int stat = g_nsmserver->Configure(msg, ctx);
143 fflush(stdout);
144 if (stat == 0) {
145 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
146 b2nsm_ok(msg, "Configured", NULL);
147 } else {
148 RFNSM_Status::Instance().set_state(nsmstate);
149 b2nsm_error(msg, NULL);
150 }
151}
152
153void RFNSM::m_UnConfigure(NSMmsg* msg, NSMcontext* ctx)
154{
155 fflush(stdout);
156 int nsmstate = RFNSM_Status::Instance().get_state();
157 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
158 int stat = g_nsmserver->UnConfigure(msg, ctx);
159 fflush(stdout);
160 if (stat == 0) {
161 RFNSM_Status::Instance().set_state(RFSTATE_UNCONFIGURED);
162 b2nsm_ok(msg, "Unconfigured", NULL);
163 } else {
164 RFNSM_Status::Instance().set_state(nsmstate);
165 b2nsm_error(msg, NULL);
166 }
167}
168
169void RFNSM::m_Start(NSMmsg* msg, NSMcontext* ctx)
170{
171 int nsmstate = RFNSM_Status::Instance().get_state();
172 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
173 int stat = g_nsmserver->Start(msg, ctx);
174 if (stat == 0) {
175 RFNSM_Status::Instance().set_state(RFSTATE_RUNNING);
176 b2nsm_ok(msg, "Running", NULL);
177 } else {
178 RFNSM_Status::Instance().set_state(nsmstate);
179 b2nsm_error(msg, NULL);
180 }
181}
182
183void RFNSM::m_Stop(NSMmsg* msg, NSMcontext* ctx)
184{
185 int nsmstate = RFNSM_Status::Instance().get_state();
186 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
187 int stat = g_nsmserver->Stop(msg, ctx);
188 if (stat == 0) {
189 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
190 b2nsm_ok(msg, "Stopped", NULL);
191 } else {
192 RFNSM_Status::Instance().set_state(nsmstate);
193 b2nsm_error(msg, NULL);
194 }
195}
196
197void RFNSM::m_Pause(NSMmsg* msg, NSMcontext* ctx)
198{
199 int nsmstate = RFNSM_Status::Instance().get_state();
200 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
201 int stat = g_nsmserver->Pause(msg, ctx);
202 if (stat == 0) {
203 RFNSM_Status::Instance().set_state(RFSTATE_IDLE);
204 b2nsm_ok(msg, "Idle", NULL);
205 } else {
206 RFNSM_Status::Instance().set_state(nsmstate);
207 b2nsm_error(msg, NULL);
208 }
209}
210
211void RFNSM::m_Resume(NSMmsg* msg, NSMcontext* ctx)
212{
213 int nsmstate = RFNSM_Status::Instance().get_state();
214 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
215 int stat = g_nsmserver->Resume(msg, ctx);
216 if (stat == 0) {
217 RFNSM_Status::Instance().set_state(RFSTATE_RUNNING);
218 b2nsm_ok(msg, "Running", NULL);
219 } else {
220 RFNSM_Status::Instance().set_state(nsmstate);
221 b2nsm_error(msg, NULL);
222 }
223}
224
225void RFNSM::m_Restart(NSMmsg* msg, NSMcontext* ctx)
226{
227 int nsmstate = RFNSM_Status::Instance().get_state();
228 RFNSM_Status::Instance().set_state(RFSTATE_TRANSITION);
229 int stat = g_nsmserver->Restart(msg, ctx);
230 if (stat == 0) {
231 RFNSM_Status::Instance().set_state(RFSTATE_CONFIGURED);
232 b2nsm_ok(msg, "Configured", NULL);
233 } else {
234 RFNSM_Status::Instance().set_state(nsmstate);
235 b2nsm_error(msg, NULL);
236 }
237}
238
239void RFNSM::m_Status(NSMmsg* msg, NSMcontext* /*ctx*/)
240{
241 /* Old imp
242 int stat = g_nsmserver->Status(msg, ctx);
243 if (stat == 0)
244 b2nsm_ok(msg, "Status", NULL);
245 else
246 b2nsm_error(msg, NULL);
247 */
248 int curstate = RFNSM_Status::Instance().get_state();
249 b2nsm_ok(msg, RFSTATE[curstate].c_str(), NULL);
250
251}
252
253// Function to handle execution status
254void RFNSM::m_OK(NSMmsg* /*msg*/, NSMcontext* /*ctx*/)
255{
256 RFNSM_Status& rfs = RFNSM_Status::Instance();
257 int flag = rfs.get_flag();
258 flag++;
259 rfs.set_flag(flag);
260 // printf ( "OK received. flag set to %d\n", flag );
261}
262
263void RFNSM::m_ERROR(NSMmsg* /*msg*/, NSMcontext* /*ctx*/)
264{
265 RFNSM_Status::Instance().set_flag(-1);
266 // printf ( "ERROR received. m_flag set to -1\n" );
267}
268
269
270// Interface to Node Info
271
272RfNodeInfo* RFNSM::GetNodeInfo()
273{
274 return m_info;
275}
276
277// RFNSM_Status implementation
278
279RFNSM_Status::RFNSM_Status()
280{
281}
282
283RFNSM_Status::~RFNSM_Status()
284{
285}
286
287RFNSM_Status& RFNSM_Status::Instance()
288{
289 if (!s_instance) {
290 s_instance = new RFNSM_Status;
291 }
292 return *s_instance;
293}
294
295void RFNSM_Status::set_flag(int val)
296{
297 m_flag = val;
298}
299
300int RFNSM_Status::get_flag()
301{
302 return m_flag;
303}
304
305void RFNSM_Status::set_state(int val)
306{
307 m_state = val;
308}
309
310int RFNSM_Status::get_state()
311{
312 return m_state;
313}
Abstract base class for different kinds of events.
STL namespace.
Definition: nsm2.h:224