#include #include #include #include "version.hpp" #include #include #include #include #include #include #include /** * \author cpellegrino */ namespace po = boost::program_options; #include "configure.hpp" namespace KM3NETDAQ { class MonitorRouter: public JDAQClient { // Objects boost::scoped_ptr m_ch; boost::asio::io_service m_service; mutable boost::asio::ip::udp::socket m_input; bool m_running; int m_port; public: MonitorRouter( std::string const& name , std::string const& server , JLogger* logger , const int level , int port ) : JDAQClient(name, server, logger, level) , m_input(m_service) , m_running(false) , m_port(port) { replaceEvent(RC_CMD, RC_MONITORING_ROUTER, ev_configure); } /** * Interface methods for actions corresponding to state transitions. */ virtual void actionEnter() {} virtual void actionExit() {} virtual void actionInit(int length, const char* buffer) {} virtual void actionReset(int length, const char* buffer) {} virtual void actionConfigure(int length, const char* buffer) { boost::property_tree::ptree const conf = detail::parse( std::string(buffer, length) ); m_input.open(boost::asio::ip::udp::v4()); m_input.bind( boost::asio::ip::udp::endpoint( boost::asio::ip::udp::v4() , m_port ) ); m_ch.reset( new JControlHost( conf.get("out_server_address") , conf.get("out_server_port") ) ); } virtual void actionQuit(int length, const char* buffer) { m_input.close(); m_ch.reset(); } virtual void actionStart(int length, const char* buffer) { assert(getRunNumber() >= 0); m_running = true; } virtual void actionStop(int length, const char* buffer) { m_input.close(); m_ch.reset(); } virtual void actionPause (int length, const char* buffer) { m_running = false; } virtual void actionContinue (int length, const char* buffer) { m_running = true; } virtual void actionRunning () {} virtual void setSelect(JFileDescriptorMask& mask) const { mask.set(m_input.native_handle()); } virtual void actionSelect(const JFileDescriptorMask& mask) { if (m_input.is_open()) { if (mask.has(m_input.native_handle())) { static size_t const max_size = 10000; static char buffer[max_size]; std::size_t const size = m_input.receive( boost::asio::buffer(buffer, max_size) ); if (m_running) { m_ch->PutFullData(IO_MONITORING_DATA, buffer, size); } } } } }; } // ns KM3NETDAQ int main(int argc, char* argv[]) { std::string server("localhost"); std::string logger("localhost"); std::string client_name("MonRouter"); int debug = 0; int port = 0; po::options_description desc("Options"); desc.add_options() ("help,h", "Print this help and exit.") ("version,v", "Print the version and exit.") ( ",H" , po::value(&server)->default_value(server) , "Set the address of the SM server." ) ( ",M" , po::value(&logger)->default_value(logger) , "Set the address of the logger server." ) ( ",u" , po::value(&client_name)->default_value(client_name) , "Set the address of the client name." ) ( ",P" , po::value(&port)->required() , "Set the UDP port to read data from" ) ( ",d" , po::value(&debug)->default_value(debug) , "Set the debug level." ); try { po::variables_map vm; po::store( po::command_line_parser(argc, argv).options(desc).run(), vm ); if (vm.count("help")) { std::cout << desc << std::endl; return EXIT_SUCCESS; } if (vm.count("version")) { std::cout << monrouter::version::v() << std::endl; return EXIT_SUCCESS; } po::notify(vm); } catch (const po::error& e) { std::cerr << "MonRouter: Error: " << e.what() << '\n' << desc << std::endl; return EXIT_FAILURE; } catch (const std::runtime_error& e) { std::cerr << "MonRouter: Error: " << e.what() << '\n' << desc << std::endl; return EXIT_FAILURE; } // Logger for the main thread (used in JDAQClient) JLOGGER::JControlHostLogger* log = new JLOGGER::JControlHostLogger(logger); KM3NETDAQ::MonitorRouter router(client_name, server, log, debug, port); router.enter(); router.run(); }