Jpp  18.0.1-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
monrouter.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <cassert>
4 
5 #include "version.hpp"
6 
10 #include <JDAQ/JDAQTags.hh>
11 
12 #include <boost/asio.hpp>
13 #include <boost/scoped_ptr.hpp>
14 #include <boost/program_options.hpp>
15 /**
16  * \author cpellegrino
17  */
18 
19 namespace po = boost::program_options;
20 #include "configure.hpp"
21 
22 namespace KM3NETDAQ {
23 
25 {
26  // Objects
27  boost::scoped_ptr<JControlHost> m_ch;
28  boost::asio::io_service m_service;
29  mutable boost::asio::ip::udp::socket m_input;
30  bool m_running;
31  int m_port;
32 
33  public:
34 
36  std::string const& name
37  , std::string const& server
38  , JLogger* logger
39  , const int level
40  , int port
41  )
42  :
43  JDAQClient(name, server, logger, level)
45  , m_running(false)
46  , m_port(port)
47  {
49  }
50 
51  /**
52  * Interface methods for actions corresponding to state transitions.
53  */
54  virtual void actionEnter() {}
55  virtual void actionExit() {}
56 
57  virtual void actionInit(int length, const char* buffer) {}
58  virtual void actionReset(int length, const char* buffer) {}
59 
60  virtual void actionConfigure(int length, const char* buffer)
61  {
62  boost::property_tree::ptree const conf = detail::parse(
63  std::string(buffer, length)
64  );
65 
66  m_input.open(boost::asio::ip::udp::v4());
67 
68  m_input.bind(
69  boost::asio::ip::udp::endpoint(
70  boost::asio::ip::udp::v4()
71  , m_port
72  )
73  );
74 
75  m_ch.reset(
76  new JControlHost(
77  conf.get<std::string>("out_server_address")
78  , conf.get<unsigned>("out_server_port")
79  )
80  );
81  }
82 
83  virtual void actionQuit(int length, const char* buffer)
84  {
85  m_input.close();
86  m_ch.reset();
87  }
88 
89  virtual void actionStart(int length, const char* buffer)
90  {
91  assert(getRunNumber() >= 0);
92 
93  m_running = true;
94  }
95 
96  virtual void actionStop(int length, const char* buffer)
97  {
98  m_input.close();
99  m_ch.reset();
100  }
101 
102  virtual void actionPause (int length, const char* buffer)
103  {
104  m_running = false;
105  }
106 
107  virtual void actionContinue (int length, const char* buffer)
108  {
109  m_running = true;
110  }
111 
112  virtual void actionRunning () {}
113 
114  virtual void setSelect(JFileDescriptorMask& mask) const
115  {
116  mask.set(m_input.native_handle());
117  }
118 
119  virtual void actionSelect(const JFileDescriptorMask& mask)
120  {
121  if (m_input.is_open()) {
122  if (mask.has(m_input.native_handle())) {
123  static size_t const max_size = 10000;
124  static char buffer[max_size];
125 
126  std::size_t const size = m_input.receive(
127  boost::asio::buffer(buffer, max_size)
128  );
129 
130  if (m_running) {
131  m_ch->PutFullData(IO_MONITORING_DATA, buffer, size);
132  }
133  }
134  }
135  }
136 };
137 
138 } // ns KM3NETDAQ
139 
140 int main(int argc, char* argv[])
141 {
142  std::string server("localhost");
143  std::string logger("localhost");
144  std::string client_name("MonRouter");
145  int debug = 0;
146  int port = 0;
147 
148  po::options_description desc("Options");
149  desc.add_options()
150  ("help,h", "Print this help and exit.")
151  ("version,v", "Print the version and exit.")
152  (
153  ",H"
154  , po::value<std::string>(&server)->default_value(server)
155  , "Set the address of the SM server."
156  )
157  (
158  ",M"
159  , po::value<std::string>(&logger)->default_value(logger)
160  , "Set the address of the logger server."
161  )
162  (
163  ",u"
164  , po::value<std::string>(&client_name)->default_value(client_name)
165  , "Set the address of the client name."
166  )
167  (
168  ",P"
169  , po::value<int>(&port)->required()
170  , "Set the UDP port to read data from"
171  )
172  (
173  ",d"
174  , po::value<int>(&debug)->default_value(debug)
175  , "Set the debug level."
176  );
177 
178  try {
179  po::variables_map vm;
180  po::store(
181  po::command_line_parser(argc, argv).options(desc).run(),
182  vm
183  );
184 
185  if (vm.count("help")) {
186  std::cout << desc << std::endl;
187  return EXIT_SUCCESS;
188  }
189 
190  if (vm.count("version")) {
191  std::cout << monrouter::version::v() << std::endl;
192  return EXIT_SUCCESS;
193  }
194 
195  po::notify(vm);
196  } catch (const po::error& e) {
197  std::cerr << "MonRouter: Error: " << e.what() << '\n'
198  << desc << std::endl;
199  return EXIT_FAILURE;
200  } catch (const std::runtime_error& e) {
201  std::cerr << "MonRouter: Error: " << e.what() << '\n'
202  << desc << std::endl;
203  return EXIT_FAILURE;
204  }
205 
206  // Logger for the main thread (used in JDAQClient)
208 
209  KM3NETDAQ::MonitorRouter router(client_name, server, log, debug, port);
210 
211  router.enter();
212  router.run();
213 }
int main(int argc, char *argv[])
Definition: Main.cc:15
JDAQStateMachine::ev_configure_event ev_configure
virtual void actionPause(int length, const char *buffer)
Definition: monrouter.cpp:102
ControlHost class.
virtual void actionStart(int length, const char *buffer)
Definition: monrouter.cpp:89
bool has(const int file_descriptor) const
Has file descriptor.
void set(const int file_descriptor)
Set file descriptor.
MonitorRouter(std::string const &name, std::string const &server, JLogger *logger, const int level, int port)
Definition: monrouter.cpp:35
Interface for logging messages.
Definition: JLogger.hh:22
Message logging based on ControlHost.
virtual void actionReset(int length, const char *buffer)
Definition: monrouter.cpp:58
virtual void actionRunning()
This method is repeatedly called when this client machine is in state Running and the clock interval ...
Definition: monrouter.cpp:112
std::string name
Definition: JDAQCHSM.chsm:154
void replaceEvent(const JTag &oldTag, const JTag &newTag, JDAQEvent_t &event)
Replace tag of given event in event table.
Definition: JDAQClient.hh:518
JSharedPointer< JControlHost > server
message server
Definition: JDAQClient.hh:826
virtual void actionQuit(int length, const char *buffer)
Definition: monrouter.cpp:83
void run()
Run as run control client following command messages via JNET::JControlHost.
Definition: JDAQClient.hh:687
virtual void actionEnter()
Interface methods for actions corresponding to state transitions.
Definition: monrouter.cpp:54
boost::asio::io_service m_service
Definition: monrouter.cpp:28
boost::property_tree::ptree parse(std::string str)
Definition: configure.hh:24
virtual void actionSelect(const JFileDescriptorMask &mask)
Action method following last select call.
Definition: monrouter.cpp:119
then usage $script[port]< option > nPossible options
boost::asio::ip::udp::socket m_input
Definition: monrouter.cpp:29
Auxiliary class for method select.
void store(const std::string &file_name, const JDetector &detector)
Store detector to output file.
then awk string
static const JNET::JTag RC_MONITORING_ROUTER
Definition: JDAQTags.hh:69
boost::scoped_ptr< JControlHost > m_ch
Definition: monrouter.cpp:27
virtual void actionStop(int length, const char *buffer)
Definition: monrouter.cpp:96
General purpose message reporting.
then set_variable DIR else fatal Wrong number of arguments fi for INPUT_FILE in ls rt $DIR stage * log
int getRunNumber() const
Get run number.
Definition: JDAQCHSM.chsm:100
virtual void actionExit()
Definition: monrouter.cpp:55
Control unit client base class.
Definition: JDAQClient.hh:298
virtual bool enter(const JArgs &args)
Enter the state machine.
Definition: JDAQClient.hh:389
JMessageLogger logger
message logger
Definition: JDAQClient.hh:827
Fixed parameters and ControlHost tags for KM3NeT DAQ.
static const JNET::JTag RC_CMD
Definition: JDAQTags.hh:58
virtual void setSelect(JFileDescriptorMask &mask) const
Set the file descriptor mask for the select call.
Definition: monrouter.cpp:114
data_type v[N+1][M+1]
Definition: JPolint.hh:777
static const JNET::JTag IO_MONITORING_DATA
Definition: JDAQTags.hh:83
virtual void actionInit(int length, const char *buffer)
Definition: monrouter.cpp:57
virtual void actionConfigure(int length, const char *buffer)
Definition: monrouter.cpp:60
virtual void actionContinue(int length, const char *buffer)
Definition: monrouter.cpp:107
int debug
debug level