2018-07-03 11:25:43 +00:00
|
|
|
/*!
|
|
|
|
* \file gnss_synchro_udp_sink.cc
|
|
|
|
* \brief Implementation of a class that sends serialized Gnss_Synchro
|
|
|
|
* objects over udp to one or multiple endponits
|
|
|
|
* \author Álvaro Cebrián Juan, 2018. acebrianjuan(at)gmail.com
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2018-07-03 11:25:43 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2018-07-03 11:25:43 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-07-03 11:25:43 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2018-07-03 11:25:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gnss_synchro_udp_sink.h"
|
|
|
|
#include <boost/archive/binary_oarchive.hpp>
|
|
|
|
#include <boost/serialization/vector.hpp>
|
|
|
|
#include <iostream>
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <sstream>
|
2018-07-03 11:25:43 +00:00
|
|
|
|
2021-12-14 16:03:29 +00:00
|
|
|
Gnss_Synchro_Udp_Sink::Gnss_Synchro_Udp_Sink(const std::vector<std::string>& addresses,
|
|
|
|
const uint16_t& port,
|
|
|
|
bool enable_protobuf)
|
|
|
|
: socket{io_context},
|
|
|
|
use_protobuf(enable_protobuf)
|
2018-07-03 11:25:43 +00:00
|
|
|
{
|
2019-04-21 11:30:59 +00:00
|
|
|
if (enable_protobuf)
|
|
|
|
{
|
|
|
|
serdes = Serdes_Gnss_Synchro();
|
|
|
|
}
|
2019-02-12 00:25:27 +00:00
|
|
|
for (const auto& address : addresses)
|
2018-07-03 11:25:43 +00:00
|
|
|
{
|
|
|
|
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::address::from_string(address, error), port);
|
|
|
|
endpoints.push_back(endpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:24:06 +00:00
|
|
|
|
2019-04-21 11:30:59 +00:00
|
|
|
bool Gnss_Synchro_Udp_Sink::write_gnss_synchro(const std::vector<Gnss_Synchro>& stocks)
|
2018-07-03 11:25:43 +00:00
|
|
|
{
|
2019-04-20 09:52:00 +00:00
|
|
|
std::string outbound_data;
|
2019-04-21 11:30:59 +00:00
|
|
|
if (use_protobuf == false)
|
2019-04-20 09:52:00 +00:00
|
|
|
{
|
|
|
|
std::ostringstream archive_stream;
|
|
|
|
boost::archive::binary_oarchive oa{archive_stream};
|
|
|
|
oa << stocks;
|
|
|
|
outbound_data = archive_stream.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outbound_data = serdes.createProtobuffer(stocks);
|
|
|
|
}
|
2018-07-03 11:25:43 +00:00
|
|
|
|
2024-05-06 16:58:09 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
for (const auto& endpoint : endpoints)
|
2018-07-03 11:25:43 +00:00
|
|
|
{
|
2024-05-06 16:58:09 +00:00
|
|
|
socket.open(endpoint.protocol(), error); // NOLINT(bugprone-unused-return-value)
|
|
|
|
|
|
|
|
if (socket.send_to(boost::asio::buffer(outbound_data), endpoint) == 0) // this can throw
|
2021-01-25 12:35:47 +00:00
|
|
|
{
|
2024-05-06 16:58:09 +00:00
|
|
|
return false;
|
2021-01-25 12:35:47 +00:00
|
|
|
}
|
2018-07-03 11:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-06 16:58:09 +00:00
|
|
|
catch (const boost::system::system_error& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Error sending data: " << e.what() << '\n';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-03 11:25:43 +00:00
|
|
|
return true;
|
|
|
|
}
|