1 #define TRACE_NAME "UDP_Receiver"
3 #include "mfextensions/Receivers/UDP_receiver.hh"
6 #include "messagefacility/Utilities/ELseverityLevel.h"
7 #include "mfextensions/Receivers/ReceiverMacros.hh"
11 :
MVReceiver(pset), message_port_(pset.get<int>(
"port", 5140)), message_addr_(pset.get<std::string>(
"message_address",
"227.128.12.27")), multicast_enable_(pset.get<bool>(
"multicast_enable", false)), multicast_out_addr_(pset.get<std::string>(
"multicast_interface_ip",
"0.0.0.0")), message_socket_(-1)
13 TLOG(TLVL_TRACE) <<
"UDPReceiver Constructor";
16 void mfviewer::UDPReceiver::setupMessageListener_()
18 TLOG(TLVL_INFO) <<
"Setting up message listen socket, address=" << message_addr_ <<
":" << message_port_;
19 message_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
20 if (message_socket_ < 0)
22 TLOG(TLVL_ERROR) <<
"Error creating socket for receiving messages! err=" << strerror(errno);
26 struct sockaddr_in si_me_request;
29 if (setsockopt(message_socket_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0)
31 TLOG(TLVL_ERROR) <<
"Unable to enable port reuse on message socket, err=" << strerror(errno);
34 memset(&si_me_request, 0,
sizeof(si_me_request));
35 si_me_request.sin_family = AF_INET;
36 si_me_request.sin_port = htons(message_port_);
37 si_me_request.sin_addr.s_addr = htonl(INADDR_ANY);
38 if (bind(message_socket_, (
struct sockaddr*)&si_me_request,
sizeof(si_me_request)) == -1)
40 TLOG(TLVL_ERROR) <<
"Cannot bind message socket to port " << message_port_ <<
", err=" << strerror(errno);
44 if (message_addr_ !=
"localhost" && multicast_enable_)
47 int sts =
ResolveHost(message_addr_.c_str(), mreq.imr_multiaddr);
50 TLOG(TLVL_ERROR) <<
"Unable to resolve multicast message address, err=" << strerror(errno);
56 TLOG(TLVL_ERROR) <<
"Unable to resolve hostname for " << multicast_out_addr_;
59 if (setsockopt(message_socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) < 0)
61 TLOG(TLVL_ERROR) <<
"Unable to join multicast group, err=" << strerror(errno);
65 TLOG(TLVL_INFO) <<
"Done setting up message receive socket";
70 TLOG(TLVL_DEBUG) <<
"Closing message receive socket";
71 close(message_socket_);
77 while (!stopRequested_)
79 if (message_socket_ == -1) setupMessageListener_();
82 struct pollfd ufds[1];
83 ufds[0].fd = message_socket_;
84 ufds[0].events = POLLIN | POLLPRI | POLLERR;
85 int rv = poll(ufds, 1, ms_to_wait);
88 if (rv <= 0 || (ufds[0].revents != POLLIN && ufds[0].revents != POLLPRI))
90 if (rv == 1 && (ufds[0].revents & (POLLNVAL | POLLERR | POLLHUP)))
92 close(message_socket_);
102 char buffer[TRACE_STREAMER_MSGMAX + 1];
103 auto packetSize = read(message_socket_, &buffer, TRACE_STREAMER_MSGMAX);
106 TLOG(TLVL_ERROR) <<
"Error receiving message, errno=" << errno <<
" (" << strerror(errno) <<
")";
110 TLOG(TLVL_TRACE) <<
"Recieved message; validating...(packetSize=" << packetSize <<
")";
111 std::string message(buffer, buffer + packetSize);
112 if (validate_packet(message))
114 TLOG(TLVL_TRACE) <<
"Valid UDP Message received! Sending to GUI!";
115 emit NewMessage(read_msg(message));
119 TLOG(TLVL_INFO) <<
"UDPReceiver shutting down!";
122 std::list<std::string> mfviewer::UDPReceiver::tokenize_(std::string
const& input)
125 std::list<std::string> output;
127 while (pos != std::string::npos && pos < input.size())
129 auto newpos = input.find(
'|', pos);
130 if (newpos != std::string::npos)
132 output.emplace_back(input, pos, newpos - pos);
138 output.emplace_back(input, pos);
148 std::string hostname, category, application, message, hostaddr, file, line, module, eventID;
149 mf::ELseverityLevel sev;
154 TLOG(TLVL_TRACE) <<
"Recieved MF/Syslog message with contents: " << input;
156 auto tokens = tokenize_(input);
157 auto it = tokens.begin();
159 if (it != tokens.end())
161 bool timestamp_found =
false;
164 while (it != tokens.end() && !timestamp_found)
166 std::string thisString = *it;
167 while (thisString.size() > 0 && !timestamp_found)
169 auto pos = thisString.find_first_of(
"0123456789");
170 if (pos != std::string::npos)
172 thisString = thisString.erase(0, pos);
175 if (strptime(thisString.c_str(),
"%d-%b-%Y %H:%M:%S", &tm) != NULL)
177 timestamp_found =
true;
181 if (thisString.size() > 0)
182 thisString = thisString.erase(0, 1);
196 if (it != tokens.end() && ++it != tokens.end() )
198 seqNum = std::stoi(*it);
201 catch (
const std::invalid_argument& e)
205 if (it != tokens.end() && ++it != tokens.end() )
209 if (it != tokens.end() && ++it != tokens.end() )
213 if (it != tokens.end() && ++it != tokens.end() )
215 sev = mf::ELseverityLevel(*it);
217 if (it != tokens.end() && ++it != tokens.end() )
221 if (it != tokens.end() && ++it != tokens.end() )
228 if (it != tokens.end() && ++it != tokens.end() )
230 pid = std::stol(*it);
233 catch (
const std::invalid_argument& e)
237 if (it != tokens.end() && ++it != tokens.end() )
241 if (it != tokens.end() && ++it != tokens.end() )
245 if (it != tokens.end() && ++it != tokens.end() )
249 if (it != tokens.end() && ++it != tokens.end() )
253 std::ostringstream oss;
255 while (it != tokens.end() && ++it != tokens.end() )
267 TLOG(TLVL_TRACE) <<
"Message content: " << oss.str();
271 qt_mf_msg msg(hostname, category, application, pid, tv);
273 msg.
setMessage(
"UDPMessage", seqNum, message);
287 if (input.find(
"MF") == std::string::npos)
289 TLOG(TLVL_WARNING) <<
"Failed to find \"MF\" in message: " << input;
292 if (input.find(
"|") == std::string::npos)
294 TLOG(TLVL_WARNING) <<
"Failed to find | separator character in message: " << input;
300 #include "moc_UDP_receiver.cpp"
void updateText()
Parse fields and create HTML string representing message
void setEventID(std::string eventID)
Set the Event ID of the message
void run() override
Receiver method. Receive messages and emit NewMessage signal
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
static bool validate_packet(std::string input)
Run simple validation tests on message
void setFileName(std::string file)
Set the file name field
Receive messages through a UDP socket. Expects the syslog format provided by UDP_mfPlugin (ELUDP) ...
UDPReceiver(fhicl::ParameterSet pset)
UDPReceiver Constructor
Qt wrapper around MessageFacility message
int GetInterfaceForNetwork(char const *host_in, in_addr &addr)
Convert an IP address to the network address of the interface sharing the subnet mask.
void setMessage(std::string prefix, int iteration, std::string msg)
Set the message
virtual ~UDPReceiver()
Destructor – Close socket
A MVReceiver class listens for messages and raises a signal when one arrives
void setSeverity(mf::ELseverityLevel sev)
Set the Severity of the message (MF levels)
void setHostAddr(std::string hostaddr)
Set the hostaddr field
void setLineNumber(std::string line)
Set the line number field
qt_mf_msg read_msg(std::string input)
Parse incoming message
void setModule(std::string module)
Set the module name