Jpp  18.4.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDAQDemoClient.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 #include <sys/types.h>
6 #include <unistd.h>
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 #include "Jeep/JArgs.hh"
11 #include "JSystem/JShell.hh"
12 
14 
15 
16 using namespace KM3NETDAQ;
17 
18 
19 /**
20  * \file
21  * Run control demo client class.
22  *
23  * The various action methods could be implemented in this class.
24  * Note that process with extension /- will sleep and kill itself at event <tt>ev_configure</tt> according the specified inputs.
25  * Note that process with extension /+ will sleep forever during the <tt>exit</tt> transition.
26  * \author mdejong
27  */
29  public JDAQClient
30 {
31 public:
32  /**
33  * Constructor.
34  *
35  * \param name name of client
36  * \param server name of command message server
37  * \param logger pointer to logger
38  * \param level debug level
39  */
41  const std::string& server,
42  JLogger* logger,
43  const int level) :
44  JDAQClient(name, server, logger, level)
45  {}
46 
47  /**
48  * Constructor.
49  *
50  * \param name name of client
51  * \param logger pointer to logger
52  * \param level debug level
53  */
55  JLogger* logger,
56  const int level) :
57  JDAQClient(name, logger, level)
58  {}
59 
60  /**
61  * Constructor.
62  *
63  * \param name name of client
64  */
66  JDAQClient(name)
67  {}
68 
69  virtual void actionEnter()
70  {
71  logger.debug("actionEnter()");
72  }
73 
74  virtual void actionExit()
75  {
76  logger.debug("actionExit()");
77 
78  if (getName() == "JDAQDemoClient/+") {
79 
80  // sleep forever
81 
82  sleep(std::numeric_limits<unsigned int>::max());
83  }
84  }
85 
86  virtual void actionInit(int length, const char* buffer)
87  {
88  using namespace std;
89  using namespace JPP;
90 
91  JDebugStream(logger) << "actionInit(" << std::string(buffer,length) << ")";
92 
93  if (string(buffer,length) == ev_error.name()) {
94  ev_error();
95  }
96  }
97 
98  virtual void actionConfigure(int length, const char* buffer)
99  {
100  using namespace std;
101  using namespace JPP;
102 
103  JDebugStream(logger) << "actionConfigure(" << std::string(buffer,length) << ")";
104 
105  if (getName() == "JDAQDemoClient/-") {
106 
107  istringstream is(string(buffer,length));
108 
109  int us1 = 0, us2 = 0;
110 
111  if (is >> us1 >> us2 && us1 > 0 && us2 > 0) {
112 
113  // sleep and terminate process
114 
115  JErrorStream(logger) << "Time to sleep/kill " << us1 << "/" << us2 << " us";
116 
117  JShell& shell = JShell::getInstance();
118 
119  shell << "(usleep " << us2 << "; kill -9 " << getpid() << ") &" << endl;
120 
121  usleep(us1);
122  }
123  }
124  }
125 
126  virtual void actionStart(int length, const char* buffer)
127  {
128  JDebugStream(logger) << "actionStart(" << std::string(buffer,length) << ")";
129  }
130 
131  virtual void actionPause(int length, const char* buffer)
132  {
133  JDebugStream(logger) << "actionPause(" << std::string(buffer,length) << ")";
134  }
135 
136  virtual void actionContinue(int length, const char* buffer)
137  {
138  JDebugStream(logger) << "actionContinue(" << std::string(buffer,length) << ")";
139  }
140 
141  virtual void actionStop(int length, const char* buffer)
142  {
143  JDebugStream(logger) << "actionStop(" << std::string(buffer,length) << ")";
144  }
145 
146  virtual void actionReset(int length, const char* buffer)
147  {
148  JDebugStream(logger) << "actionReset(" << std::string(buffer,length) << ")";
149  }
150 
151  virtual void actionQuit(int length, const char* buffer)
152  {
153  JDebugStream(logger) << "actionQuit(" << std::string(buffer,length) << ")";
154  }
155 };
156 
157 
158 /**
159  * Run control demo client.
160  */
161 int main(int argc, char* argv[])
162 {
163  using namespace std;
164  using namespace JPP;
165  using namespace KM3NETDAQ;
166 
167  string server;
168  string logger;
169  string client_name;
170  bool use_cout;
171  int debug;
172 
173  string file_name;
174  int port;
175 
176  try {
177 
178  JParser<> zap("Application for writing real-time data to disk.");
179 
180  zap['H'] = make_field(server) = "localhost";
181  zap['M'] = make_field(logger) = "localhost";
182  zap['u'] = make_field(client_name) = "%";
183  zap['c'] = make_field(use_cout);
184  zap['d'] = make_field(debug) = 3;
185 
186  zap['f'] = make_field(file_name) = "";
187  zap['P'] = make_field(port) = -1;
188 
189  zap(argc, argv);
190  }
191  catch(const exception &error) {
192  FATAL(error.what() << endl);
193  }
194 
195  if (port == -1) {
196 
197  JLogger* out = NULL;
198 
199  if (use_cout)
200  out = new JStreamLogger(cout);
201  else
202  out = new JControlHostLogger(logger);
203 
204  if (file_name == "") {
205 
206  JDAQDemoClient demo(getProcessName(client_name, argv[0]), server, out, debug);
207 
208  demo.enter();
209  demo.run();
210 
211  } else {
212 
213  JDAQDemoClient demo(getProcessName(client_name, argv[0]), out, debug);
214 
215  if (debug >= debug_t && use_cout) {
216  demo.debug(CHSM::machine::D_all);
217  }
218  demo.CHSM::machine::enter();
219 
220  ifstream in(file_name.c_str());
221 
222  demo.run(in);
223 
224  in.close();
225  }
226 
227  } else {
228 
229  JDAQDemoClient demo(getProcessName(client_name, argv[0]));
230 
231  demo.run(port);
232  }
233 }
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:32
JDAQDemoClient(const std::string &name)
Constructor.
Utility class to parse command line options.
Definition: JParser.hh:1514
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
virtual void actionEnter()
Interface methods for actions corresponding to state transitions.
std::string getProcessName(const std::string &name, const std::string &process)
Get process name of run control client.
Message logging based on std::ostream.
Interface for logging messages.
Definition: JLogger.hh:22
Message logging based on ControlHost.
then echo Enter input within $TIMEOUT_S seconds echo n User name
Definition: JCookie.sh:42
virtual void actionExit()
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:690
virtual void actionQuit(int length, const char *buffer)
is
Definition: JDAQCHSM.chsm:167
virtual void actionConfigure(int length, const char *buffer)
virtual void actionInit(int length, const char *buffer)
virtual void actionPause(int length, const char *buffer)
JDAQDemoClient(const std::string &name, const std::string &server, JLogger *logger, const int level)
Constructor.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
then awk string
Level specific message streamers.
General purpose messaging.
virtual void actionStart(int length, const char *buffer)
#define FATAL(A)
Definition: JMessage.hh:67
virtual void actionContinue(int length, const char *buffer)
JDAQDemoClient(const std::string &name, JLogger *logger, const int level)
Constructor.
Control unit client base class.
Definition: JDAQClient.hh:298
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:48
Utility class to parse command line options.
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition: JShell.hh:76
Shell interaction via I/O streams.
virtual void actionReset(int length, const char *buffer)
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:392
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:60
virtual void actionStop(int length, const char *buffer)
int debug
debug level