1 #include "mfextensions/Receivers/UDP_receiver.hh"
2 #include "mfextensions/Receivers/ReceiverMacros.hh"
3 #include <boost/tokenizer.hpp>
4 #include <boost/regex.hpp>
7 mfviewer::UDPReceiver::UDPReceiver(fhicl::ParameterSet pset) : MVReceiver(pset)
8 , port_(pset.get<int>(
"port", 5140))
10 , socket_(io_service_)
11 , debug_(pset.get<bool>(
"debug_mode", false))
14 boost::system::error_code ec;
15 udp::endpoint listen_endpoint(boost::asio::ip::address::from_string(
"0.0.0.0"), port_);
16 socket_.open(listen_endpoint.protocol());
17 boost::asio::socket_base::reuse_address option(
true);
18 socket_.set_option(option, ec);
21 std::cerr <<
"An error occurred setting reuse_address: " << ec.message() << std::endl;
23 socket_.bind(listen_endpoint, ec);
27 std::cerr <<
"An error occurred in bind(): " << ec.message() << std::endl;
30 if (pset.get<
bool>(
"multicast_enable",
false))
32 std::string multicast_address_string = pset.get<std::string>(
"multicast_address",
"227.128.12.27");
33 auto multicast_address = boost::asio::ip::address::from_string(multicast_address_string);
34 boost::asio::ip::multicast::join_group group_option(multicast_address);
35 socket_.set_option(group_option, ec);
38 std::cerr <<
"An error occurred joining the multicast group " << multicast_address_string
39 <<
": " << ec.message() << std::endl;
42 boost::asio::ip::multicast::enable_loopback loopback_option(
true);
43 socket_.set_option(loopback_option, ec);
47 std::cerr <<
"An error occurred setting the multicast loopback option: " << ec.message() << std::endl;
50 if (debug_) { std::cout <<
"UDPReceiver Constructor Done" << std::endl; }
53 mfviewer::UDPReceiver::~UDPReceiver()
55 if (debug_) { std::cout <<
"Closing UDP Socket" << std::endl; }
59 void mfviewer::UDPReceiver::run()
61 while (!stopRequested_)
63 if (socket_.available() <= 0)
70 udp::endpoint remote_endpoint;
71 boost::system::error_code ec;
72 size_t packetSize = socket_.receive_from(boost::asio::buffer(buffer_), remote_endpoint, 0, ec);
73 if (debug_) { std::cout <<
"Recieved message; validating...(packetSize=" << packetSize <<
")" << std::endl; }
74 std::string message(buffer_, buffer_ + packetSize);
77 std::cerr <<
"Recieved error code: " << ec.message() << std::endl;
79 else if (packetSize > 0 && validate_packet(message))
81 if (debug_) { std::cout <<
"Valid UDP Message received! Sending to GUI!" << std::endl; }
82 emit NewMessage(read_msg(message));
87 std::cout <<
"UDPReceiver shutting down!" << std::endl;
90 mf::MessageFacilityMsg mfviewer::UDPReceiver::read_msg(std::string input)
92 mf::MessageFacilityMsg msg;
93 if (debug_) { std::cout <<
"Recieved MF/Syslog message with contents: " << input << std::endl; }
95 boost::char_separator<char> sep(
"|",
"",boost::keep_empty_tokens);
96 typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
97 tokenizer tokens(input, sep);
98 tokenizer::iterator it = tokens.begin();
101 boost::regex timestamp(
".*?(\\d{2}-[^-]*-\\d{4}\\s\\d{2}:\\d{2}:\\d{2})");
103 while (it != tokens.end() && !boost::regex_search(*it, res, timestamp))
108 if (it != tokens.end())
113 std::string value(res[1].first, res[1].second);
114 strptime(value.c_str(),
"%d-%b-%Y %H:%M:%S", &tm);
119 msg.setTimestamp(tv);
125 if (++it != tokens.end()) { seqNum = std::stoi(*it); }
127 catch (std::invalid_argument e) { it = prevIt; }
128 if (++it != tokens.end()) { msg.setHostname(*it); }
129 if (++it != tokens.end()) { msg.setHostaddr(*it); }
130 if (++it != tokens.end()) { msg.setSeverity(*it); }
131 if (++it != tokens.end()) { msg.setCategory(*it); }
132 if (++it != tokens.end()) { msg.setApplication(*it); }
133 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
134 if (++it != tokens.end()) { msg.setProcess(*it); }
139 if (++it != tokens.end()) { msg.setPid(std::stol(*it)); }
141 catch (std::invalid_argument e) { it = prevIt; }
142 if (++it != tokens.end()) { msg.setContext(*it); }
143 if (++it != tokens.end()) { msg.setModule(*it); }
144 std::ostringstream oss;
146 while (++it != tokens.end())
148 if (!first) { oss <<
"|"; }
149 else { first =
false; }
152 if (debug_) { std::cout <<
"Message content: " << oss.str() << std::endl; }
153 msg.setMessage(std::string(
"UDPMessage"), std::to_string(seqNum), oss.str());
159 bool mfviewer::UDPReceiver::validate_packet(std::string input)
162 if (input.find(
"MF") == std::string::npos)
164 std::cout <<
"Failed to find \"MF\" in message: " << input << std::endl;
167 if (input.find(
"|") == std::string::npos)
169 std::cout <<
"Failed to find | separator character in message: " << input << std::endl;
175 #include "moc_UDP_receiver.cpp"
Receive messages through a UDP socket. Expects the syslog format provided by UDP_mfPlugin (ELUDP) ...