1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-15 04:30:33 +00:00

Merge branch 'next' of https://github.com/gnss-sdr/gnss-sdr into next

This commit is contained in:
Carles Fernandez 2018-10-21 21:19:23 +02:00
commit dc374ed6ef
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 69 additions and 58 deletions

View File

@ -103,81 +103,92 @@ void TcpCmdInterface::run_cmd_server(int tcp_port)
// Socket and acceptor // Socket and acceptor
boost::asio::io_service service; boost::asio::io_service service;
boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)); try
bool keep_running = true;
while (keep_running)
{ {
try boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
bool keep_running = true;
while (keep_running)
{ {
std::cout << "Telecommand TCP interface listening on port " << tcp_port << std::endl; try
boost::asio::ip::tcp::socket socket(service);
acceptor.accept(socket, not_throw);
if (not_throw)
{ {
std::cerr << "Error when binding the port in the socket" << std::endl; std::cout << "Telecommand TCP interface listening on port " << tcp_port << std::endl;
continue;
}
// Read a message boost::asio::ip::tcp::socket socket(service);
boost::system::error_code error = boost::asio::error::eof; acceptor.accept(socket, not_throw);
do if (not_throw)
{
std::string response;
boost::asio::streambuf b;
boost::asio::read_until(socket, b, '\n');
std::istream is(&b);
std::string line;
std::getline(is, line);
std::cout << "received command: " << line << std::endl;
std::istringstream iss(line);
std::vector<std::string> cmd_vector(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if (cmd_vector.size() > 0)
{ {
try std::cerr << "Error when binding the port in the socket" << std::endl;
continue;
}
// Read a message
boost::system::error_code error = boost::asio::error::eof;
do
{
std::string response;
boost::asio::streambuf b;
boost::asio::read_until(socket, b, '\n');
std::istream is(&b);
std::string line;
std::getline(is, line);
std::cout << "received command: " << line << std::endl;
std::istringstream iss(line);
std::vector<std::string> cmd_vector(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if (cmd_vector.size() > 0)
{ {
response = functions[cmd_vector.at(0)](cmd_vector); try
{
response = functions[cmd_vector.at(0)](cmd_vector);
}
catch (const std::exception &ex)
{
response = "ERROR: command execution error: " + std::string(ex.what()) + "\n";
}
} }
catch (const std::exception &ex) else
{ {
response = "ERROR: command execution error: " + std::string(ex.what()) + "\n"; response = "ERROR: empty command\n";
} }
//send cmd response
socket.write_some(boost::asio::buffer(response), not_throw);
if (not_throw)
{
std::cerr << "Error sending(" << not_throw.value() << "): " << not_throw.message() << std::endl;
break;
}
}
while (!error); // && error != boost::asio::error::eof);
if (error == boost::asio::error::eof)
{
std::cout << "EOF detected\n";
} }
else else
{ {
response = "ERROR: empty command\n"; std::cout << "error: " << error << std::endl;
} }
//send cmd response // Close socket
socket.write_some(boost::asio::buffer(response), not_throw); socket.close();
if (not_throw)
{
std::cerr << "Error sending(" << not_throw.value() << "): " << not_throw.message() << std::endl;
break;
}
} }
while (error > 0); // && error != boost::asio::error::eof); catch (const boost::exception &e)
if (error == boost::asio::error::eof)
{ {
std::cout << "EOF detected\n"; std::cout << "Boost exception " << std::endl;
} }
else catch (const std::exception &ex)
{ {
std::cout << "error: " << error << std::endl; std::cout << "Exception " << ex.what() << std::endl;
} }
// Close socket
socket.close();
}
catch (const std::exception &ex)
{
std::cout << "Exception " << ex.what() << std::endl;
} }
} }
catch (const boost::exception &e)
{
std::cout << "TCP Command Interface exception: address already in use" << std::endl;
}
} }

View File

@ -28,8 +28,8 @@
* *
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
#ifndef TCPCMDINTERFACE_H_ #ifndef GNSS_SDR_TCPCMDINTERFACE_H_
#define TCPCMDINTERFACE_H_ #define GNSS_SDR_TCPCMDINTERFACE_H_
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -38,7 +38,7 @@
#include <unordered_map> #include <unordered_map>
#include <algorithm> #include <algorithm>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <stdint.h> #include <cstdint>
class TcpCmdInterface class TcpCmdInterface
@ -61,4 +61,4 @@ private:
void register_functions(); void register_functions();
}; };
#endif /* TCPCMDINTERFACE_H_ */ #endif /* GNSS_SDR_TCPCMDINTERFACE_H_ */