rename the sequence receiver

This commit is contained in:
Sven Czarnian
2021-11-13 23:01:12 +01:00
parent 8b1292c95a
commit a3ef28ae39
4 changed files with 59 additions and 39 deletions

View File

@@ -32,8 +32,8 @@ SET(SOURCE_FILES
)
SET(SOURCE_COM_FILES
com/BackendReceiver.cpp
com/BackendNotification.cpp
com/AircraftScheduler.cpp
com/ZmqContext.cpp
com/ZmqContext.h
)
@@ -51,8 +51,8 @@ SET(SOURCE_FILES_RES
)
SET(INCLUDE_COM_FILES
${CMAKE_SOURCE_DIR}/include/aman/com/BackendReceiver.h
${CMAKE_SOURCE_DIR}/include/aman/com/BackendNotification.h
${CMAKE_SOURCE_DIR}/include/aman/com/AircraftScheduler.h
)
SET(INCLUDE_CONFIG_FILES

View File

@@ -15,7 +15,7 @@
#include <Shlwapi.h>
#include <Windows.h>
#include <aman/com/AircraftScheduler.h>
#include <aman/com/BackendReceiver.h>
#include <aman/com/BackendNotification.h>
#include <aman/config/CommunicationFileFormat.h>
#include <aman/config/IdentifierFileFormat.h>
@@ -62,20 +62,20 @@ PlugIn::PlugIn() :
ZmqContext::instance().initialize();
if (false == AircraftReporter::instance().initialize(this->m_configuration)) {
if (false == BackendNotification::instance().initialize(this->m_configuration)) {
this->DisplayUserMessage(PLUGIN_NAME, "ERROR", "Unable to initialize the reporter-connection to the backend", true, true, true, true, true);
return;
}
if (false == AircraftScheduler::instance().initialize(this->m_configuration)) {
if (false == BackendReceiver::instance().initialize(this->m_configuration)) {
this->DisplayUserMessage(PLUGIN_NAME, "ERROR", "Unable to initialize the scheduling-connection to the backend", true, true, true, true, true);
AircraftReporter::instance().deinitialize();
BackendNotification::instance().deinitialize();
return;
}
}
PlugIn::~PlugIn() noexcept {
AircraftScheduler::instance().deinitialize();
BackendReceiver::instance().deinitialize();
BackendNotification::instance().deinitialize();
ZmqContext::instance().deinitialize();
google::protobuf::ShutdownProtobufLibrary();

View File

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