Belle II Software  release-08-01-10
NSMCommunicator.h
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 #ifndef _Belle2_NSMCommunicator_hh
9 #define _Belle2_NSMCommunicator_hh
10 
11 #include "daq/slc/nsm/NSMMessage.h"
12 #include "daq/slc/nsm/NSMNode.h"
13 
14 #include <daq/slc/system/Mutex.h>
15 
16 extern "C" {
17 #include <nsm2/nsm2.h>
18 }
19 
20 #include <queue>
21 #include <vector>
22 
23 namespace Belle2 {
29  class NSMCallback;
30  class NSMMessage;
31 
33 
34  typedef std::vector<NSMCommunicator*> NSMCommunicatorList;
35 
36  friend class NSMData;
37 
38  public:
39  static NSMCommunicator& select(double sec);
40  static NSMCommunicator& connected(const std::string& node);
41  static NSMCommunicatorList& get() { return g_comm; }
42  static bool send(const NSMMessage& msg);
43 
44  private:
45  static NSMCommunicatorList g_comm;
46  static Mutex g_mutex;
47  static Mutex g_mutex_select;
48 
49  public:
50  NSMCommunicator(const std::string& host = "", int port = -1);
51  explicit NSMCommunicator(NSMcontext* nsmc);
52  ~NSMCommunicator() {}
53 
54  public:
55  void init(const NSMNode& node, const std::string& host, int port)
56  ;
57  void setCallback(NSMCallback* callback);
58  // 20191004 nakao
59  // handling nsmlib_recv inside a user program is against the usage of NSM2
60  void callContext();
61 
62  public:
63  int getId() const { return m_id; }
64  void setId(int id) { m_id = id; }
65  NSMMessage& getMessage() { return m_message; }
66  const NSMMessage& getMessage() const { return m_message; }
67  void setMessage(const NSMMessage& msg);
68  const NSMNode& getNode() const { return m_node; }
69  int getNodeIdByName(const std::string& name);
70  int getNodePidByName(const std::string& name);
71  // 20191004 nakao
72  // making hostname visible to application is against the philosophy of NSM2
73  //const std::string getNodeHost(const std::string& nodename);
74  //const std::string getNodeHost();
75  const std::string& getHostName() { return m_host; }
76  int getPort() { return m_port; }
77  NSMCallback& getCallback();
78  bool isConnected(const std::string& node);
79  const std::string getNodeNameById(int id);
80  void pushQueue(const NSMMessage& msg) { m_msg_q.push(msg); }
81  bool hasQueue() const { return !m_msg_q.empty(); }
82  NSMMessage popQueue();
83 
84  private:
85  NSMcontext* getContext() { return m_nsmc; }
86 
87  private:
88  bool sendRequest(const NSMMessage& msg);
89 
90  private:
91  int m_id;
92  NSMcontext* m_nsmc;
93  NSMCallback* m_callback;
94  NSMMessage m_message;
95  std::string m_host;
96  int m_port;
97  NSMNode m_node;
98  std::queue<NSMMessage> m_msg_q;
99 
100  };
101 
102  typedef std::vector<NSMCommunicator*> NSMCommunicatorList;
103 
105 };
106 
107 #endif
Abstract base class for different kinds of events.