|
@@ -2,26 +2,28 @@
|
|
|
* Author:
|
|
|
* Sven Czarnian <devel@svcz.de>
|
|
|
* Brief:
|
|
|
- * Implements the scheduling notification
|
|
|
+ * Implements the backend receiver
|
|
|
* Copyright:
|
|
|
* 2021 Sven Czarnian
|
|
|
* License:
|
|
|
* GNU General Public License v3 (GPLv3)
|
|
|
*/
|
|
|
|
|
|
-#include <aman/com/AircraftScheduler.h>
|
|
|
+#include <aman/com/BackendReceiver.h>
|
|
|
|
|
|
#include "ZmqContext.h"
|
|
|
|
|
|
using namespace aman;
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
-AircraftScheduler::AircraftScheduler() noexcept :
|
|
|
+BackendReceiver::BackendReceiver() noexcept :
|
|
|
m_socket(),
|
|
|
m_receiverThread(),
|
|
|
- m_stopReceiver(false) { }
|
|
|
+ m_stopReceiver(false),
|
|
|
+ m_sequences(),
|
|
|
+ m_sequencesLock() { }
|
|
|
|
|
|
-bool AircraftScheduler::initialize(const Communication& configuration) {
|
|
|
+bool BackendReceiver::initialize(const Communication& configuration) {
|
|
|
if (nullptr != this->m_socket)
|
|
|
return true;
|
|
|
if (false == configuration.valid)
|
|
@@ -47,12 +49,12 @@ bool AircraftScheduler::initialize(const Communication& configuration) {
|
|
|
}
|
|
|
|
|
|
this->m_stopReceiver = false;
|
|
|
- this->m_receiverThread = std::thread(&AircraftScheduler::run, this);
|
|
|
+ this->m_receiverThread = std::thread(&BackendReceiver::run, this);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool AircraftScheduler::deinitialize() {
|
|
|
+bool BackendReceiver::deinitialize() {
|
|
|
if (nullptr == this->m_socket)
|
|
|
return true;
|
|
|
|
|
@@ -64,42 +66,56 @@ bool AircraftScheduler::deinitialize() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool AircraftScheduler::initialized() const noexcept {
|
|
|
+bool BackendReceiver::initialized() const noexcept {
|
|
|
return nullptr != this->m_socket;
|
|
|
}
|
|
|
|
|
|
-std::unique_ptr<aman::AircraftSchedule> AircraftScheduler::receive() {
|
|
|
+void BackendReceiver::receiveSequence() {
|
|
|
zmq::message_t message;
|
|
|
|
|
|
if (nullptr == this->m_socket)
|
|
|
- return std::unique_ptr<aman::AircraftSchedule>();
|
|
|
+ return;
|
|
|
|
|
|
try {
|
|
|
auto result = this->m_socket->recv(message, zmq::recv_flags::dontwait);
|
|
|
if (false == result.has_value() || 0 == result.value())
|
|
|
- return std::unique_ptr<aman::AircraftSchedule>();
|
|
|
+ return;
|
|
|
}
|
|
|
catch (zmq::error_t&) {
|
|
|
- return std::unique_ptr<aman::AircraftSchedule>();
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- std::unique_ptr<aman::AircraftSchedule> retval = std::make_unique<aman::AircraftSchedule>();
|
|
|
+ std::unique_ptr<aman::AircraftSequence> retval = std::make_unique<aman::AircraftSequence>();
|
|
|
retval->ParseFromString(reinterpret_cast<const char*>(message.data()));
|
|
|
|
|
|
- return retval;
|
|
|
+ std::lock_guard guard(this->m_sequencesLock);
|
|
|
+ this->m_sequences.push_back(std::move(retval));
|
|
|
}
|
|
|
|
|
|
-void AircraftScheduler::run() {
|
|
|
+void BackendReceiver::run() {
|
|
|
while (false == this->m_stopReceiver) {
|
|
|
auto message = this->receive();
|
|
|
if (nullptr == message) {
|
|
|
std::this_thread::sleep_for(500ms);
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
+ this->receiveSequence();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-AircraftScheduler& AircraftScheduler::instance() noexcept {
|
|
|
- static AircraftScheduler __instance;
|
|
|
+std::shared_ptr<aman::AircraftSequence> BackendReceiver::receive() {
|
|
|
+ std::lock_guard guard(this->m_sequencesLock);
|
|
|
+ if (0 != this->m_sequences.size()) {
|
|
|
+ auto retval = this->m_sequences.front();
|
|
|
+ this->m_sequences.pop_front();
|
|
|
+ return retval;
|
|
|
+ }
|
|
|
+
|
|
|
+ return nullptr;
|
|
|
+}
|
|
|
+
|
|
|
+BackendReceiver& BackendReceiver::instance() noexcept {
|
|
|
+ static BackendReceiver __instance;
|
|
|
return __instance;
|
|
|
}
|