Jpp  16.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JControlHost.hh
Go to the documentation of this file.
1 #ifndef __JNET__JCONTROLHOST__
2 #define __JNET__JCONTROLHOST__
3 
4 #include <string>
5 #include <sstream>
6 #include <arpa/inet.h>
7 #include <set>
8 
9 #include "JNet/JPrefix.hh"
10 #include "JNet/JSocket.hh"
11 #include "JNet/JSocketBlocking.hh"
12 #include "JNet/JHostname.hh"
13 #include "JLang/JException.hh"
14 #include "JLang/JThrow.hh"
15 #include "JLang/JTimeval.hh"
16 #include "JMath/JMath.hh"
17 
18 
19 /**
20  * \author mdejong
21  */
22 
23 namespace JNET {}
24 namespace JPP { using namespace JNET; }
25 
26 namespace JNET {
27 
28  using JMATH::JMath;
30  using JLANG::JThrow;
31  using JLANG::JTimeval;
32 
33 
34  static const std::string CHOO_VERSION = "1.0";
35 
36 
37  /**
38  * ControlHost subscription types.
39  */
43  //SUBSCRIBE_SHARED_MEMORY = 'm'
44  };
45 
46 
47  /**
48  * ControlHost subscription.
49  */
50  class JSubscription :
51  public JTag
52  {
53  public:
54  /**
55  * Constructor.
56  *
57  * \param sub subscription
58  * \param tag tag
59  */
61  const JTag& tag) :
62  JTag (tag),
63  subscription(sub)
64  {}
65 
66 
67  /**
68  * Get subscription type.
69  *
70  * \return subscription
71  */
73  {
74  return subscription;
75  }
76 
77 
78  /**
79  * Convert subscription to string.
80  *
81  * \return subscription
82  */
83  std::string toString() const
84  {
85  return std::string(1, (char) subscription) + " " + JTag::toString();
86  }
87 
88 
89  protected:
91  };
92 
93 
94  /**
95  * Auxiliary class for all subscription.
96  */
98  public JSubscription
99  {
100  /**
101  * Constructor.
102  *
103  * \param tag tag
104  */
105  JSubscriptionAll(const JTag& tag) :
107  {}
108  };
109 
110 
111  /**
112  * Auxiliary class for any subscription.
113  */
115  public JSubscription
116  {
117  /**
118  * Constructor.
119  *
120  * \param tag tag
121  */
122  JSubscriptionAny(const JTag& tag) :
124  {}
125  };
126 
127 
128  /**
129  * Subscription list.
130  */
132  protected std::set<JSubscription> ,
133  public JMath<JSubscriptionList>
134  {
135  public:
136 
139 
140 
141  /**
142  * Default constructor.
143  */
145  {}
146 
147 
148  /**
149  * Copy constructor.
150  *
151  * \param subscription subscription
152  */
153  JSubscriptionList(const JSubscription& subscription)
154  {
155  add(subscription);
156  }
157 
158 
159  /**
160  * Smart pointer.
161  *
162  * \return pointer to set
163  */
165  {
166  return static_cast<const std::set<JSubscription>*>(this);
167  }
168 
169 
170  /**
171  * Add subscription.
172  *
173  * \param subscription subscription
174  */
175  JSubscriptionList& add(const JSubscription& subscription)
176  {
177  const_iterator p = this->find(subscription);
178 
179  if (p != this->end() && p->getID() == subscription.getID()) {
180 
181  if (p-> getSubscription() == SUBSCRIBE_ALL ||
182  subscription.getSubscription() == SUBSCRIBE_ANY)
183  return *this; // maintain higher subscription level
184  else
185  this->erase(p); // remove lower subscription level
186  }
187 
188  this->insert(subscription);
189 
190  return *this;
191  }
192 
193 
194  /**
195  * Add subscription.
196  *
197  * \param subscription subscription
198  * \return this subscription
199  */
201  {
202  for (JSubscriptionList::const_iterator i = subscription.begin(); i != subscription.end(); ++i) {
203  this->add(*i);
204  }
205 
206  return *this;
207  }
208 
209 
210  /**
211  * Convert subscription list to string.
212  *
213  * \return subscription
214  */
215  std::string toString() const
216  {
217  std::string buffer;
218 
219  for (const_iterator i = this->begin(); i != this->end(); ++i)
220  buffer += ' ' + i->toString();
221 
222  return buffer;
223  }
224  };
225 
226 
227  /**
228  * Add operator.
229  *
230  * \param first subscription
231  * \param second subscription
232  * \return subscription list
233  */
235  {
236  JSubscriptionList buffer;
237 
238  buffer.add(first);
239  buffer.add(second);
240 
241  return buffer;
242  }
243 
244 
245  /**
246  * ControlHost class.
247  */
248  class JControlHost :
249  public JSocketBlocking,
250  public JThrow<JControlHost>
251  {
252  protected:
253  /**
254  * Default constructor.
255  */
257  JSocketBlocking(JSocket(AF_INET, SOCK_STREAM))
258  {
259  configure();
260  }
261 
262 
263  public:
264  /**
265  * Check special ControlHost tags.
266  *
267  * \param tag tag
268  * \return true if possibly special tag; else false
269  */
270  static bool maybe_special(const JTag& tag)
271  {
272  return tag[0] == '_';
273  }
274 
275 
276  /**
277  * Check validity of subscription specifier.
278  *
279  * \param c subscription specifier
280  * \return true if valid; else false
281  */
282  static bool is_valid(const char c)
283  {
284  return (c == SUBSCRIBE_ALL ||
285  c == SUBSCRIBE_ANY);// ||
286  //c == SUBSCRIBE_SHARED_MEMORY);
287  }
288 
289 
290  /**
291  * Constructor.
292  *
293  * \param server host name and optional port number
294  */
295  JControlHost(const JHostname& server) :
296  JSocketBlocking(JSocket(AF_INET, SOCK_STREAM))
297  {
298  if (server.hostname != "")
299  connect(server.hostname, server.port);
300  else
301  connect(server.port);
302 
303  configure();
304  }
305 
306 
307  /**
308  * Constructor.
309  *
310  * \param server host name
311  * \param port port
312  */
313  JControlHost(const std::string& server,
314  const int port) :
315  JSocketBlocking(JSocket(AF_INET, SOCK_STREAM))
316  {
317  connect(server, port);
318 
319  configure();
320  }
321 
322 
323  /**
324  * Constructor.
325  *
326  * \param ip_number IP number
327  * \param port port
328  */
329  JControlHost(const int ip_number,
330  const int port = DISPATCH_PORT) :
331  JSocketBlocking(JSocket(AF_INET, SOCK_STREAM))
332  {
333  connect(ip_number, port);
334 
335  configure();
336  }
337 
338 
339  /**
340  * Constructor.
341  *
342  * \param socket socket
343  */
344  JControlHost(const JSocket& socket) :
345  JSocketBlocking(JSocket(socket))
346  {}
347 
348 
349  /**
350  * Destructor.
351  */
353  {
354  shutdown();
355  }
356 
357 
358  /**
359  * Subscribe to single tag.
360  *
361  * \param subscription subscription
362  * \return 0 if OK; -1 if socket error
363  */
364  int Subscribe(const JSubscription& subscription)
365  {
366  return PutFullString(DISPTAG_Subscribe, subscription.toString());
367  }
368 
369 
370  /**
371  * Subscribe to list of tags.
372  *
373  * \param subscription subscription
374  * \return 0 if OK; -1 if socket error
375  */
376  int Subscribe(const JSubscriptionList& subscription)
377  {
378  return PutFullString(DISPTAG_Subscribe, subscription.toString());
379  }
380 
381 
382  /**
383  * Identify.
384  *
385  * \param nick_name nick name
386  * \return 0 if OK; -1 if socket error
387  */
388  int MyId(const std::string& nick_name)
389  {
390  return PutFullString(DISPTAG_MyId, nick_name);
391  }
392 
393 
394  /**
395  * Tell server to send next message.
396  *
397  * \return 0 if OK; -1 if socket error
398  */
400  {
401  return PutFullData(DISPTAG_Gime, NULL, 0);
402  }
403 
404 
405  /**
406  * Tell server to send messages forever.
407  *
408  * \return 0 if OK; -1 if socket error
409  */
411  {
412  return PutFullData(DISPTAG_Always, NULL, 0);
413  }
414 
415 
416  /**
417  * Send data.
418  *
419  * \param tag tag
420  * \param buffer data
421  * \param length number of bytes
422  * \return 0 if OK; -1 if socket error; -2 if other error
423  */
424  int PutFullData(const JTag& tag, const void* buffer, const long long int length)
425  {
426  try {
427 
428  JPrefix __prefix__(tag, length);
429 
430  write((char*) &__prefix__, sizeof(JPrefix));
431 
432  if (length != 0) {
433  write((char*) buffer, (int) length);
434  }
435 
436  return 0;
437  }
438  catch (const JSocketException& error) {
439  return Throw(error, -1);
440  }
441  }
442 
443 
444  /**
445  * Send data.
446  *
447  * \param tag tag
448  * \param buffer data
449  * \param length number of bytes
450  * \return 0 if OK; -1 if socket error; -2 if other error
451  */
452  int PutFullData(const std::string& tag, const void* buffer, const long long int length)
453  {
454  try {
455  return PutFullData(JTag(tag), buffer, length);
456  }
457  catch (const JSocketException& error) {
458  return Throw(error, -1);
459  }
460  catch (const JControlHostException& error) {
461  return Throw(error, -2);
462  }
463  }
464 
465 
466  /**
467  * Send string.
468  *
469  * \param tag tag
470  * \param buffer data
471  * \return 0 if OK; -1 if socket error; -2 if other error
472  */
473  int PutFullString(const JTag& tag,
474  const std::string& buffer)
475  {
476  return PutFullData(tag, buffer.c_str(), buffer.size());
477  }
478 
479 
480  /**
481  * Send string.
482  *
483  * \param tag tag
484  * \param buffer data
485  * \return 0 if OK; -1 if socket error; -2 if other error
486  */
487  int PutFullString(const std::string& tag,
488  const std::string& buffer)
489  {
490  return PutFullData(tag, buffer.c_str(), buffer.size());
491  }
492 
493 
494  /**
495  * Send version.
496  *
497  * \return 0 if OK; -1 if socket error; -2 if other error
498  */
499  int Connected()
500  {
502  }
503 
504 
505  /**
506  * Wait for header.
507  *
508  * \param prefix prefix
509  * \return 0 if OK; -1 if socket error
510  */
512  {
513  try {
514 
515  read((char*) &this->prefix, sizeof(JPrefix));
516 
517  prefix = this->prefix;
518 
519  return 0;
520  }
521  catch (const JSocketException& error) {
522  return Throw(error, -1);
523  }
524  }
525 
526 
527  /**
528  * Wait for header.
529  *
530  * \param tag tag
531  * \param length number of bytes
532  * \return 0 if OK; -1 if socket error
533  */
534  int WaitHead(std::string& tag, long long int& length)
535  {
536  const int rvalue = WaitHead(this->prefix);
537 
538  if (rvalue == 0) {
539  tag = this->prefix.getTag();
540  length = this->prefix.getSize();
541  }
542 
543  return rvalue;
544  }
545 
546 
547  /**
548  * Check for header, without waiting.
549  *
550  * \param prefix prefix
551  * \param timeout timeout
552  * \return 1 if header; 0 if no header; -1 if socket error
553  */
555  {
556  try {
557 
558  if (in_avail(timeout)) {
559 
560  WaitHead(prefix);
561 
562  return 1;
563 
564  } else {
565 
566  return 0;
567  }
568  }
569  catch (const JSocketException& error) {
570  return Throw(error, -1);
571  }
572  }
573 
574 
575  /**
576  * Check for header, without waiting.
577  *
578  * \param tag tag
579  * \param length number of bytes
580  * \param timeout_us timeout [us]
581  * \return 1 if header; 0 if no header; -1 if socket error
582  */
583  int CheckHead(std::string& tag, long long int& length, const int timeout_us = 0)
584  {
585  const int rvalue = CheckHead(this->prefix, timeout_us);
586 
587  if (rvalue == 1) {
588  tag = this->prefix.getTag();
589  length = this->prefix.getSize();
590  }
591 
592  return rvalue;
593  }
594 
595 
596  /**
597  * Receive data.
598  *
599  * \param buffer data
600  * \param length number of bytes
601  * \return 0 if OK; -1 if socket error
602  */
603  int GetFullData(void* buffer, long long int length)
604  {
605  try {
606 
607  read((char*) buffer, (int) length);
608 
609  return 0;
610  }
611  catch (const JSocketException& error) {
612  return Throw(error, -1);
613  }
614  }
615 
616 
617  /**
618  * Receive string.
619  *
620  * \param buffer data
621  * \return 0 if OK; -1 if socket error
622  */
623  int GetFullString(std::string& buffer)
624  {
625  buffer.resize(this->prefix.getSize());
626 
627  return GetFullData((char*) buffer.data(), buffer.size());
628  }
629 
630 
631  /**
632  * Locate ControlHost client(s).
633  *
634  * \param host_name host name
635  * \param nick_name nick name
636  * \param answer list of host names
637  * \return 0 if OK; -1 if socket error
638  */
639  static int WhereIs(const std::string& host_name,
640  const std::string& nick_name,
641  std::string& answer)
642  {
643  try {
644 
645  using namespace std;
646 
647  JControlHost socket(host_name);
648 
649  socket.PutFullString(DISPTAG_WhereIs, nick_name);
650 
651  string tag;
652  long long int length;
653 
654  socket.WaitHead(tag, length);
655  socket.GetFullString(answer);
656 
657  return 0;
658  }
659  catch (const JSocketException& error) {
660  return Throw(error, -1);
661  }
662  }
663 
664 
665  /**
666  * Configure socket (factory reset).
667  *
668  * \return 0 if OK; -1 if socket error
669  */
670  int configure()
671  {
672  try {
673 
674  setTcpNoDelay (true);
675  setReuseAddress(true);
676  setKeepAlive (true);
677  setSendBufferSize (128*1024);
678  setReceiveBufferSize(128*1024);
679  setNonBlocking (false);
680 
681  return 0;
682  }
683  catch (const JSocketException& error) {
684  return Throw(error, -1);
685  }
686  }
687 
688 
689  private:
690  mutable JPrefix prefix;
691 
692  /**
693  */
694  JControlHost(const JControlHost&);
696  };
697 
698 
699  /**
700  * Match name.
701  */
703 }
704 
705 #endif
ControlHost prefix.
Definition: JPrefix.hh:31
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:207
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
JSubscription_t getSubscription() const
Get subscription type.
Definition: JControlHost.hh:72
int Subscribe(const JSubscriptionList &subscription)
Subscribe to list of tags.
static const int DISPATCH_PORT
Default ControlHost port.
Definition: JHostname.hh:24
Exceptions.
JSubscriptionAll(const JTag &tag)
Constructor.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:110
int MyId(const std::string &nick_name)
Identify.
int CheckHead(std::string &tag, long long int &length, const int timeout_us=0)
Check for header, without waiting.
ControlHost class.
JSubscription_t
ControlHost subscription types.
Definition: JControlHost.hh:40
int write(const char *buffer, const int length)
Write data to socket.
static const std::string CHOO_VERSION
Definition: JControlHost.hh:34
const char * c_str() const
C-string.
Definition: JTag.hh:201
void setSendBufferSize(const int size)
Set send buffer size.
Definition: JSocket.hh:273
int Connected()
Send version.
JSubscriptionList(const JSubscription &subscription)
Copy constructor.
int WaitHead(std::string &tag, long long int &length)
Wait for header.
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
static JTimeval min()
Get minimal time value.
Definition: JTimeval.hh:119
const std::set< JSubscription > * operator->() const
Smart pointer.
int PutFullString(const std::string &tag, const std::string &buffer)
Send string.
static const JTag DISPTAG_MyId("_MyId")
std::string toString() const
Convert subscription list to string.
int Subscribe(const JSubscription &subscription)
Subscribe to single tag.
int SendMeAlways()
Tell server to send messages forever.
std::set< JSubscription >::const_reverse_iterator const_reverse_iterator
static const JTag DISPTAG_WhereIs("_WhereIs")
Socket class.
Definition: JSocket.hh:43
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
std::string toString() const
Convert subscription to string.
Definition: JControlHost.hh:83
int getSize() const
Get size.
Definition: JPrefix.hh:62
Subscription list.
JControlHost(const JSocket &socket)
Constructor.
std::string hostname
Definition: JHostname.hh:154
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
static const JTag DISPTAG_Version("_Version")
Auxiliary class for time values.
Definition: JTimeval.hh:26
int CheckHead(JPrefix &prefix, JTimeval timeout=JTimeval::min())
Check for header, without waiting.
bool in_avail(JTimeval timeout=JTimeval::min()) const
Check availability of input.
Definition: JFile.hh:100
JSubscription(const JSubscription_t sub, const JTag &tag)
Constructor.
Definition: JControlHost.hh:60
JSubscriptionList()
Default constructor.
JControlHost(const std::string &server, const int port)
Constructor.
int PutFullData(const std::string &tag, const void *buffer, const long long int length)
Send data.
JControlHost(const int ip_number, const int port=DISPATCH_PORT)
Constructor.
JControlHost(const JHostname &server)
Constructor.
int GetFullString(std::string &buffer)
Receive string.
std::set< JSubscription >::const_iterator const_iterator
int shutdown()
Shut down socket.
Definition: JSocket.hh:95
JControlHost ControlHost
Match name.
int SendMeNext()
Tell server to send next message.
JTag_t getID() const
Get identifier.
Definition: JTag.hh:160
Auxiliary class for any subscription.
JSubscriptionList & add(const JSubscriptionList &subscription)
Add subscription.
JControlHost & operator=(const JControlHost &)
JSubscriptionList & add(const JSubscription &subscription)
Add subscription.
Exception for ControlHost.
Definition: JException.hh:468
Auxiliary class for all subscription.
Definition: JControlHost.hh:97
JSubscriptionAny(const JTag &tag)
Constructor.
static bool is_valid(const char c)
Check validity of subscription specifier.
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
static bool maybe_special(const JTag &tag)
Check special ControlHost tags.
void setReceiveBufferSize(const int size)
Set receive buffer size.
Definition: JSocket.hh:251
Vec operator+(const Vec &a, const Vec &b)
Add two vectors.
Definition: Vec.hh:337
Blocking socket I/O.
int WaitHead(JPrefix &prefix)
Wait for header.
std::string toString() const
Convert tag to string.
Definition: JTag.hh:171
int configure()
Configure socket (factory reset).
int GetFullData(void *buffer, long long int length)
Receive data.
static const JTag DISPTAG_Gime("_Gime")
ControlHost subscription.
Definition: JControlHost.hh:50
Exception for socket.
Definition: JException.hh:450
void connect(const int port)
Connect to port on local host.
Definition: JSocket.hh:385
Exception handling.
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JSocket.hh:110
JSubscriptionList getSubscription(const JEventTable &event_table)
Convert event table to ControlHost subscription.
Definition: JEventTable.hh:129
Base class for data structures with artithmetic capabilities.
void setKeepAlive(const bool on)
Set keep alive of socket.
Definition: JSocket.hh:152
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
static int WhereIs(const std::string &host_name, const std::string &nick_name, std::string &answer)
Locate ControlHost client(s).
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:229
ControlHost tag.
Definition: JTag.hh:38
int read(char *buffer, const int length)
Read data from socket.
static const JTag DISPTAG_Always("_Always")
~JControlHost()
Destructor.
JSubscription_t subscription
Definition: JControlHost.hh:90
int PutFullData(const JTag &tag, const void *buffer, const long long int length)
Send data.
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
int PutFullString(const JTag &tag, const std::string &buffer)
Send string.
JControlHost()
Default constructor.