Jpp  18.2.0-rc.1
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  * Get process name of run control client.
64  * The process name corresponds to the name of the executable file.
65  * A wild card in the client name is replaced by the process name.
66  *
67  * \param name name of client
68  * \param process name of process
69  * \return process name
70  */
72  const std::string& process)
73 
74  {
75  using namespace std;
76 
77  string buffer(name);
78 
79  size_t j = buffer.find_first_of(getWildCard());
80 
81  if (j != string::npos) {
82  buffer.replace(j, 1, process);
83  }
84 
85  return buffer;
86  }
87 
88 
89  /**
90  * Get full name of run control client.
91  * The full name is the unique identifier of each run control client.
92  *
93  * \param hostname name of host
94  * \param name name of client
95  * \return full name
96  */
97  inline std::string getFullName(const std::string& hostname,
98  const std::string& name)
99  {
100  using namespace JSYSTEM;
101 
104  name);
105  }
106 
107 
108  /**
109  * Get full name of run control client.
110  *
111  * \param buffer full name (possibly followed by more text)
112  * \return full name
113  */
114  inline std::string getFullName(const std::string& buffer)
115  {
116  using namespace std;
117 
118  size_t j = 0;
119 
120  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? j : 0));
121  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? ++j : 0));
122  j = buffer.find_first_of(getTokenDelimeter(), (j != string::npos ? ++j : 0));
123 
124  return buffer.substr(0, j);
125  }
126 
127 
128  /**
129  * Simple data structure for DAQ run.
130  */
131  struct JDAQRun {
132  /**
133  * Default constructor .
134  */
136  unique_id(-1),
137  run (-1)
138  {}
139 
140 
141  /**
142  * Constructor .
143  *
144  * \param path directory path
145  * \param run run number
146  * \param extension file name extension
147  */
149  const int run,
150  const std::string& extension = "root")
151  {
152  this->path = path;
153  this->unique_id = -1;
154  this->run = run;
155  this->extension = extension;
156  }
157 
158 
159  /**
160  * Constructor .
161  *
162  * \param path directory path
163  * \param unique_id unique identifier
164  * \param run run number
165  * \param extension file name extension
166  */
168  const int unique_id,
169  const int run,
170  const std::string& extension = "root")
171  {
172  this->path = path;
173  this->unique_id = unique_id;
174  this->run = run;
175  this->extension = extension;
176  }
177 
178 
179  /**
180  * Constructor.
181  *
182  * \param file_name file name
183  */
184  JDAQRun(const char* file_name)
185  {
186  *this = valueOf(file_name);
187  }
188 
189 
190  /**
191  * Get file name prefix.
192  *
193  * \return prefix
194  */
195  static const char* getPrefix()
196  {
197  return "KM3NeT";
198  }
199 
200 
201  /**
202  * Extract DAQ run parameters.
203  *
204  * \param file_name file name
205  * \return DAQ run parameters
206  */
207  static JDAQRun valueOf(const std::string& file_name)
208  {
209  JDAQRun result;
210 
211  using namespace std;
212 
213  string buffer(file_name);
214 
215  for (string::iterator i = buffer.begin(); i != buffer.end(); ++i) {
216  if (*i == '_') {
217  *i = ' ';
218  }
219  }
220 
221  istringstream is(buffer);
222 
223  if (is >> result.path >> result.unique_id) {
224 
225  size_t pos = result.path.find(getPrefix()); // standard %KM3NeT file name?
226 
227  if (pos == string::npos) {
228  pos = result.path.rfind("/"); // standard directory path
229  }
230 
231  if (pos != string::npos) {
232  result.path.erase(pos);
233  }
234 
235  if (!(is >> result.run)) {
236 
237  result.run = result.unique_id; // no unique identifier
238  result.unique_id = -1;
239 
240  is.clear();
241  }
242 
243  is >> result.extension;
244 
245  if (!result.extension.empty() && result.extension[0] == '.') {
246  result.extension.erase(0, 1);
247  }
248 
249  return result;
250  }
251 
252  THROW(JIOException, "JDAQRun::valueOf() error parsing " << buffer);
253  }
254 
255 
256  /**
257  * Convert DAQ run to string.
258  *
259  * \return string
260  */
262  {
263  using namespace std;
264 
265  ostringstream os;
266 
267  if (!path.empty()) {
268 
269  os << path;
270 
271  if (*path.rbegin() != '/') {
272  os << '/';
273  }
274  }
275 
276  os << getPrefix()
277  << "_" << setw(8) << setfill('0') << unique_id
278  << "_" << setw(8) << setfill('0') << run
279  << '.' << extension;
280 
281  return os.str();
282  }
283 
284 
285  /**
286  * Get file name of run.
287  *
288  * \param path directory path
289  * \param unique_id unique identifier
290  * \param run run number
291  * \return file name
292  */
293  static inline std::string getFilename(const std::string& path, const int unique_id, const int run)
294  {
295  return JDAQRun(path, unique_id, run).toString();
296  }
297 
298 
299  std::string path; //!< directory path
300  int unique_id; //!< unique identifier
301  int run; //!< run number
302  std::string extension; //!< file name extension
303  };
304 
305 
306  /**
307  * Get unique tag of run control client.
308  * The unique tag is used to communicate with a single client.
309  *
310  * The unique tag consists of a sequence of 8 characters, where
311  * character 1-4 correspond the hexadecimal coded subaddress of the IP number of the host and
312  * character 5-8 to the short name of the client (i.e.\ part following the client name delimeter).
313  *
314  * \param hostname host name
315  * \param name client name
316  * \return tag
317  */
318  inline JTag getUniqueTag(const std::string& hostname,
319  const std::string& name)
320  {
321  using namespace std;
322  using namespace JSYSTEM;
323 
324  ostringstream os;
325 
326  os << hex << getSubaddress(getIPnumber(hostname));
327 
328  string::size_type pos = name.find_first_of(CLIENTNAME_DELIMETER);
329 
330  if (pos != string::npos) {
331  os << name.substr(pos + 1, 4);
332  }
333 
334  return JTag(os.str());
335  }
336 
337 
338  /**
339  * Auxiliary class for itemization of process list.
340  */
341  class JDAQProcess :
342  public JHostname
343  {
344  public:
345  /**
346  * Default constructor.
347  */
349  JHostname(),
350  index ()
351  {}
352 
353 
354  /**
355  * Constructor.
356  *
357  * \param hostname host name
358  * \param key index
359  */
360  JDAQProcess(const JHostname& hostname,
361  const std::string& key = "") :
362  JHostname(),
363  index (key)
364  {}
365 
366 
367  /**
368  * Get unique tag.
369  *
370  * \return tag
371  */
373  {
374  using namespace std;
375  using namespace JSYSTEM;
376 
377  ostringstream os;
378 
379  os << hex << getSubaddress(getIPnumber(this->hostname)) << this->index;
380 
381  return JTag(os.str());
382  }
383 
384 
385  /**
386  * Test whether given tag equals.
387  * The comparison is based on the unique tag.
388  *
389  * \param tag tag
390  * \return true if match; else false
391  */
392  bool equals(const JTag& tag) const
393  {
394  return this->getUniqueTag() == tag;
395  }
396 
397 
398  /**
399  * Test whether given port equals.
400  * The comparison is based on the IP number and port number.
401  *
402  * \param port port
403  * \return true if match; else false
404  */
405  bool equals(int port) const
406  {
407  return (JSYSTEM::getIPnumber() == JSYSTEM::getIPnumber(this->hostname) && port == this->port);
408  }
409 
410 
411  /**
412  * Test whether given host name equals.
413  * The comparison is based on the IP number and port number.
414  *
415  * \param hostname host name
416  * \return true if match; else false
417  */
418  bool equals(const JHostname& hostname) const
419  {
420  return (JSYSTEM::getIPnumber(hostname.hostname) == JSYSTEM::getIPnumber(this->hostname) && hostname.port == this->port);
421  }
422 
423 
424  /**
425  * Read process from input stream.
426  *
427  * \param in input stream
428  * \param object process
429  * \return input stream
430  */
431  friend inline std::istream& operator>>(std::istream& in, JDAQProcess& object)
432  {
433  in >> object.index;
434  in >> static_cast<JHostname&>(object);
435 
436  return in;
437  }
438 
439 
440  /**
441  * Write process to output stream.
442  *
443  * \param out output stream
444  * \param object process
445  * \return output stream
446  */
447  friend inline std::ostream& operator<<(std::ostream& out, const JDAQProcess& object)
448  {
449  out << object.index;
450  out << ' ';
451  out << static_cast<const JHostname&>(object);
452 
453  return out;
454  }
455 
456 
457  std::string index; //!< index in process list
458  };
459 
460 
461  /**
462  * Get name of state.
463  * Note that this method is used for backward compatibility.
464  *
465  * \param name name of state
466  * \return name of state
467  */
469  {
470  using namespace std;
471 
472  static const string target = "Operational.";
473 
474  string buffer = name;
475 
476  const string::size_type pos = buffer.find(target);
477 
478  if (pos != string::npos)
479  return buffer.erase(pos, target.size());
480  else
481  return buffer;
482  }
483 }
484 
485 #endif
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
Exceptions.
Target.
Definition: JHead.hh:298
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:51
std::string extension
file name extension
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
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:52
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.
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:38
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:53
JDAQRun(const std::string &path, const int run, const std::string &extension="root")
Constructor .
bool equals(int port) const
Test whether given port equals.
then awk string
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.
char getWildCard()
Get the wild card.
bool equals(const JTag &tag) const
Test whether given tag equals.
static const char * getPrefix()
Get file name prefix.
static const char WILD_CARD
Definition: JDAQTags.hh:50
static JDAQRun valueOf(const std::string &file_name)
Extract DAQ run parameters.
Fixed parameters and ControlHost tags for KM3NeT DAQ.
int j
Definition: JPolint.hh:703
std::string toString() const
Convert DAQ run to string.
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
bool equals(const JHostname &hostname) const
Test whether given host name equals.
Hostname and IP address functions.
Exception for I/O.
Definition: JException.hh:340