Belle II Software  release-05-02-19
LogConnectionUDP.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/logging/LogConnectionUDP.h>
12 #include <framework/logging/LogMessage.h>
13 
14 using namespace boost::asio;
15 using boost::asio::ip::udp;
16 
17 using namespace Belle2;
18 
19 LogConnectionUDP::LogConnectionUDP(const std::string& hostname, unsigned short port) : m_socket(m_ioservice)
20 {
21  ip::udp::resolver resolver(m_ioservice);
22  ip::udp::resolver::query query(udp::v4(), hostname, std::to_string(port));
23 
24  m_socket.open(ip::udp::v4());
26  m_remoteEndpoint = *resolver.resolve(query);
27 }
28 
30 {
31  return true;
32 }
33 
35 {
36  const std::string& jsonMessage = message.toJSON(true);
37 
38  boost::system::error_code err;
39  m_socket.send_to(buffer(jsonMessage, jsonMessage.size()), m_remoteEndpoint, 0, err);
40  const bool sent = err.value() != 0;
41  return sent;
42 }
43 
45 {
46  m_socket.close();
47 }
Belle2::LogConnectionUDP::isConnected
bool isConnected() final
there is no way to check if a UDP connection is fine, so we just return True always
Definition: LogConnectionUDP.cc:29
Belle2::LogConnectionUDP::sendMessage
bool sendMessage(const LogMessage &message) final
Send the log message as JSON to the UDP server.
Definition: LogConnectionUDP.cc:34
Belle2::LogConnectionUDP::m_ioservice
boost::asio::io_service m_ioservice
asio service for handling the requests
Definition: LogConnectionUDP.h:43
Belle2::LogConnectionUDP::~LogConnectionUDP
~LogConnectionUDP() override
Close the socket on destruction.
Definition: LogConnectionUDP.cc:44
Belle2::LogConnectionUDP::m_remoteEndpoint
boost::asio::ip::udp::endpoint m_remoteEndpoint
the remote endpoint we send to
Definition: LogConnectionUDP.h:47
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::LogConnectionUDP::m_socket
boost::asio::ip::udp::socket m_socket
the socket to the UDP server
Definition: LogConnectionUDP.h:45
Belle2::LogMessage
The LogMessage class.
Definition: LogMessage.h:39