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