Belle II Software  release-08-01-10
NSMNodeDaemon.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/slc/nsm/NSMNodeDaemon.h"
9 
10 #include "daq/slc/nsm/NSMCommunicator.h"
11 #include "daq/slc/base/TimeoutException.h"
12 
13 #include <daq/slc/system/LogFile.h>
14 #include <daq/slc/system/Time.h>
15 
16 #include <daq/slc/base/StringUtil.h>
17 #include <daq/slc/base/ConfigFile.h>
18 
19 #include <cstdlib>
20 #include <cstdio>
21 #include <fstream>
22 
23 using namespace Belle2;
24 
25 void NSMNodeDaemon::add(NSMCallback* callback,
26  const std::string& host, int port)
27 {
28  try {
29  if (callback != NULL && host.size() > 0 && port > 0) {
30  NSMCommunicator* com = new NSMCommunicator();
31  NSMNode& node(callback->getNode());
32  com->init(node, host, port);
33  com->setCallback(callback);
34  callback->init(*com);
35  callback->alloc_open(*com);
36  m_timeout = callback->getTimeout();
37  if (callback->getLogNode().getName().size() == 0) {
38  ConfigFile file("slowcontrol");
39  callback->setLogNode(NSMNode(file.get("log.collector")));
40  }
41  m_callback.push_back(callback);
42  }
43  } catch (const std::exception& e) {
44  LogFile::fatal(e.what());
45  exit(1);
46  }
47 }
48 
49 void NSMNodeDaemon::run()
50 {
51  NSMCommunicatorList& com_v(NSMCommunicator::get());
52  try {
53  {
54  std::string nodename = com_v[0]->getCallback().getNode().getName();
55  std::string filename = ("/tmp/nsmvget." + StringUtil::tolower(nodename));
56  std::ifstream fin(filename.c_str());
57  std::vector<std::string> nodes, vnames;
58  std::string node, vname;
59  while (fin >> node >> vname) {
60  nodes.push_back(node);
61  vnames.push_back(vname);
62  }
63  ::remove(filename.c_str());
64  for (size_t i = 0; i < com_v.size(); i++) {
65  NSMCommunicator& com(*com_v[i]);
66  for (size_t j = 0; j < nodes.size(); j++) {
67  com.getCallback().vget(nodes[j], vnames[j]);
68  }
69  }
70  }
71  double t0 = Time().get();
72  while (true) {
73  try {
74  NSMCommunicator& com(NSMCommunicator::select(m_timeout));
75  com.getCallback().perform(com);
76  } catch (const TimeoutException& e) {}
77  for (size_t i = 0; i < com_v.size(); i++) {
78  NSMCommunicator& com(*com_v[i]);
79  while (com.hasQueue()) {
80  com.setMessage(com.popQueue());
81  com.getCallback().perform(com);
82  }
83  }
84  double t = Time().get();
85  if (t - t0 >= m_timeout) {
86  for (size_t i = 0; i < com_v.size(); i++) {
87  NSMCommunicator& com(*com_v[i]);
88  com.getCallback().timeout(com);
89  com.getCallback().alloc_open(com);
90  }
91  t0 = t;
92  }
93  }
94  } catch (const std::exception& e) {
95  LogFile::fatal("NSM node brdge : Caught exception %s\n"
96  "Terminate process...", e.what());
97  }
98  for (size_t i = 0; i < com_v.size(); i++) {
99  NSMCommunicator& com(*com_v[i]);
100  com.getCallback().term();
101  }
102 }
103 
Abstract base class for different kinds of events.