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