Belle II Software  release-08-01-10
rfcommand.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 <stdio.h>
10 #include <unistd.h>
11 
12 #include <string>
13 
14 extern "C" {
15 #include <nsm2/nsm2.h>
16 #include <nsm2/belle2nsm.h>
17 }
18 
19 #include "daq/rfarm/manager/RFConf.h"
20 
21 using namespace std;
22 using namespace Belle2;
23 
24 static int done_flag;
25 
26 void ok_handler(NSMmsg* /*msg*/, NSMcontext*)
27 {
28  done_flag = 1;
29  // printf ( "OK received\n" );
30 }
31 
32 void error_handler(NSMmsg* /*msg*/, NSMcontext*)
33 {
34  done_flag = -1;
35  printf("ERROR received\n");
36 }
37 
38 int main(int argc, char** argv)
39 {
40  // Usage : getconf conffile node
41 
42  if (argc < 4) {
43  printf("Usage : rfcommand nodename command\n");
44  exit(-1);
45  }
46 
47  char* conffile = argv[1];
48  char* nodename = argv[2];
49  char* command = argv[3];
50 
51  char* confdir = getenv("RFARM_CONFDIR");
52  string confpath = string(confdir) + "/" + string(conffile) + ".conf";
53 
54  RFConf conf(confpath.c_str());
55  int port = conf.getconfi("system", "nsmport");
56 
57  // NSM initialization
58  // if (! b2nsm_init("COMMANDER")) {
59  if (! b2nsm_init2("COMMANDER", 1, 0, port, 0)) {
60  printf("rfcommand : initialization error %s\n", b2nsm_strerror());
61  return -1;
62  }
63  // printf ( "initialization done\n" );
64 
65  // handlers
66  if (b2nsm_callback("OK", ok_handler) < 0) {
67  printf("rfcommand : error to hook CALLBACK(OK) %s", b2nsm_strerror());
68  return -1;
69  }
70  if (b2nsm_callback("ERROR", ok_handler) < 0) {
71  printf("rfcommand : error to hook CALLBACK(ERROR) %s", b2nsm_strerror());
72  return -1;
73  }
74 
75 
76  // Send NSM request
77  int* pars = nullptr;
78  done_flag = 0;
79  b2nsm_sendreq(nodename, command, 0, pars);
80  while (done_flag == 0) usleep(1000);
81  return done_flag;
82 }
83 
Abstract base class for different kinds of events.
Definition: nsm2.h:224
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91