ソースを参照

introduce the aircraft reporter

Sven Czarnian 3 年 前
コミット
8e36b094a5
2 ファイル変更130 行追加0 行削除
  1. 53 0
      include/aman/com/AircraftReporter.h
  2. 77 0
      src/com/AircraftReporter.cpp

+ 53 - 0
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 <devel@svcz.de>
+ * @copyright Copyright 2021 Sven Czarnian
+ * @license This project is published under the GNU General Public License v3 (GPLv3)
+ */
+
+#pragma once
+
+#include <zmq.hpp>
+
+#include <aman/types/Communication.h>
+
+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 <typename T>
+        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();
+    };
+}

+ 77 - 0
src/com/AircraftReporter.cpp

@@ -0,0 +1,77 @@
+/*
+ * Author:
+ *   Sven Czarnian <devel@svcz.de>
+ * Brief:
+ *   Implements the aircraft reporter
+ * Copyright:
+ *   2021 Sven Czarnian
+ * License:
+ *   GNU General Public License v3 (GPLv3)
+ */
+
+#include <aman/com/AircraftReporter.h>
+
+#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;
+}