Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JClient.hh
Go to the documentation of this file.
1 #ifndef __JRUNCONTROL__JCLIENT__
2 #define __JRUNCONTROL__JCLIENT__
3 
4 #include <string>
5 #include <iostream>
6 #include <sstream>
7 
8 #include "JLang/JException.hh"
9 #include "JLang/JString.hh"
11 #include "JDAQ/JDAQTags.hh"
13 #include "JNet/JPrefix.hh"
14 #include "JSystem/JShell.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace KM3NETDAQ {
22 
23 
24  /**
25  * Client data structure.
26  */
27  class JClient {
28  public:
29 
30 
31  static std::string SERVER; //!< host name of message server
32  static std::string LOGGER; //!< host name of message logger
33 
34 
35  /**
36  * Client modi.
37  */
38  enum JMode { UNKNOWN = -1, SLEEP = 0, ACTIVE, IGNORE = -2, ILLEGAL = -3 };
39 
40 
41  /**
42  * Default constructor.
43  */
44  JClient() :
45  name (),
46  hostname (),
47  start_command(),
48  mode(UNKNOWN),
49  born(0),
50  died(0),
51  is_alive(false),
52  state_name("?"),
53  event_name("?"),
54  event_number(-1)
55  {}
56 
57 
58  /**
59  * Constructor.
60  *
61  * \param __name name of client
62  * \param __hostname host name
63  * \param __start_command start command
64  */
65  JClient(const std::string& __name,
66  const std::string& __hostname,
67  const std::string& __start_command = "") :
68  name (__name),
69  hostname (__hostname),
70  start_command(__start_command),
71  mode(UNKNOWN),
72  born(0),
73  died(0),
74  is_alive(false),
75  state_name("?"),
76  event_name("?"),
77  event_number(-1)
78  {
79  configure();
80  }
81 
82 
83  /**
84  * Start process.
85  * The start command is issued on the (remote) host.
86  */
87  void start()
88  {
89  using JSYSTEM::JShell;
90 
91  if (start_command != "") {
92 
93  JShell& shell = JShell::getInstance();
94 
95  shell << start_command << std::endl;
96  shell.flush();
97  }
98  }
99 
100 
101  /**
102  * Stop process.
103  * A kill command is issued on the (remote) host.
104  * To determine the PID for the kill command, the start command is compared to
105  * the output of a ps command on the (remote) host.
106  *
107  * \param signal signal
108  */
109  void stop(const int signal = -9)
110  {
111  using namespace std;
112  using JSYSTEM::JShell;
113 
114  if (start_command != "") {
115 
116  const string header = "__header___";
117  const string trailer = "__trailer__";
118 
119  ostringstream os;
120 
121  os << "ssh " << hostname << ' '
122  << "\""
123  << "echo" << ' ' << header << ";"
124  << "ps hax -o '%p %a'" << ";"
125  << "echo" << ' ' << trailer
126  << "\"";
127 
128  JShell& shell = JShell::getInstance();
129 
130  shell << os.str() << endl;
131 
132  os.str("");
133 
134  string buffer;
135 
136  while (getline(shell, buffer) && buffer.find(header) == string::npos) {}
137 
138  istringstream is;
139 
140  while (getline(shell, buffer) && buffer.find(trailer) == string::npos) {
141 
142  is.clear();
143  is.str(buffer);
144 
145  int pid;
146 
147  if (is >> pid && getline(is >> ws, buffer)) {
148  if (start_command.find(buffer) != string::npos) {
149  os << "ssh " << hostname << ' '
150  << "\""
151  << "kill " << signal << " " << pid << " </dev/null >&/dev/null"
152  << "\"";
153  }
154  }
155  }
156 
157  shell.flush();
158 
159  if (os.str() != "") {
160  shell << os.str() << endl;
161  shell.flush();
162  }
163  }
164  }
165 
166 
167  /**
168  * Get name of run control client.
169  *
170  * \return name
171  */
172  const std::string& getName() const
173  {
174  return name;
175  }
176 
177 
178  /**
179  * Get host name of run control client.
180  *
181  * \return host name
182  */
183  const std::string& getHostname() const
184  {
185  return hostname;
186  }
187 
188 
189  /**
190  * Get full name of run control client.
191  *
192  * \return full name
193  */
194  const std::string& getFullName() const
195  {
196  return full_name;
197  }
198 
199 
200  /**
201  * Get start command of run control client.
202  *
203  * \return name
204  */
205  const std::string& getStartCommand() const
206  {
207  return start_command;
208  }
209 
210 
211  /**
212  * Check sleep mode.
213  *
214  * \return true if sleep; else false
215  */
216  bool isSleep() const
217  {
218  return mode == SLEEP;
219  }
220 
221 
222  /**
223  * Check active mode.
224  *
225  * \return true if active; else false
226  */
227  bool isActive() const
228  {
229  return mode == ACTIVE;
230  }
231 
232 
233  /**
234  * Check ignore mode.
235  *
236  * \return true if ignore; else false
237  */
238  bool isIgnore() const
239  {
240  return mode == IGNORE;
241  }
242 
243 
244  /**
245  * Check illegal mode.
246  *
247  * \return true if illegal; else false
248  */
249  bool isIllegal() const
250  {
251  return mode == ILLEGAL;
252  }
253 
254 
255  /**
256  * Update client.
257  * The client parameters are updated according to the ControlHost tag and message.
258  *
259  * \param tag tag
260  * \param buffer message
261  * \return true if OK; else false
262  */
263  bool update(const JNET::JTag& tag, const std::string& buffer)
264  {
265  using namespace std;
267 
268  if (tag == JNET::DISPTAG_Born) {
269 
270  this->is_alive = true;
271  this->born += 1;
272 
273  return true;
274 
275  } else if (tag == JNET::DISPTAG_Died) {
276 
277  this->is_alive = false;
278  this->died += 1;
279 
280  return true;
281 
282  } else if (tag == RC_REPLY) {
283 
284  string key;
285  string ip;
286  string name;
287  JEvent_t event;
288  string state;
289 
290  istringstream is(buffer);
291 
292  const locale loc(is.getloc(), new JWhiteSpacesFacet(is.getloc(), TOKEN_DELIMETER));
293 
294  is.imbue(loc);
295 
296  if (is >> key >> ip >> name >> event >> state && key == RUN_CONTROL_CLIENT) {
297 
298  this->event_name = event.getName();
299  this->event_number = event.getNumber();
300  this->state_name = state;
301 
302  return true;
303  }
304  }
305 
306  return false;
307  }
308 
309 
310  /**
311  * Get name of current state.
312  *
313  * \return state name
314  */
315  const std::string& getStatename() const
316  {
317  return state_name;
318  }
319 
320 
321  /**
322  * Get name of last event.
323  *
324  * \return event name
325  */
326  const std::string& getEventname() const
327  {
328  return event_name;
329  }
330 
331 
332  /**
333  * Get number of last event.
334  *
335  * \return event number
336  */
337  int getEventnumber() const
338  {
339  return event_number;
340  }
341 
342 
343  /**
344  * Get alive status.
345  *
346  * \return true if alive; else false
347  */
348  bool getAlive() const
349  {
350  return is_alive;
351  }
352 
353 
354  /**
355  * Set alive status.
356  *
357  * \param alive alive
358  */
359  void setAlive(const bool alive)
360  {
361  is_alive = alive;
362  }
363 
364 
365  /**
366  * Get mode.
367  *
368  * \return mode
369  */
370  JMode getMode() const
371  {
372  return mode;
373  }
374 
375 
376  /**
377  * Set mode.
378  *
379  * \param mode mode
380  */
381  void setMode(const JMode mode)
382  {
383  this->mode = mode;
384  }
385 
386 
387  /**
388  * Get born count.
389  *
390  * \return number of times born
391  */
392  int getBorn() const
393  {
394  return born;
395  }
396 
397 
398  /**
399  * Get died count.
400  *
401  * \return number of times died
402  */
403  int getDied() const
404  {
405  return died;
406  }
407 
408 
409  /**
410  * Read client from input.
411  *
412  * \param in istream
413  * \param client client
414  * \return istream
415  */
416  friend inline std::istream& operator>>(std::istream& in, JClient& client)
417  {
418  in >> client.name;
419  in >> client.hostname;
420 
421  if (in) {
422 
423  getline(in >> std::ws, client.start_command);
424 
425  in.clear(); // clear error state of input stream if no start command is available.
426 
427  client.configure();
428  }
429 
430  return in;
431  }
432 
433 
434  /**
435  * Write client to output.
436  *
437  * \param out ostream
438  * \param client client
439  * \return ostream
440  */
441  friend inline std::ostream& operator<<(std::ostream& out, const JClient& client)
442  {
443  out << client.name;
444  out << ' ';
445  out << client.hostname;
446  out << ' ';
447  out << client.start_command;
448 
449  return out;
450  }
451 
452 
453  protected:
454  /**
455  * Configure client parameters.
456  * In this, the following tokens in the start command are substituted:
457  * $HOST$ by <host name>;
458  * $NAME$ by <process name>;
459  * $SERVER$ by JClient::SERVER;
460  * $LOGGER$ by JClient::LOGGER;
461  * $ARGS$ by part following last '/' in <process name>;
462  */
463  void configure()
464  {
465  using std::string;
466 
468 
470  start_command = start_command.replace(" ", " ");
471 
472  start_command.replace("$HOST$", hostname);
473  start_command.replace("$NAME$", name);
474  start_command.replace("$SERVER$", SERVER);
475  start_command.replace("$LOGGER$", LOGGER);
476 
477  string::size_type pos = name.find_last_of(CLIENTNAME_DELIMETER);
478 
479  if (pos != string::npos) {
480  start_command.replace("$ARGS$", name.substr(pos + 1));
481  }
482  }
483 
484 
485  std::string name;
486  std::string hostname;
488  std::string full_name;
490  int born;
491  int died;
492  bool is_alive;
493  std::string state_name;
494  std::string event_name;
496  };
497 }
498 
499 #endif
JSocketNonblockingWriter out
writer for outgoing messages
Definition: JLigier.cc:438
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:32
int getEventnumber() const
Get number of last event.
Definition: JClient.hh:337
Exceptions.
bool isIgnore() const
Check ignore mode.
Definition: JClient.hh:238
static const JTag DISPTAG_Died("Died")
Wrapper class around STL string class.
Definition: JString.hh:28
bool update(const JNET::JTag &tag, const std::string &buffer)
Update client.
Definition: JClient.hh:263
friend std::istream & operator>>(std::istream &in, JClient &client)
Read client from input.
Definition: JClient.hh:416
const std::string & getStatename() const
Get name of current state.
Definition: JClient.hh:315
bool getAlive() const
Get alive status.
Definition: JClient.hh:348
ControlHost client manager.
Definition: JLigier.cc:241
static std::string LOGGER
host name of message logger
Definition: JClient.hh:32
int getDied() const
Get died count.
Definition: JClient.hh:403
std::string name
Definition: JClient.hh:485
Client data structure.
Definition: JClient.hh:27
std::string full_name
Definition: JClient.hh:488
Structure to store the ToT mean and standard deviation of the hits produced by a nanobeacon in a sour...
static const std::string TOKEN_DELIMETER
Definition: JDAQTags.hh:36
std::string event_name
Definition: JClient.hh:494
void start()
Start process.
Definition: JClient.hh:87
void setMode(const JMode mode)
Set mode.
Definition: JClient.hh:381
int getBorn() const
Get born count.
Definition: JClient.hh:392
JLANG::JString start_command
Definition: JClient.hh:487
bool isActive() const
Check active mode.
Definition: JClient.hh:227
friend std::ostream & operator<<(std::ostream &out, const JClient &client)
Write client to output.
Definition: JClient.hh:441
JMode
Client modi.
Definition: JClient.hh:38
static const std::string RUN_CONTROL_CLIENT
Definition: JDAQTags.hh:22
void setAlive(const bool alive)
Set alive status.
Definition: JClient.hh:359
JSocketInputChannel_t in
reader for incoming messages
Definition: JLigier.cc:437
static const JNET::JTag RC_REPLY
Definition: JDAQTags.hh:45
void stop(const int signal=-9)
Stop process.
Definition: JClient.hh:109
JMode getMode() const
Get mode.
Definition: JClient.hh:370
JString & trim()
Trim string.
Definition: JString.hh:248
static const std::string CLIENTNAME_DELIMETER
Definition: JDAQTags.hh:37
const std::string & getFullName() const
Get full name of run control client.
Definition: JClient.hh:194
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
bool isSleep() const
Check sleep mode.
Definition: JClient.hh:216
Auxiliary class for handling event name and optional number.
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition: JShell.hh:184
std::string state_name
Definition: JClient.hh:493
static const JTag DISPTAG_Born("Born")
bool isIllegal() const
Check illegal mode.
Definition: JClient.hh:249
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
Auxiliary class to specify white space character(s) in currect locale.
const std::string & getHostname() const
Get host name of run control client.
Definition: JClient.hh:183
const std::string & getName() const
Get name of run control client.
Definition: JClient.hh:172
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition: JShell.hh:76
Shell interaction via I/O streams.
std::string hostname
Definition: JClient.hh:486
JString & replace(const char target, const char replacement, const std::size_t max=std::numeric_limits< std::size_t >::max())
Replace characters.
Definition: JString.hh:189
static std::string SERVER
host name of message server
Definition: JClient.hh:31
JClient(const std::string &__name, const std::string &__hostname, const std::string &__start_command="")
Constructor.
Definition: JClient.hh:65
Fixed parameters for KM3NeT DAQ.
ControlHost tag.
Definition: JTag.hh:35
const std::string & getEventname() const
Get name of last event.
Definition: JClient.hh:326
std::string getFullName(const std::string &hostname, const std::string &name)
Get full name of run control client.
void configure()
Configure client parameters.
Definition: JClient.hh:463
JClient()
Default constructor.
Definition: JClient.hh:44
const std::string & getStartCommand() const
Get start command of run control client.
Definition: JClient.hh:205