EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
SocketData.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_SOCKETDATA_HPP_
2 #define ORG_EEROS_CONTROL_SOCKETDATA_HPP_
3 
5 #include <eeros/math/Matrix.hpp>
6 #include <array>
7 #include <eeros/core/System.hpp>
10 
11 namespace eeros {
12  namespace control {
13 
14  template < typename SigInType, typename SigOutType, typename Enable = void >
15  class SocketData: public Block1i1o<SigInType, SigOutType> { };
16 
17  template < typename SigInType, typename SigOutType>
18  class SocketData<SigInType, SigOutType,
19  typename std::enable_if<std::is_compound<SigInType>::value && std::is_compound<SigOutType>::value>::type>
20  : public Block1i1o<SigInType, SigOutType> {
21  public:
22  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
23  bufInLen = sizeof(SigInType) / sizeof(SigInValueType);
24  bufOutLen = sizeof(SigOutType) / sizeof(SigOutValueType);
25  isServer = serverIP.empty();
26  if (isServer)
27  server = new eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(port, period, timeout);
28  else
29  client = new eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(serverIP, port, period, timeout);;
30  }
31 
32  ~SocketData() {if (isServer) server->stop(); else client->stop();}
33 
34  virtual void run() {
35  // receive
36  SigOutType output;
37  if (isServer) {
38  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = server->getReceiveBuffer();
39  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
40  } else {
41  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = client->getReceiveBuffer();
42  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
43  }
44  // send
45  if (this->in.isConnected()) {
46  for(int i = 0; i < bufInLen; i++) sendData[i] = this->in.getSignal().getValue()(i);
47  if (isServer) server->setSendBuffer(sendData);
48  else client->setSendBuffer(sendData);
49  }
50 
51  this->out.getSignal().setValue(output);
53  this->out.getSignal().setTimestamp(time);
54  }
55 
56  virtual bool isNew() {
57  if (isServer) return server->newData; else return client->newData;
58  }
59 
60  virtual void resetNew() {
61  if (isServer) server->newData = false; else client->newData = false;
62  }
63 
64  template <typename X, typename Y>
65  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
66 
67  protected:
68  typedef typename SigInType::value_type SigInValueType;
69  typedef typename SigOutType::value_type SigOutValueType;
70  eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* server;
71  eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* client;
72  std::array<SigInValueType, sizeof(SigInType) / sizeof(SigInValueType)> sendData;
73  uint32_t bufInLen, bufOutLen;
74  bool isServer;
75  };
76 
77  template < typename SigInType, typename SigOutType >
78  class SocketData<SigInType, SigOutType,
79  typename std::enable_if<std::is_arithmetic<SigInType>::value && std::is_compound<SigOutType>::value>::type>
80  : public Block1i1o<SigInType, SigOutType> {
81  public:
82  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
83  bufInLen = 1;
84  bufOutLen = sizeof(SigOutType) / sizeof(SigOutValueType);
85  isServer = serverIP.empty();
86  if (isServer)
87  server = new eeros::sockets::SocketServer<1, SigInType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(port, period, timeout);
88  else
89  client = new eeros::sockets::SocketClient<1, SigInType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(serverIP, port, period, timeout);
90  }
91 
92  ~SocketData() {if (isServer) server->stop(); else client->stop();}
93 
94  virtual void run() {
95  // receive
96  SigOutType output;
97  if (isServer) {
98  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = server->getReceiveBuffer();
99  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
100  } else {
101  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = client->getReceiveBuffer();
102  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
103  }
104 
105  // send
106  if (this->in.isConnected()) {
107  sendData[0] = this->in.getSignal().getValue();
108  if (isServer) server->setSendBuffer(sendData);
109  else client->setSendBuffer(sendData);
110  }
111 
112  this->out.getSignal().setValue(output);
114  this->out.getSignal().setTimestamp(time);
115  }
116 
117  virtual bool isNew() {
118  if (isServer) return server->newData; else return client->newData;
119  }
120 
121  virtual void resetNew() {
122  if (isServer) server->newData = false; else client->newData = false;
123  }
124 
125  template <typename X, typename Y>
126  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
127 
128  protected:
129  typedef typename SigOutType::value_type SigOutValueType;
130  eeros::sockets::SocketServer<1, SigInType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* server;
131  eeros::sockets::SocketClient<1, SigInType, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* client;
132  std::array<SigInType, 1> sendData;;
133  uint32_t bufInLen, bufOutLen;
134  bool isServer;
135  };
136 
137  template < typename SigInType, typename SigOutType >
138  class SocketData<SigInType, SigOutType,
139  typename std::enable_if<std::is_compound<SigInType>::value && std::is_arithmetic<SigOutType>::value>::type>
140  : public Block1i1o<SigInType, SigOutType> {
141  public:
142  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
143  bufInLen = sizeof(SigInType) / sizeof(SigInValueType);
144  bufOutLen = 1;
145  isServer = serverIP.empty();
146  if (isServer)
147  server = new eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 1, SigOutType>(port, period, timeout);
148  else
149  client = new eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 1, SigOutType>(serverIP, port, period, timeout);
150  }
151 
152  ~SocketData() {if (isServer) server->stop(); else client->stop();}
153 
154  virtual void run() {
155  // receive
156  SigOutType output;
157  if (isServer) {
158  std::array<SigOutType, 1>& getData = server->getReceiveBuffer();
159  output = getData[0];
160  } else {
161  std::array<SigOutType, 1>& getData = client->getReceiveBuffer();
162  output = getData[0];
163  }
164 
165  // send
166  if (this->in.isConnected()) {
167  for(int i = 0; i < bufInLen; i++) sendData[i] = this->in.getSignal().getValue()(i);
168  if (isServer) server->setSendBuffer(sendData);
169  else client->setSendBuffer(sendData);
170  }
171 
172  this->out.getSignal().setValue(output);
174  this->out.getSignal().setTimestamp(time);
175  }
176 
177  virtual bool isNew() {
178  if (isServer) return server->newData; else return client->newData;
179  }
180 
181  virtual void resetNew() {
182  if (isServer) server->newData = false; else client->newData = false;
183  }
184 
185  template <typename X, typename Y>
186  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
187 
188  protected:
189  typedef typename SigInType::value_type SigInValueType;
190  eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 1, SigOutType>* server;
191  eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 1, SigOutType>* client;
192  std::array<SigInValueType, sizeof(SigInType) / sizeof(SigInValueType)> sendData;
193  uint32_t bufInLen, bufOutLen;
194  bool isServer;
195  };
196 
197  template < typename SigInType, typename SigOutType >
198  class SocketData<SigInType, SigOutType,
199  typename std::enable_if<std::is_arithmetic<SigInType>::value && std::is_arithmetic<SigOutType>::value>::type>
200  : public Block1i1o<SigInType, SigOutType> {
201  public:
202  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
203  bufInLen = 1;
204  bufOutLen = 1;
205  isServer = serverIP.empty();
206  if (isServer)
207  server = new eeros::sockets::SocketServer<1, SigInType, 1, SigOutType>(port, period, timeout);
208  else
209  client = new eeros::sockets::SocketClient<1, SigInType, 1, SigOutType>(serverIP, port, period, timeout);
210  }
211 
212  ~SocketData() {if (isServer) server->stop(); else client->stop();}
213 
214  virtual void run() {
215  // receive
216  SigOutType output;
217  if (isServer) {
218  std::array<SigOutType, 1>& getData = server->getReceiveBuffer();
219  output = getData[0];
220  } else {
221  std::array<SigOutType, 1>& getData = client->getReceiveBuffer();
222  output = getData[0];
223  }
224 
225  // send
226  if (this->in.isConnected()) {
227  sendData[0] = this->in.getSignal().getValue();
228  if (isServer) server->setSendBuffer(sendData);
229  else client->setSendBuffer(sendData);
230  }
231 
232  this->out.getSignal().setValue(output);
234  this->out.getSignal().setTimestamp(time);
235  }
236 
237  virtual bool isNew() {
238  if (isServer) return server->newData; else return client->newData;
239  }
240 
241  virtual void resetNew() {
242  if (isServer) server->newData = false; else client->newData = false;
243  }
244 
245  template <typename X, typename Y>
246  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
247 
248  protected:
251  std::array<SigInType, 1> sendData;
252  uint32_t bufInLen, bufOutLen;
253  bool isServer;
254  };
255 
256  template < typename SigInType, typename SigOutType >
257  class SocketData<SigInType, SigOutType,
258  typename std::enable_if<std::is_compound<SigInType>::value && std::is_same<SigOutType, std::nullptr_t>::value>::type>
259  : public Block1i1o<SigInType> {
260  public:
261  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
262  bufInLen = sizeof(SigInType) / sizeof(SigInValueType);
263  isServer = serverIP.empty();
264  if (isServer)
265  server = new eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t>(port, period, timeout);
266  else
267  client = new eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t>(serverIP, port, period, timeout);
268  }
269 
270  ~SocketData() {if (isServer) server->stop(); else client->stop();}
271 
272  virtual void run() {
273  // send
274  if (this->in.isConnected()) {
275  for(int i = 0; i < bufInLen; i++) sendData[i] = this->in.getSignal().getValue()(i);
276  if (isServer) server->setSendBuffer(sendData);
277  else client->setSendBuffer(sendData);
278  }
279  }
280 
281  template <typename X, typename Y>
282  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
283 
284  protected:
285  typedef typename SigInType::value_type SigInValueType;
286  eeros::sockets::SocketServer<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t>* server;
287  eeros::sockets::SocketClient<sizeof(SigInType) / sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t>* client;
288  std::array<SigInValueType, sizeof(SigInType) / sizeof(SigInValueType)> sendData;
289  uint32_t bufInLen;
290  bool isServer;
291  };
292 
293  template < typename SigInType, typename SigOutType >
294  class SocketData<SigInType, SigOutType,
295  typename std::enable_if<std::is_same<SigInType, std::nullptr_t>::value && std::is_compound<SigOutType>::value>::type>
296  : public Block1i1o<SigOutType> {
297  public:
298  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
299  bufOutLen = sizeof(SigOutType) / sizeof(SigOutValueType);
300  isServer = serverIP.empty();
301  if (isServer)
302  server = new eeros::sockets::SocketServer<0, std::nullptr_t, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(port, period, timeout);
303  else
304  client = new eeros::sockets::SocketClient<0, std::nullptr_t, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>(serverIP, port, period, timeout);
305  }
306 
307  ~SocketData() {if (isServer) server->stop(); else client->stop();}
308 
309  virtual void run() {
310  // receive
311  SigOutType output;
312  if (isServer) {
313  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = server->getReceiveBuffer();
314  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
315  } else {
316  std::array<SigOutValueType, sizeof(SigOutType) / sizeof(SigOutValueType)>& getData = client->getReceiveBuffer();
317  for (int i = 0; i < bufOutLen; i++) output(i) = getData[i];
318  }
319 
320  this->out.getSignal().setValue(output);
322  this->out.getSignal().setTimestamp(time);
323  }
324 
325  virtual bool isNew() {
326  if (isServer) return server->newData; else return client->newData;
327  }
328 
329  virtual void resetNew() {
330  if (isServer) server->newData = false; else client->newData = false;
331  }
332 
333  template <typename X, typename Y>
334  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
335 
336  protected:
337  typedef typename SigOutType::value_type SigOutValueType;
338  eeros::sockets::SocketServer<0, std::nullptr_t, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* server;
339  eeros::sockets::SocketClient<0, std::nullptr_t, sizeof(SigOutType) / sizeof(SigOutValueType), SigOutValueType>* client;
340  uint32_t bufOutLen;
341  bool isServer;
342  };
343 
344  template < typename SigInType, typename SigOutType >
345  class SocketData<SigInType, SigOutType,
346  typename std::enable_if<std::is_arithmetic<SigInType>::value && std::is_same<SigOutType, std::nullptr_t>::value>::type>
347  : public Block1i1o<SigInType> {
348  public:
349  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
350  bufInLen = 1;
351  isServer = serverIP.empty();
352  if (isServer)
353  server = new eeros::sockets::SocketServer<1, SigInType, 0, std::nullptr_t>(port, period, timeout);
354  else
355  client = new eeros::sockets::SocketClient<1, SigInType, 0, std::nullptr_t>(serverIP, port, period, timeout);
356  }
357 
358  ~SocketData() {if (isServer) server->stop(); else client->stop();}
359 
360  virtual void run() {
361  // send
362  if (this->in.isConnected()) {
363  sendData[0] = this->in.getSignal().getValue();
364  if (isServer) server->setSendBuffer(sendData);
365  else client->setSendBuffer(sendData);
366  }
367  }
368 
369  template <typename X, typename Y>
370  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
371 
372  protected:
375  std::array<SigInType, 1> sendData;
376  uint32_t bufInLen;
377  bool isServer;
378  };
379 
380  template < typename SigInType, typename SigOutType >
381  class SocketData<SigInType, SigOutType,
382  typename std::enable_if<std::is_same<SigInType, std::nullptr_t>::value && std::is_arithmetic<SigOutType>::value>::type>
383  : public Block1i1o<SigOutType> {
384  public:
385  SocketData(std::string serverIP, uint16_t port, double period = 0.01, double timeout = 1.0) {
386  bufOutLen = 1;
387  isServer = serverIP.empty();
388  if (isServer)
389  server = new eeros::sockets::SocketServer<0, std::nullptr_t, 1, SigOutType>(port, period, timeout);
390  else
391  client = new eeros::sockets::SocketClient<0, std::nullptr_t, 1, SigOutType>(serverIP, port, period, timeout);
392  }
393 
394  ~SocketData() {if (isServer) server->stop(); else client->stop();}
395 
396  virtual void run() {
397  // receive
398  SigOutType output;
399  if (isServer) {
400  std::array<SigOutType, 1>& getData = server->getReceiveBuffer();
401  output = getData[0];
402  } else {
403  std::array<SigOutType, 1>& getData = client->getReceiveBuffer();
404  output = getData[0];
405  }
406 
407  this->out.getSignal().setValue(output);
409  this->out.getSignal().setTimestamp(time);
410  }
411 
412  virtual bool isNew() {
413  if (isServer) return server->newData; else return client->newData;
414  }
415 
416  virtual void resetNew() {
417  if (isServer) server->newData = false; else client->newData = false;
418  }
419 
420  template <typename X, typename Y>
421  friend std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s);
422 
423  protected:
426  uint32_t bufOutLen;
427  bool isServer;
428  };
429 
430  /********** Print functions **********/
431  template <typename X, typename Y>
432  std::ostream& operator<<(std::ostream& os, SocketData<X,Y>& s) {
433  os << "Block socket data: '" << s.getName() << "'";
434  }
435 
436  };
437 }
438 
439 #endif /* ORG_EEROS_CONTROL_SOCKETDATA_HPP_ */
eeros::sockets::SocketServer< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t > * server
Definition: SocketData.hpp:286
eeros::sockets::SocketClient< 0, std::nullptr_t, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * client
Definition: SocketData.hpp:339
virtual Signal< T > & getSignal()
Definition: Output.hpp:16
SocketData(std::string serverIP, uint16_t port, double period=0.01, double timeout=1.0)
Definition: SocketData.hpp:22
eeros::sockets::SocketServer< 0, std::nullptr_t, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * server
Definition: SocketData.hpp:338
eeros::sockets::SocketClient< 1, SigInType, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * client
Definition: SocketData.hpp:131
virtual T getValue() const
Definition: Signal.hpp:49
virtual bool isConnected() const
Definition: Input.hpp:34
Definition: SocketData.hpp:15
Input< SigInType > in
Definition: Block1i1o.hpp:27
eeros::sockets::SocketServer< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * server
Definition: SocketData.hpp:70
Definition: Config.hpp:14
static uint64_t getTimeNs()
Definition: System_POSIX.cpp:41
Output< SigOutType > out
Definition: Block1i1o.hpp:28
virtual void setValue(T newValue)
Definition: Signal.hpp:53
virtual void setTimestamp(timestamp_t newTimestamp)
Definition: Signal.hpp:66
eeros::sockets::SocketClient< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, 0, std::nullptr_t > * client
Definition: SocketData.hpp:287
eeros::sockets::SocketClient< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, 1, SigOutType > * client
Definition: SocketData.hpp:191
Definition: Block1i1o.hpp:12
virtual Signal< T > & getSignal()
Definition: Input.hpp:38
Definition: SocketClient.hpp:22
eeros::sockets::SocketServer< 1, SigInType, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * server
Definition: SocketData.hpp:130
Definition: SocketServer.hpp:24
eeros::sockets::SocketServer< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, 1, SigOutType > * server
Definition: SocketData.hpp:190
eeros::sockets::SocketClient< sizeof(SigInType)/sizeof(SigInValueType), SigInValueType, sizeof(SigOutType)/sizeof(SigOutValueType), SigOutValueType > * client
Definition: SocketData.hpp:71
uint64_t timestamp_t
Definition: types.hpp:12