Jpp  18.0.0-rc.1
the software that should make you happy
 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 namespace std;
90  using namespace JPP;
91 
92  if (start_command != "") {
93 
94  JShell& shell = JShell::getInstance();
95 
96  shell << start_command << endl;
97  shell.flush(cout);
98  }
99  }
100 
101 
102  /**
103  * Stop process.
104  * A kill command is issued on the (remote) host.
105  * To determine the PID for the kill command, the start command is compared to
106  * the output of a ps command on the (remote) host.
107  *
108  * \param signal signal
109  */
110  void stop(const int signal = -9)
111  {
112  using namespace std;
113  using namespace JPP;
114 
115  if (start_command != "") {
116 
117  const string header = "__header___";
118  const string trailer = "__trailer__";
119 
120  ostringstream os;
121 
122  os << "ssh " << hostname << ' '
123  << "\""
124  << "echo" << ' ' << header << ";"
125  << "ps hax -o '%p %a'" << ";"
126  << "echo" << ' ' << trailer
127  << "\"";
128 
129  JShell& shell = JShell::getInstance();
130 
131  shell << os.str() << endl;
132 
133  os.str("");
134 
135  string buffer;
136 
137  while (getline(shell, buffer) && buffer.find(header) == string::npos) {}
138 
139  istringstream is;
140 
141  while (getline(shell, buffer) && buffer.find(trailer) == string::npos) {
142 
143  is.clear();
144  is.str(buffer);
145 
146  int pid;
147 
148  if (is >> pid && getline(is >> ws, buffer)) {
149  if (start_command.find(buffer) != string::npos) {
150  os << "ssh " << hostname << ' '
151  << "\""
152  << "kill " << signal << " " << pid << " </dev/null >&/dev/null"
153  << "\"";
154  }
155  }
156  }
157 
158  shell.flush();
159 
160  if (os.str() != "") {
161  shell << os.str() << endl;
162  shell.flush();
163  }
164  }
165  }
166 
167 
168  /**
169  * Get name of run control client.
170  *
171  * \return name
172  */
173  const std::string& getName() const
174  {
175  return name;
176  }
177 
178 
179  /**
180  * Get host name of run control client.
181  *
182  * \return host name
183  */
184  const std::string& getHostname() const
185  {
186  return hostname;
187  }
188 
189 
190  /**
191  * Get full name of run control client.
192  *
193  * \return full name
194  */
195  const std::string& getFullName() const
196  {
197  return full_name;
198  }
199 
200 
201  /**
202  * Get start command of run control client.
203  *
204  * \return name
205  */
207  {
208  return start_command;
209  }
210 
211 
212  /**
213  * Check sleep mode.
214  *
215  * \return true if sleep; else false
216  */
217  bool isSleep() const
218  {
219  return mode == SLEEP;
220  }
221 
222 
223  /**
224  * Check active mode.
225  *
226  * \return true if active; else false
227  */
228  bool isActive() const
229  {
230  return mode == ACTIVE;
231  }
232 
233 
234  /**
235  * Check ignore mode.
236  *
237  * \return true if ignore; else false
238  */
239  bool isIgnore() const
240  {
241  return mode == IGNORE;
242  }
243 
244 
245  /**
246  * Check illegal mode.
247  *
248  * \return true if illegal; else false
249  */
250  bool isIllegal() const
251  {
252  return mode == ILLEGAL;
253  }
254 
255 
256  /**
257  * Update client.
258  * The client parameters are updated according to the ControlHost tag and message.
259  *
260  * \param tag tag
261  * \param buffer message
262  * \return true if OK; else false
263  */
264  bool update(const JNET::JTag& tag, const std::string& buffer)
265  {
266  using namespace std;
268 
269  if (tag == JNET::DISPTAG_Born) {
270 
271  this->is_alive = true;
272  this->born += 1;
273 
274  return true;
275 
276  } else if (tag == JNET::DISPTAG_Died) {
277 
278  this->is_alive = false;
279  this->died += 1;
280 
281  return true;
282 
283  } else if (tag == RC_REPLY) {
284 
285  string key;
286  string ip;
287  string name;
288  JEvent_t event;
289  string state;
290 
291  istringstream is(buffer);
292 
293  const locale loc(is.getloc(), new JWhiteSpacesFacet(is.getloc(), TOKEN_DELIMETER));
294 
295  is.imbue(loc);
296 
297  if (is >> key >> ip >> name >> event >> state && key == RUN_CONTROL_CLIENT) {
298 
299  this->event_name = event.getName();
300  this->event_number = event.getNumber();
301  this->state_name = state;
302 
303  return true;
304  }
305  }
306 
307  return false;
308  }
309 
310 
311  /**
312  * Get name of current state.
313  *
314  * \return state name
315  */
316  const std::string& getStatename() const
317  {
318  return state_name;
319  }
320 
321 
322  /**
323  * Get name of last event.
324  *
325  * \return event name
326  */
327  const std::string& getEventname() const
328  {
329  return event_name;
330  }
331 
332 
333  /**
334  * Get number of last event.
335  *
336  * \return event number
337  */
338  int getEventnumber() const
339  {
340  return event_number;
341  }
342 
343 
344  /**
345  * Get alive status.
346  *
347  * \return true if alive; else false
348  */
349  bool getAlive() const
350  {
351  return is_alive;
352  }
353 
354 
355  /**
356  * Set alive status.
357  *
358  * \param alive alive
359  */
360  void setAlive(const bool alive)
361  {
362  is_alive = alive;
363  }
364 
365 
366  /**
367  * Get mode.
368  *
369  * \return mode
370  */
371  JMode getMode() const
372  {
373  return mode;
374  }
375 
376 
377  /**
378  * Set mode.
379  *
380  * \param mode mode
381  */
382  void setMode(const JMode mode)
383  {
384  this->mode = mode;
385  }
386 
387 
388  /**
389  * Get born count.
390  *
391  * \return number of times born
392  */
393  int getBorn() const
394  {
395  return born;
396  }
397 
398 
399  /**
400  * Get died count.
401  *
402  * \return number of times died
403  */
404  int getDied() const
405  {
406  return died;
407  }
408 
409 
410  /**
411  * Read client from input.
412  *
413  * \param in istream
414  * \param client client
415  * \return istream
416  */
417  friend inline std::istream& operator>>(std::istream& in, JClient& client)
418  {
419  in >> client.name;
420  in >> client.hostname;
421 
422  if (in) {
423 
424  getline(in >> std::ws, client.start_command);
425 
426  in.clear(); // clear error state of input stream if no start command is available.
427 
428  client.configure();
429  }
430 
431  return in;
432  }
433 
434 
435  /**
436  * Write client to output.
437  *
438  * \param out ostream
439  * \param client client
440  * \return ostream
441  */
442  friend inline std::ostream& operator<<(std::ostream& out, const JClient& client)
443  {
444  out << client.name;
445  out << ' ';
446  out << client.hostname;
447  out << ' ';
448  out << client.start_command;
449 
450  return out;
451  }
452 
453 
454  protected:
455  /**
456  * Configure client parameters.
457  * In this, the following tokens in the start command are substituted:
458  * $HOST$ by <host name>;
459  * $NAME$ by <process name>;
460  * $SERVER$ by JClient::SERVER;
461  * $LOGGER$ by JClient::LOGGER;
462  * $ARGS$ by part following last '/' in <process name>;
463  */
464  void configure()
465  {
466  using std::string;
467 
468  full_name = KM3NETDAQ::getFullName(hostname, name);
469 
470  start_command = start_command.trim();
471  start_command = start_command.replace(" ", " ");
472 
473  start_command.replace("$HOST$", hostname);
474  start_command.replace("$NAME$", name);
475  start_command.replace("$SERVER$", SERVER);
476  start_command.replace("$LOGGER$", LOGGER);
477 
478  string::size_type pos = name.find_last_of(CLIENTNAME_DELIMETER);
479 
480  if (pos != string::npos) {
481  start_command.replace("$ARGS$", name.substr(pos + 1));
482  }
483  }
484 
485 
491  int born;
492  int died;
493  bool is_alive;
497  };
498 }
499 
500 #endif
JSocketNonblockingWriter out
writer for outgoing messages
Definition: JLigier.cc:462
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:338
Exceptions.
bool isIgnore() const
Check ignore mode.
Definition: JClient.hh:239
static const JTag DISPTAG_Died("Died")
Wrapper class around STL string class.
Definition: JString.hh:27
bool update(const JNET::JTag &tag, const std::string &buffer)
Update client.
Definition: JClient.hh:264
friend std::istream & operator>>(std::istream &in, JClient &client)
Read client from input.
Definition: JClient.hh:417
const std::string & getStatename() const
Get name of current state.
Definition: JClient.hh:316
bool getAlive() const
Get alive status.
Definition: JClient.hh:349
ControlHost client manager.
Definition: JLigier.cc:243
static std::string LOGGER
host name of message logger
Definition: JClient.hh:32
int getDied() const
Get died count.
Definition: JClient.hh:404
std::string name
Definition: JClient.hh:486
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
std::string full_name
Definition: JClient.hh:489
static const std::string TOKEN_DELIMETER
Definition: JDAQTags.hh:50
std::string event_name
Definition: JClient.hh:495
void start()
Start process.
Definition: JClient.hh:87
void setMode(const JMode mode)
Set mode.
Definition: JClient.hh:382
int getBorn() const
Get born count.
Definition: JClient.hh:393
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR &dev null set_variable SERVER localhost define_variable LOGGER if do_usage *then usage $script[host[local file]] fi case set_variable LOGGER
Definition: JStopDAQ.sh:23
is
Definition: JDAQCHSM.chsm:167
JLANG::JString start_command
Definition: JClient.hh:488
bool isActive() const
Check active mode.
Definition: JClient.hh:228
friend std::ostream & operator<<(std::ostream &out, const JClient &client)
Write client to output.
Definition: JClient.hh:442
JMode
Client modi.
Definition: JClient.hh:38
static const std::string RUN_CONTROL_CLIENT
Definition: JDAQTags.hh:36
void setAlive(const bool alive)
Set alive status.
Definition: JClient.hh:360
JSocketInputChannel_t in
reader for incoming messages
Definition: JLigier.cc:461
static const JNET::JTag RC_REPLY
Definition: JDAQTags.hh:59
void stop(const int signal=-9)
Stop process.
Definition: JClient.hh:110
then echo Test string reversed by client(hit< return > to continue)." $DIR/JProcess -c "$DIR/JEcho-r" -C fi if (( 1 ))
JMode getMode() const
Get mode.
Definition: JClient.hh:371
static const std::string CLIENTNAME_DELIMETER
Definition: JDAQTags.hh:51
const std::string & getFullName() const
Get full name of run control client.
Definition: JClient.hh:195
then awk string
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
bool isSleep() const
Check sleep mode.
Definition: JClient.hh:217
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:494
static const JTag DISPTAG_Born("Born")
bool isIllegal() const
Check illegal mode.
Definition: JClient.hh:250
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:184
const std::string & getName() const
Get name of run control client.
Definition: JClient.hh:173
&set_variable SERVER
Definition: JStopDAQ.sh:29
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:487
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 and ControlHost tags for KM3NeT DAQ.
ControlHost tag.
Definition: JTag.hh:38
const std::string & getEventname() const
Get name of last event.
Definition: JClient.hh:327
std::string getFullName(const std::string &hostname, const std::string &name)
Get full name of run control client.
char * loc(char *orig)
void configure()
Configure client parameters.
Definition: JClient.hh:464
JClient()
Default constructor.
Definition: JClient.hh:44
const std::string & getStartCommand() const
Get start command of run control client.
Definition: JClient.hh:206