|
@@ -2,31 +2,30 @@
|
|
|
* Author:
|
|
|
* Sven Czarnian <devel@svcz.de>
|
|
|
* Brief:
|
|
|
- * Implements the backend notification
|
|
|
+ * Implements the backend communication
|
|
|
* Copyright:
|
|
|
* 2021 Sven Czarnian
|
|
|
* License:
|
|
|
* GNU General Public License v3 (GPLv3)
|
|
|
*/
|
|
|
|
|
|
-#include <aman/com/BackendNotification.h>
|
|
|
+#include <aman/com/Backend.h>
|
|
|
#include <aman/helper/String.h>
|
|
|
|
|
|
#include "ZmqContext.h"
|
|
|
|
|
|
using namespace aman;
|
|
|
|
|
|
-BackendNotification::BackendNotification() noexcept :
|
|
|
+Backend::Backend() noexcept :
|
|
|
m_socket() { }
|
|
|
|
|
|
-bool BackendNotification::initialize(const Communication& configuration) {
|
|
|
+bool Backend::initialize(const Communication& configuration) {
|
|
|
if (nullptr != this->m_socket)
|
|
|
return true;
|
|
|
if (false == configuration.valid)
|
|
|
return false;
|
|
|
|
|
|
- this->m_socket = std::make_unique<zmq::socket_t>(ZmqContext::instance().context(), zmq::socket_type::pub);
|
|
|
- this->m_socket->set(zmq::sockopt::immediate, true);
|
|
|
+ this->m_socket = std::make_unique<zmq::socket_t>(ZmqContext::instance().context(), zmq::socket_type::req);
|
|
|
|
|
|
/* configure the encryption */
|
|
|
if (false == this->setSocketKey(configuration.serverPublicIdentifier, zmq::sockopt::curve_serverkey))
|
|
@@ -48,7 +47,7 @@ bool BackendNotification::initialize(const Communication& configuration) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool BackendNotification::deinitialize() {
|
|
|
+bool Backend::deinitialize() {
|
|
|
if (nullptr == this->m_socket)
|
|
|
return true;
|
|
|
|
|
@@ -58,13 +57,33 @@ bool BackendNotification::deinitialize() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool BackendNotification::initialized() const noexcept {
|
|
|
+bool Backend::initialized() const noexcept {
|
|
|
return nullptr != this->m_socket;
|
|
|
}
|
|
|
|
|
|
-bool BackendNotification::send(aman::AircraftUpdate& report) {
|
|
|
- bool retval = false;
|
|
|
+std::shared_ptr<aman::AircraftSequence> Backend::receiveSequence() {
|
|
|
+ zmq::message_t message;
|
|
|
|
|
|
+ if (nullptr == this->m_socket)
|
|
|
+ return nullptr;
|
|
|
+
|
|
|
+ try {
|
|
|
+ //auto result = this->m_socket->recv(message, zmq::recv_flags::dontwait);
|
|
|
+ auto result = this->m_socket->recv(message);
|
|
|
+ if (false == result.has_value() || 0 == result.value())
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+ catch (zmq::error_t&) {
|
|
|
+ return nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::unique_ptr<aman::AircraftSequence> retval = std::make_unique<aman::AircraftSequence>();
|
|
|
+ retval->ParseFromString(reinterpret_cast<const char*>(message.data()));
|
|
|
+
|
|
|
+ return std::move(retval);
|
|
|
+}
|
|
|
+
|
|
|
+std::shared_ptr<aman::AircraftSequence> Backend::update(aman::AircraftUpdate& report) {
|
|
|
if (nullptr != this->m_socket) {
|
|
|
/* serialize the report */
|
|
|
std::string serialized = report.SerializeAsString();
|
|
@@ -72,19 +91,20 @@ bool BackendNotification::send(aman::AircraftUpdate& report) {
|
|
|
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;
|
|
|
+ const auto size = message.size();
|
|
|
+ const auto result = this->m_socket->send(message, zmq::send_flags::none);
|
|
|
+ if (result.value() == size)
|
|
|
+ return this->receiveSequence();
|
|
|
}
|
|
|
catch (zmq::error_t&) {
|
|
|
- return false;
|
|
|
+ return nullptr;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return retval;
|
|
|
+ return nullptr;
|
|
|
}
|
|
|
|
|
|
-BackendNotification& BackendNotification::instance() noexcept {
|
|
|
- static BackendNotification __instance;
|
|
|
+Backend& Backend::instance() noexcept {
|
|
|
+ static Backend __instance;
|
|
|
return __instance;
|
|
|
}
|