/* * Author: * Sven Czarnian * Brief: * Implements the aircraft reporter * Copyright: * 2021 Sven Czarnian * License: * GNU General Public License v3 (GPLv3) */ #include #include "ZmqContext.h" using namespace aman; AircraftReporter::AircraftReporter() noexcept : m_socket() { } bool AircraftReporter::initialize(const Communication& configuration) { if (nullptr != this->m_socket) return true; if (false == configuration.valid) return false; this->m_socket = std::make_unique(ZmqContext::instance().context(), zmq::socket_type::pub); this->m_socket->set(zmq::sockopt::immediate, true); /* configure the encryption */ if (false == this->setSocketKey(configuration.serverPublicIdentifier, zmq::sockopt::curve_serverkey)) return false; if (false == this->setSocketKey(configuration.clientPublicIdentifier, zmq::sockopt::curve_publickey)) return false; if (false == this->setSocketKey(configuration.clientPrivateIdentifier, zmq::sockopt::curve_secretkey)) return false; /* connect to the server */ try { this->m_socket->connect("tcp://" + configuration.address + ":" + std::to_string(configuration.portReporter)); } catch (zmq::error_t&) { this->m_socket = std::unique_ptr(); return false; } return true; } bool AircraftReporter::deinitialize() { if (nullptr == this->m_socket) return true; this->m_socket->close(); this->m_socket = std::make_unique(); return true; } bool AircraftReporter::initialized() const noexcept { return nullptr != this->m_socket; } bool AircraftReporter::send(const aman::AircraftReport& report) { bool retval = false; if (nullptr != this->m_socket) { /* serialize the report */ std::string serialized = report.SerializeAsString(); zmq::message_t message(serialized.size()); std::memcpy(message.data(), serialized.c_str(), serialized.size()); try { auto size = message.size(); auto result = this->m_socket->send(message, zmq::send_flags::none); retval = result.value() == size; } catch (zmq::error_t&) { return false; } } return retval; } AircraftReporter& AircraftReporter::instance() noexcept { static AircraftReporter __instance; return __instance; }