Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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
22namespace KM3NETDAQ {
23
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 WILDCARD;
37 }
38
39
40 /**
41 * Get the event name delimeter.
42 *
43 * \return event name delimeter
44 */
46 {
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 */
71 inline std::string getProcessName(const std::string& name,
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 */
148 JDAQRun(const std::string& path,
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 */
167 JDAQRun(const std::string& path,
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 {
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 */
261 std::string toString() const
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 */
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 */
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 */
468 inline std::string getStateName(const std::string& name)
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
Fixed parameters and ControlHost tags for KM3NeT DAQ.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Hostname and IP address functions.
Exception for I/O.
ControlHost tag.
Definition JTag.hh:38
Auxiliary class for itemization of process list.
JTag getUniqueTag() const
Get unique tag.
friend std::istream & operator>>(std::istream &in, JDAQProcess &object)
Read process from input stream.
bool equals(const JTag &tag) const
Test whether given tag equals.
JDAQProcess()
Default constructor.
bool equals(const JHostname &hostname) const
Test whether given host name equals.
friend std::ostream & operator<<(std::ostream &out, const JDAQProcess &object)
Write process to output stream.
bool equals(int port) const
Test whether given port equals.
std::string index
index in process list
JDAQProcess(const JHostname &hostname, const std::string &key="")
Constructor.
Auxiliary classes and methods for operating system calls.
int getIPnumber()
Get IP number.
Definition JNetwork.hh:142
std::string getIPaddress()
Get IP address (decimal-dot notation).
Definition JNetwork.hh:178
unsigned short getSubaddress()
Get host identifier within network.
Definition JNetwork.hh:205
return result
Definition JPolint.hh:862
int j
Definition JPolint.hh:801
KM3NeT DAQ data structures and auxiliaries.
Definition DataQueue.cc:39
std::string getFullName(const std::string &hostname, const std::string &name)
Get full name of run control client.
static const std::string RUN_CONTROL_CLIENT
Definition JDAQTags.hh:44
static const std::string TOKEN_DELIMETER
Definition JDAQTags.hh:58
JTag getUniqueTag(const std::string &hostname, const std::string &name)
Get unique tag of run control client.
static const char WILDCARD
Definition JDAQTags.hh:56
std::string getStateName(const std::string &name)
Get name of state.
static const char EVENTNAME_DELIMETER
Definition JDAQTags.hh:57
std::string getProcessName(const std::string &name, const std::string &process)
Get process name of run control client.
char getEventnameDelimeter()
Get the event name delimeter.
char getWildCard()
Get the wild card.
char getTokenDelimeter()
Get the token delimeter for command messages.
static const std::string CLIENTNAME_DELIMETER
Definition JDAQTags.hh:59
Target.
Definition JHead.hh:300
Auxiliary data structure for hostname and port number.
Definition JHostname.hh:35
std::string hostname
Definition JHostname.hh:171
Simple data structure for DAQ run.
int unique_id
unique identifier
JDAQRun(const char *file_name)
Constructor.
static JDAQRun valueOf(const std::string &file_name)
Extract DAQ run parameters.
static const char * getPrefix()
Get file name prefix.
std::string path
directory path
std::string extension
file name extension
JDAQRun()
Default constructor .
JDAQRun(const std::string &path, const int unique_id, const int run, const std::string &extension="root")
Constructor .
std::string toString() const
Convert DAQ run to string.
JDAQRun(const std::string &path, const int run, const std::string &extension="root")
Constructor .
static std::string getFilename(const std::string &path, const int unique_id, const int run)
Get file name of run.