90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
/*
|
|
* 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_socket() { }
|
|
|
|
bool AircraftReporter::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);
|
|
|
|
/* 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&) {
|
|
this->m_socket = std::unique_ptr<zmq::socket_t>();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AircraftReporter::deinitialize() {
|
|
if (nullptr == this->m_socket)
|
|
return true;
|
|
|
|
this->m_socket->close();
|
|
this->m_socket = std::make_unique<zmq::socket_t>();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AircraftReporter::initialized() const noexcept {
|
|
return nullptr != this->m_socket;
|
|
}
|
|
|
|
bool AircraftReporter::send(const aman::AircraftReport& report) {
|
|
bool retval = false;
|
|
|
|
if (nullptr != this->m_socket) {
|
|
/* serialize the report */
|
|
std::string serialized = report.SerializeAsString();
|
|
zmq::message_t message(serialized.size());
|
|
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;
|
|
}
|
|
catch (zmq::error_t&) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return retval;
|
|
}
|
|
|
|
AircraftReporter& AircraftReporter::instance() noexcept {
|
|
static AircraftReporter __instance;
|
|
return __instance;
|
|
}
|