Jpp
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 non-empty data at event ev_configure will make the process prematurely exit.
25  * Note that process with extension /+ will sleep forever during the exit 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  */
40  JDAQDemoClient(const std::string& name,
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  */
54  JDAQDemoClient(const std::string& name,
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  */
65  JDAQDemoClient(const std::string& name) :
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  istringstream is(string(buffer,length));
106 
107  int us1 = 0, us2 = 0;
108 
109  if (is >> us1 >> us2) {
110 
111  // terminate process
112 
113  JErrorStream(logger) << "Time to sleep/kill " << us1 << "/" << us2 << " us";
114 
115  JShell& shell = JShell::getInstance();
116 
117  shell << "(usleep " << us2 << "; kill -9 " << getpid() << ") &" << endl;
118 
119  usleep(us1);
120  }
121  }
122 
123  virtual void actionStart(int length, const char* buffer)
124  {
125  JDebugStream(logger) << "actionStart(" << std::string(buffer,length) << ")";
126  }
127 
128  virtual void actionPause(int length, const char* buffer)
129  {
130  JDebugStream(logger) << "actionPause(" << std::string(buffer,length) << ")";
131  }
132 
133  virtual void actionContinue(int length, const char* buffer)
134  {
135  JDebugStream(logger) << "actionContinue(" << std::string(buffer,length) << ")";
136  }
137 
138  virtual void actionStop(int length, const char* buffer)
139  {
140  JDebugStream(logger) << "actionStop(" << std::string(buffer,length) << ")";
141  }
142 
143  virtual void actionReset(int length, const char* buffer)
144  {
145  JDebugStream(logger) << "actionReset(" << std::string(buffer,length) << ")";
146  }
147 
148  virtual void actionQuit(int length, const char* buffer)
149  {
150  JDebugStream(logger) << "actionQuit(" << std::string(buffer,length) << ")";
151  }
152 };
153 
154 
155 /**
156  * Run control demo client.
157  */
158 int main(int argc, char* argv[])
159 {
160  using namespace std;
161  using namespace JPP;
162  using namespace KM3NETDAQ;
163 
164  string server;
165  string logger;
166  string client_name;
167  bool use_cout;
168  int debug;
169 
170  string file_name;
171  int port;
172 
173  try {
174 
175  JParser<> zap("Application for writing real-time data to disk.");
176 
177  zap['H'] = make_field(server) = "localhost";
178  zap['M'] = make_field(logger) = "localhost";
179  zap['u'] = make_field(client_name) = "%";
180  zap['c'] = make_field(use_cout);
181  zap['d'] = make_field(debug) = 3;
182 
183  zap['f'] = make_field(file_name) = "";
184  zap['P'] = make_field(port) = -1;
185 
186  zap(argc, argv);
187  }
188  catch(const exception &error) {
189  FATAL(error.what() << endl);
190  }
191 
192  if (port == -1) {
193 
194  JLogger* out = NULL;
195 
196  if (use_cout)
197  out = new JStreamLogger(cout);
198  else
199  out = new JControlHostLogger(logger);
200 
201  if (file_name == "") {
202 
203  JDAQDemoClient demo(getProcessName(client_name, argv[0]), server, out, debug);
204 
205  demo.enter();
206  demo.run();
207 
208  } else {
209 
210  JDAQDemoClient demo(getProcessName(client_name, argv[0]), out, debug);
211 
212  if (debug >= debug_t && use_cout) {
213  demo.debug(CHSM::machine::D_all);
214  }
215  demo.CHSM::machine::enter();
216 
217  ifstream in(file_name.c_str());
218 
219  demo.run(in);
220 
221  in.close();
222  }
223 
224  } else {
225 
226  JDAQDemoClient demo(getProcessName(client_name, argv[0]));
227 
228  demo.run(port);
229  }
230 }
JLOGGER::JDebugStream
Level specific message streamers.
Definition: JMessageStream.hh:113
JLOGGER::JLogger
Interface for logging messages.
Definition: JLogger.hh:22
JDAQDemoClient::actionStart
virtual void actionStart(int length, const char *buffer)
Definition: JDAQDemoClient.cc:123
JDAQDemoClient::actionContinue
virtual void actionContinue(int length, const char *buffer)
Definition: JDAQDemoClient.cc:133
JDAQDemoClient::actionReset
virtual void actionReset(int length, const char *buffer)
Definition: JDAQDemoClient.cc:143
JDAQDemoClient::JDAQDemoClient
JDAQDemoClient(const std::string &name)
Constructor.
Definition: JDAQDemoClient.cc:65
JMessage.hh
JSYSTEM::JShell
The JShell clas can be used to interact with the shell via I/O streams.
Definition: JShell.hh:32
JDAQDemoClient::actionInit
virtual void actionInit(int length, const char *buffer)
Definition: JDAQDemoClient.cc:86
JLOGGER::JErrorStream
Definition: JMessageStream.hh:115
JDAQDemoClient::JDAQDemoClient
JDAQDemoClient(const std::string &name, const std::string &server, JLogger *logger, const int level)
Constructor.
Definition: JDAQDemoClient.cc:40
JDAQDemoClient::actionStop
virtual void actionStop(int length, const char *buffer)
Definition: JDAQDemoClient.cc:138
KM3NETDAQ::JDAQClient::run
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:661
JDAQDemoClient::actionConfigure
virtual void actionConfigure(int length, const char *buffer)
Definition: JDAQDemoClient.cc:98
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
main
int main(int argc, char *argv[])
Run control demo client.
Definition: JDAQDemoClient.cc:158
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JArgs.hh
JDAQDemoClient::actionQuit
virtual void actionQuit(int length, const char *buffer)
Definition: JDAQDemoClient.cc:148
debug
int debug
debug level
Definition: JSirene.cc:59
JLOGGER::JStreamLogger
Message logging based on std::ostream.
Definition: JStreamLogger.hh:22
JDAQDemoClient::actionExit
virtual void actionExit()
Definition: JDAQDemoClient.cc:74
JDAQDemoClient::actionPause
virtual void actionPause(int length, const char *buffer)
Definition: JDAQDemoClient.cc:128
KM3NETDAQ::JDAQClient
Control unit client base class.
Definition: JDAQClient.hh:272
JLOGGER::JControlHostLogger
Message logging based on ControlHost.
Definition: JControlHostLogger.hh:26
JParser.hh
JDAQDemoClient::JDAQDemoClient
JDAQDemoClient(const std::string &name, JLogger *logger, const int level)
Constructor.
Definition: JDAQDemoClient.cc:54
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JROOT::getName
const char * getName()
Get ROOT name of given data type.
Definition: JRootToolkit.hh:45
std
Definition: jaanetDictionary.h:36
JDAQDemoClient::actionEnter
virtual void actionEnter()
Interface methods for actions corresponding to state transitions.
Definition: JDAQDemoClient.cc:69
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
JSYSTEM::JShell::getInstance
static JShell & getInstance()
Get reference to unique instance of this class object.
Definition: JShell.hh:76
JShell.hh
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JEEP::debug_t
debug
Definition: JMessage.hh:29
KM3NETDAQ::getProcessName
std::string getProcessName(const std::string &name, const std::string &process)
Get process name of run control client.
Definition: JRuncontrolToolkit.hh:194
JDAQDemoClient
Definition: JDAQDemoClient.cc:28
KM3NETDAQ::JDAQClient::enter
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:363
JDAQClient.hh