Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRuncontrolToolkit.hh
Go to the documentation of this file.
1 #ifndef __JRUNCONTROLTOOLKIT__
2 #define __JRUNCONTROLTOOLKIT__
3 
4 #include <string>
5 #include <istream>
6 #include <ostream>
7 #include <sstream>
8 #include <iomanip>
9 
10 #include "JLang/JException.hh"
11 #include "JSystem/JNetwork.hh"
12 #include "JNet/JHostname.hh"
13 #include "JNet/JPrefix.hh"
14 #include "JDAQ/JDAQTags.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace KM3NETDAQ {
22 
23  using JLANG::JIOException;
24  using JNET::JTag;
25  using JNET::JHostname;
26 
27 
28  /**
29  * Get the wild card.
30  *
31  * \return wild card
32  */
33  inline char getWildCard()
34  {
35  return WILD_CARD;
36  }
37 
38 
39  /**
40  * Get the event name delimeter.
41  *
42  * \return event name delimeter
43  */
44  inline char getEventnameDelimeter()
45  {
46  return EVENTNAME_DELIMETER;
47  }
48 
49 
50  /**
51  * Get the token delimeter for command messages.
52  *
53  * \return token delimeter
54  */
55  inline char getTokenDelimeter()
56  {
57  return TOKEN_DELIMETER[0];
58  }
59 
60 
61  /**
62  * Auxiliary class for handling event name and optional number.
63  */
64  struct JEvent_t {
65  /**
66  * Default constructor.
67  */
69  event_name ("?"),
70  event_number(0)
71  {}
72 
73 
74  /**
75  * Constructor.
76  *
77  * \param name event name
78  * \param number event number
79  */
80  JEvent_t(const std::string& name,
81  const int number) :
82  event_name (name),
83  event_number(number)
84  {}
85 
86 
87  /**
88  * Get event name.
89  *
90  * \return event name
91  */
92  const std::string& getName() const
93  {
94  return event_name;
95  }
96 
97 
98  /**
99  * Get event number.
100  *
101  * \return event number
102  */
103  int getNumber() const
104  {
105  return event_number;
106  }
107 
108 
109  /**
110  * Convert string to event.
111  *
112  * \param buffer buffer
113  * \return event
114  */
115  static JEvent_t toValue(const std::string& buffer)
116  {
117  using namespace std;
118 
119  JEvent_t object;
120 
121  const string::size_type sep = buffer.find(EVENTNAME_DELIMETER);
122 
123  if (sep != string::npos) {
124 
125  istringstream(buffer.substr(sep + 1)) >> object.event_number;
126 
127  object.event_name = buffer.substr(0, sep);
128 
129  } else {
130 
131  object.event_name = buffer;
132  object.event_number = -1;
133  }
134 
135  return object;
136  }
137 
138 
139  /**
140  * Read event name and optional number from input stream.
141  *
142  * \param in input stream
143  * \param object event
144  * \return input stream
145  */
146  friend inline std::istream& operator>>(std::istream& in, JEvent_t& object)
147  {
148  std::string buffer;
149 
150  in >> buffer;
151 
152  object = JEvent_t::toValue(buffer);
153 
154  return in;
155  }
156 
157 
158  /**
159  * Write event to output stream.
160  *
161  * \param out output stream
162  * \param object event
163  * \return output stream
164  */
165  friend inline std::ostream& operator<<(std::ostream& out, const JEvent_t& object)
166  {
167  out << object.event_name;
168 
169  if (object.event_number != -1) {
170  out << getEventnameDelimeter() << object.event_number;
171  }
172 
173  return out;
174  }
175 
176 
177  protected:
178  std::string event_name;
180  };
181 
182 
183  /**
184  * Get process name of run control client.
185  * The process name corresponds to the name of the executable file.
186  * A wild card in the client name is replaced by the process name.
187  *
188  * \param name name of client
189  * \param process name of process
190  * \return process name
191  */
192  inline std::string getProcessName(const std::string& name,
193  const std::string& process)
194 
195  {
196  using namespace std;
197 
198  string buffer(name);
199 
200  size_t j = buffer.find_first_of(getWildCard());
201 
202  if (j != string::npos) {
203  buffer.replace(j, 1, process);
204  }
205 
206  return buffer;
207  }
208 
209 
210  /**
211  * Get full name of run control client.
212  * The full name is the unique identifier of each run control client.
213  *
214  * \param hostname name of host
215  * \param name name of client
216  * \return full name
217  */
218  inline std::string getFullName(const std::string& hostname,
219  const std::string& name)
220  {
221  using namespace JSYSTEM;
222 
225  name);
226  }
227 
228 
229  /**
230  * Get full name of run control client.
231  *
232  * \param buffer full name (possibly followed by more text)
233  * \return full name
234  */
235  inline std::string getFullName(const std::string& buffer)
236  {
237  using namespace std;
238 
239  size_t j = 0;
240 
241  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? j : 0));
242  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? ++j : 0));
243  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? ++j : 0));
244 
245  return buffer.substr(0, j);
246  }
247 
248 
249  /**
250  * Simple data structure for DAQ run.
251  */
252  struct JDAQRun {
253  /**
254  * Default constructor .
255  */
257  unique_id(-1),
258  run (-1)
259  {}
260 
261 
262  /**
263  * Constructor .
264  *
265  * \param path directory path
266  * \param run run number
267  * \param extension file name extension
268  */
269  JDAQRun(const std::string& path,
270  const int run,
271  const std::string& extension = "root")
272  {
273  this->path = path;
274  this->unique_id = -1;
275  this->run = run;
276  this->extension = extension;
277  }
278 
279 
280  /**
281  * Constructor .
282  *
283  * \param path directory path
284  * \param unique_id unique identifier
285  * \param run run number
286  * \param extension file name extension
287  */
288  JDAQRun(const std::string& path,
289  const int unique_id,
290  const int run,
291  const std::string& extension = "root")
292  {
293  this->path = path;
294  this->unique_id = unique_id;
295  this->run = run;
296  this->extension = extension;
297  }
298 
299 
300  /**
301  * Constructor.
302  *
303  * \param file_name file name
304  */
305  JDAQRun(const char* file_name)
306  {
307  *this = valueOf(file_name);
308  }
309 
310 
311  /**
312  * Get file name prefix.
313  *
314  * \return prefix
315  */
316  static const char* getPrefix()
317  {
318  return "KM3NeT";
319  }
320 
321 
322  /**
323  * Extract DAQ run parameters.
324  *
325  * \param file_name file name
326  * \return DAQ run parameters
327  */
328  static JDAQRun valueOf(const std::string& file_name)
329  {
330  JDAQRun result;
331 
332  using namespace std;
333 
334  string buffer(file_name);
335 
336  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
337  if (*i == '_') {
338  *i = ' ';
339  }
340  }
341 
342  istringstream is(buffer);
343 
344  if (is >> result.path >> result.unique_id) {
345 
346  size_t pos = result.path.find(getPrefix()); // standard KM3NeT file name?
347 
348  if (pos == string::npos) {
349  pos = result.path.rfind("/"); // standard directory path
350  }
351 
352  if (pos != string::npos) {
353  result.path.erase(pos);
354  }
355 
356  if (!(is >> result.run)) {
357 
358  result.run = result.unique_id; // no unique identifier
359  result.unique_id = -1;
360 
361  is.clear();
362  }
363 
364  is >> result.extension;
365 
366  if (!result.extension.empty() && result.extension[0] == '.') {
367  result.extension.erase(0, 1);
368  }
369 
370  return result;
371  }
372 
373  throw JIOException("JDAQRun::valueOf() error parsing " + buffer);
374  }
375 
376 
377  /**
378  * Convert DAQ run to string.
379  *
380  * \return string
381  */
382  std::string toString() const
383  {
384  using namespace std;
385 
386  ostringstream os;
387 
388  if (!path.empty()) {
389 
390  os << path;
391 
392  if (*path.rbegin() != '/') {
393  os << '/';
394  }
395  }
396 
397  os << getPrefix()
398  << "_" << setw(8) << setfill('0') << unique_id
399  << "_" << setw(8) << setfill('0') << run
400  << '.' << extension;
401 
402  return os.str();
403  }
404 
405 
406  /**
407  * Get file name of run.
408  *
409  * \param path directory path
410  * \param unique_id unique identifier
411  * \param run run number
412  * \return file name
413  */
414  static inline std::string getFilename(const std::string& path, const int unique_id, const int run)
415  {
416  return JDAQRun(path, unique_id, run).toString();
417  }
418 
419 
420  std::string path; //!< directory path
421  int unique_id; //!< unique identifier
422  int run; //!< run number
423  std::string extension; //!< file name extension
424  };
425 
426 
427  /**
428  * Get unique tag of run control client.
429  * The unique tag is used to communicate with a single client.
430  *
431  * The unique tag consists of a sequence of 8 characters, where
432  * character 1-4 correspond the hexadecimal coded subaddress of the IP number of the host and
433  * character 5-8 to the short name of the client (i.e. part following the client name delimeter).
434  *
435  * \param hostname host name
436  * \param name client name
437  * \return tag
438  */
439  inline JTag getUniqueTag(const std::string& hostname,
440  const std::string& name)
441  {
442  using namespace std;
443  using namespace JSYSTEM;
444 
445  ostringstream os;
446 
447  os << hex << getSubaddress(getIPnumber(hostname));
448 
449  string::size_type pos = name.find_first_of(CLIENTNAME_DELIMETER);
450 
451  if (pos != string::npos) {
452  os << name.substr(pos + 1, 4);
453  }
454 
455  return JTag(os.str());
456  }
457 
458 
459  /**
460  * Auxiliary class for itemization of process list.
461  */
462  class JDAQProcess :
463  public JHostname
464  {
465  public:
466  /**
467  * Default constructor.
468  */
470  JHostname(),
471  index ()
472  {}
473 
474 
475  /**
476  * Constructor.
477  *
478  * \param hostname host name
479  * \param key index
480  */
481  JDAQProcess(const JHostname& hostname,
482  const std::string& key = "") :
483  JHostname(),
484  index (key)
485  {}
486 
487 
488  /**
489  * Get unique tag.
490  *
491  * \return tag
492  */
494  {
495  using namespace std;
496  using namespace JSYSTEM;
497 
498  ostringstream os;
499 
500  os << hex << getSubaddress(getIPnumber(this->hostname)) << this->index;
501 
502  return JTag(os.str());
503  }
504 
505 
506  /**
507  * Test whether given tag equals.
508  * The comparison is based on the unique tag.
509  *
510  * \param tag tag
511  * \return true if match; else false
512  */
513  bool equals(const JTag& tag) const
514  {
515  return this->getUniqueTag() == tag;
516  }
517 
518 
519  /**
520  * Test whether given port equals.
521  * The comparison is based on the IP number and port number.
522  *
523  * \param port port
524  * \return true if match; else false
525  */
526  bool equals(int port) const
527  {
528  return (JSYSTEM::getIPnumber() == JSYSTEM::getIPnumber(this->hostname) && port == this->port);
529  }
530 
531 
532  /**
533  * Test whether given host name equals.
534  * The comparison is based on the IP number and port number.
535  *
536  * \param hostname host name
537  * \return true if match; else false
538  */
539  bool equals(const JHostname& hostname) const
540  {
541  return (JSYSTEM::getIPnumber(hostname.hostname) == JSYSTEM::getIPnumber(this->hostname) && hostname.port == this->port);
542  }
543 
544 
545  /**
546  * Read process from input stream.
547  *
548  * \param in input stream
549  * \param object process
550  * \return input stream
551  */
552  friend inline std::istream& operator>>(std::istream& in, JDAQProcess& object)
553  {
554  in >> object.index;
555  in >> static_cast<JHostname&>(object);
556 
557  return in;
558  }
559 
560 
561  /**
562  * Write process to output stream.
563  *
564  * \param out output stream
565  * \param object process
566  * \return output stream
567  */
568  friend inline std::ostream& operator<<(std::ostream& out, const JDAQProcess& object)
569  {
570  out << object.index;
571  out << ' ';
572  out << static_cast<const JHostname&>(object);
573 
574  return out;
575  }
576 
577 
578  std::string index; //!< index in process list
579  };
580 
581 
582  /**
583  * Get name of state.
584  * Note that this method is used for backward compatibility.
585  *
586  * \param name name of state
587  * \return name of state
588  */
589  inline std::string getStateName(const std::string& name)
590  {
591  using namespace std;
592 
593  static const string target = "Operational.";
594 
595  string buffer = name;
596 
597  const string::size_type pos = buffer.find(target);
598 
599  if (pos != string::npos)
600  return buffer.erase(pos, target.size());
601  else
602  return buffer;
603  }
604 }
605 
606 #endif
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
const std::string & getName() const
Get event name.
Exceptions.
Target.
Definition: JHead.hh:146
std::string getProcessName(const std::string &name, const std::string &process)
Get process name of run control client.
friend std::istream & operator>>(std::istream &in, JDAQProcess &object)
Read process from input stream.
static std::string getFilename(const std::string &path, const int unique_id, const int run)
Get file name of run.
JTag getUniqueTag(const std::string &hostname, const std::string &name)
Get unique tag of run control client.
static const char EVENTNAME_DELIMETER
Definition: JDAQTags.hh:35
std::string extension
file name extension
int unique_id
unique identifier
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:150
static const std::string TOKEN_DELIMETER
Definition: JDAQTags.hh:36
unsigned short getSubaddress(const int ip)
Get host identifier within network.
Definition: JNetwork.hh:186
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
Simple data structure for DAQ run.
std::string hostname
Definition: JHostname.hh:154
JDAQRun(const char *file_name)
Constructor.
static JEvent_t toValue(const std::string &buffer)
Convert string to event.
std::string index
index in process list
std::string path
directory path
std::string getStateName(const std::string &name)
Get name of state.
static const std::string RUN_CONTROL_CLIENT
Definition: JDAQTags.hh:22
Auxiliary class for itemization of process list.
friend std::ostream & operator<<(std::ostream &out, const JDAQProcess &object)
Write process to output stream.
JDAQProcess()
Default constructor.
static const std::string CLIENTNAME_DELIMETER
Definition: JDAQTags.hh:37
JDAQRun(const std::string &path, const int run, const std::string &extension="root")
Constructor .
bool equals(int port) const
Test whether given port equals.
Auxiliary class for handling event name and optional number.
friend std::istream & operator>>(std::istream &in, JEvent_t &object)
Read event name and optional number from input stream.
JDAQProcess(const JHostname &hostname, const std::string &key="")
Constructor.
char getTokenDelimeter()
Get the token delimeter for command messages.
JTag getUniqueTag() const
Get unique tag.
char getEventnameDelimeter()
Get the event name delimeter.
JEvent_t()
Default constructor.
char getWildCard()
Get the wild card.
bool equals(const JTag &tag) const
Test whether given tag equals.
int getNumber() const
Get event number.
static const char * getPrefix()
Get file name prefix.
static const char WILD_CARD
Definition: JDAQTags.hh:34
static JDAQRun valueOf(const std::string &file_name)
Extract DAQ run parameters.
Fixed parameters for KM3NeT DAQ.
std::string toString() const
Convert DAQ run to string.
friend std::ostream & operator<<(std::ostream &out, const JEvent_t &object)
Write event to output stream.
JDAQRun(const std::string &path, const int unique_id, const int run, const std::string &extension="root")
Constructor .
ControlHost tag.
Definition: JTag.hh:35
std::string getFullName(const std::string &hostname, const std::string &name)
Get full name of run control client.
JDAQRun()
Default constructor .
bool equals(const JHostname &hostname) const
Test whether given host name equals.
Hostname and IP address functions.
JEvent_t(const std::string &name, const int number)
Constructor.
Exception for I/O.
Definition: JException.hh:306