|
@@ -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;
|
|
|
+}
|