diff --git a/include/aman/com/AircraftReporter.h b/include/aman/com/AircraftReporter.h new file mode 100644 index 0000000..e1b5b0e --- /dev/null +++ b/include/aman/com/AircraftReporter.h @@ -0,0 +1,53 @@ +/* + * @brief Defines the aircraft reporter module to communicate with the backend + * @file aman/com/AircraftReporter.h + * @author Sven Czarnian + * @copyright Copyright 2021 Sven Czarnian + * @license This project is published under the GNU General Public License v3 (GPLv3) + */ + +#pragma once + +#include + +#include + +namespace aman { + /** + * @brief Defines the base class for all file formats + * @ingroup format + * + * The base class provides error information, etc. + */ + class AircraftReporter { + private: + bool m_initialized; + zmq::socket_t m_socket; + + AircraftReporter() noexcept; + + template + bool setSocketKey(const std::string& key, T entry) { + try { + this->m_socket.set(entry, key); + return true; + } + catch (std::exception&) { + return false; + } + } + + public: + AircraftReporter(const AircraftReporter&) = delete; + AircraftReporter(AircraftReporter&&) = delete; + AircraftReporter& operator=(const AircraftReporter&) = delete; + AircraftReporter& operator=(AircraftReporter&&) = delete; + + bool initialize(const Communication& configuration); + bool deinitialize(); + + bool send(zmq::message_t& message); + + static AircraftReporter& instance(); + }; +} diff --git a/src/com/AircraftReporter.cpp b/src/com/AircraftReporter.cpp new file mode 100644 index 0000000..cfd5a6d --- /dev/null +++ b/src/com/AircraftReporter.cpp @@ -0,0 +1,77 @@ +/* + * 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_initialized(false), + m_socket() { } + +bool AircraftReporter::initialize(const Communication& configuration) { + if (true == this->m_initialized || false == configuration.valid) + return this->m_initialized; + + this->m_socket = zmq::socket_t(ZmqContext::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&) { + return false; + } + + this->m_initialized = true; + return true; +} + +bool AircraftReporter::deinitialize() { + if (false == this->m_initialized) + return true; + + this->m_socket.close(); + return true; +} + +bool AircraftReporter::send(zmq::message_t& message) { + bool retval = false; + + if (true == this->m_initialized) { + try { + auto bla = message.size(); + auto result = this->m_socket.send(message, zmq::send_flags::none); + retval = result.value() == bla; + } + catch (zmq::error_t&) { + return false; + } + } + + return retval; +} + +AircraftReporter& AircraftReporter::instance() { + static AircraftReporter __instance; + return __instance; +}