EUDAQ
 All Classes Namespaces Files Functions Variables Pages
TransportBase.hh
Go to the documentation of this file.
1 #ifndef EUDAQ_INCLUDED_TransportBase
2 #define EUDAQ_INCLUDED_TransportBase
3 
7 #include "eudaq/Platform.hh"
8 #include "eudaq/Mutex.hh"
9 #include "eudaq/Exception.hh"
10 #include "eudaq/BufferSerializer.hh"
11 #include <string>
12 #include <queue>
13 #include <iosfwd>
14 #include <cstring>
15 #include <iostream>
16 
17 namespace eudaq {
18 
22  class DLLEXPORT ConnectionInfo {
23  public:
24  explicit ConnectionInfo(const std::string &name = "")
25  : m_state(0), m_name(name) { }
26 
27  ConnectionInfo(const std::string &name, const std::string type)
28  : m_state(0), m_name(name), m_type(type) { }
29 
30 
31  virtual ~ConnectionInfo() {}
32  virtual void Print(std::ostream &) const;
33  virtual bool Matches(const ConnectionInfo &other) const;
34  bool IsEnabled() const { return m_state >= 0; }
35  int GetState() const { return m_state; }
36  void SetState(int state) { m_state = state; }
37  std::string GetType() const { return m_type; }
38  void SetType(const std::string &type) { m_type = type; }
39  std::string GetName() const { return m_name; }
40  void SetName(const std::string &name) { m_name = name; }
41 
42  int GetRemoteInfo() const;
43  virtual std::string GetRemote() const { return m_host; }
44 
45  static const ConnectionInfo ALL;
46 
47  virtual ConnectionInfo *Clone() const { return new ConnectionInfo(*this); }
48 
49  bool operator < (const ConnectionInfo comp) const
50  {
51  return(GetRemoteInfo() < comp.GetRemoteInfo());
52  /*
53  std::string comp_host = comp.GetRemote();
54  return (
55  std::stoi(m_host.substr(m_host.find(":"))) <
56  std::stoi(comp_host.substr(comp_host.find(":")))
57  ); */
58  }
59 
60  protected:
61  int m_state;
62  std::string m_type, m_name;
63  std::string m_host;
64  /*
65 
66  public:
67  virtual bool operator = (const ClientID & other) = 0;
68  virtual std::string Name() const = 0;
69  */
70  };
71 
72  inline std::ostream &operator<<(std::ostream &os, const ConnectionInfo &id) {
73  id.Print(os);
74  return os;
75  }
76 
81  public:
82  enum EventType { CONNECT, DISCONNECT, RECEIVE };
83  TransportEvent(EventType et, ConnectionInfo &i, const std::string &p = "")
84  : etype(et), id(i), packet(p) {}
85  EventType etype;
87  std::string packet;
88  };
89 
101  class Helper {
102  public:
103  virtual void call(TransportEvent &) = 0;
104  virtual ~Helper() {}
105  virtual Helper *Clone() const = 0;
106  };
110  class HelperFunction : public Helper {
111  public:
112  typedef void (*FuncType)(TransportEvent &);
113  HelperFunction(FuncType func) : m_func(func) {}
114  virtual void call(TransportEvent &ev) { m_func(ev); }
115  virtual Helper *Clone() const { return new HelperFunction(*this); }
116 
117  private:
118  FuncType m_func;
119  };
125  template <typename T> class HelperMember : public Helper {
126  public:
127  typedef T ObjType;
128  typedef void (ObjType::*FuncType)(TransportEvent &);
129  HelperMember(ObjType *obj, FuncType func) : m_obj(obj), m_func(func) {}
130  virtual void call(TransportEvent &ev) { (m_obj->*m_func)(ev); }
131  virtual Helper *Clone() const { return new HelperMember(*this); }
132 
133  private:
134  ObjType *m_obj;
135  FuncType m_func;
136  };
137 
138  public:
139  TransportCallback() : m_helper(0) {}
140  TransportCallback(HelperFunction::FuncType funcptr)
141  : m_helper(new HelperFunction(funcptr)) {}
142  template <typename T>
143  TransportCallback(T *obj, typename HelperMember<T>::FuncType func)
144  : m_helper(new HelperMember<T>(obj, func)) {}
145  void operator()(TransportEvent &ev) {
146  if (m_helper)
147  m_helper->call(ev);
148  }
150  : m_helper(other.m_helper->Clone()) {}
151  TransportCallback &operator=(const TransportCallback &other) {
152  delete m_helper;
153  m_helper = other.m_helper->Clone();
154  return *this;
155  }
156  ~TransportCallback() { delete m_helper; }
157 
158  private:
159  Helper *m_helper;
160  };
161 
169  public:
170  TransportBase();
171  virtual ~TransportBase();
172 
178  virtual void SendPacket(const unsigned char *data, size_t len,
179  const ConnectionInfo & = ConnectionInfo::ALL,
180  bool duringconnect = false) = 0;
181  void SendPacket(const std::string &data,
182  const ConnectionInfo &inf = ConnectionInfo::ALL,
183  bool duringconnect = false) {
184  // std::cout << "SendPacket (string)" << std::endl;
185  SendPacket(reinterpret_cast<const unsigned char *>(&data[0]),
186  data.length(), inf, duringconnect);
187  }
188  void SendPacket(const char *str,
189  const ConnectionInfo &inf = ConnectionInfo::ALL,
190  bool duringconnect = false) {
191  // std::cout << "SendPacket (char*)" << std::endl;
192  SendPacket(reinterpret_cast<const unsigned char *>(str), std::strlen(str),
193  inf, duringconnect);
194  }
195  void SendPacket(const BufferSerializer &t,
196  const ConnectionInfo &inf = ConnectionInfo::ALL,
197  bool duringconnect = false) {
198  SendPacket(&t[0], t.size(), inf, duringconnect);
199  }
200 
206  virtual void Close(const ConnectionInfo &) {}
207 
215  virtual void ProcessEvents(int timeout) = 0;
216 
217  void Process(int timeout = -1);
218  bool ReceivePacket(std::string *packet, int timeout = -1,
219  const ConnectionInfo & = ConnectionInfo::ALL);
220  bool SendReceivePacket(const std::string &sendpacket,
221  std::string *recpacket,
222  const ConnectionInfo &connection, int timeout = -1);
223  template <typename T>
224  T SendReceivePacket(const std::string &packet,
225  const ConnectionInfo &connection, int timeout = -1);
226  void SetCallback(const TransportCallback &);
227  virtual bool IsNull() const { return false; }
228 
229  protected:
230  std::queue<TransportEvent>
234  Mutex m_mutex;
235  };
236 
237  template <typename T>
238  inline T TransportBase::SendReceivePacket(const std::string &packet,
239  const ConnectionInfo &connection,
240  int timeout) {
241  std::string buf;
242  bool ok = SendReceivePacket(packet, &buf, connection, timeout);
243  if (!ok)
244  EUDAQ_THROW("Timeout: No response in SendReceivePacket");
245  BufferSerializer ser(buf.begin(), buf.end());
246  T result;
247  ser.read(result);
248  return result;
249  }
250 }
251 
252 #endif // EUDAQ_INCLUDED_TransportBase
Definition: TransportBase.hh:80
virtual void SendPacket(const unsigned char *data, size_t len, const ConnectionInfo &=ConnectionInfo::ALL, bool duringconnect=false)=0
std::queue< TransportEvent > m_events
A buffer to queue up events until they are handled.
Definition: TransportBase.hh:231
Definition: Mutex.hh:8
std::string packet
The packet of data in case of a RECEIVE event.
Definition: TransportBase.hh:87
virtual void Close(const ConnectionInfo &)
Definition: TransportBase.hh:206
virtual void ProcessEvents(int timeout)=0
ConnectionInfo & id
The id of the connection.
Definition: TransportBase.hh:86
TransportCallback m_callback
The callback function to invoke on a transport event.
Definition: TransportBase.hh:233
Definition: TransportBase.hh:168
EventType etype
The type of event.
Definition: TransportBase.hh:85
Definition: TransportBase.hh:97
Definition: TransportBase.hh:22
Definition: BufferSerializer.hh:12