Belle II Software development
LogConnectionUDP.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/logging/LogConnectionUDP.h>
10#include <framework/logging/LogMessage.h>
11
12using namespace boost::asio;
13using boost::asio::ip::udp;
14
15using namespace Belle2;
16
17LogConnectionUDP::LogConnectionUDP(const std::string& hostname, unsigned short port) : m_socket(m_ioservice)
18{
19 ip::udp::resolver resolver(m_ioservice);
20 ip::udp::resolver::query udpquery(udp::v4(), hostname, std::to_string(port));
21
22 m_socket.open(ip::udp::v4());
24 m_remoteEndpoint = *resolver.resolve(udpquery);
25}
26
28{
29 return true;
30}
31
33{
34 const std::string& jsonMessage = message.toJSON(true);
35
36 boost::system::error_code err;
37 m_socket.send_to(buffer(jsonMessage, jsonMessage.size()), m_remoteEndpoint, 0, err);
38 const bool sent = err.value() != 0;
39 return sent;
40}
41
43{
44 m_socket.close();
45}
~LogConnectionUDP() override
Close the socket on destruction.
boost::asio::ip::udp::endpoint m_remoteEndpoint
the remote endpoint we send to
boost::asio::ip::udp::socket m_socket
the socket to the UDP server
bool sendMessage(const LogMessage &message) final
Send the log message as JSON to the UDP server.
bool isConnected() final
there is no way to check if a UDP connection is fine, so we just return True always
LogConnectionUDP(const std::string &hostname, unsigned short port)
Create a new UDP log connection with the hostname (as name or IP) and the port to connect to.
boost::asio::io_service m_ioservice
asio service for handling the requests
The LogMessage class.
Definition: LogMessage.h:29
Abstract base class for different kinds of events.