From a5c50a4f262f0a880734623f79d4dc2f1aa8a0a2 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 13 Aug 2019 10:29:39 +0200 Subject: Pull in files from odr-mmbtools-common Replace ASIO by simpler implementation, meaning that the telnet RC now only supports a single connection. Move Log, RC to lib/ --- lib/RemoteControl.h | 249 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) create mode 100644 lib/RemoteControl.h (limited to 'lib/RemoteControl.h') diff --git a/lib/RemoteControl.h b/lib/RemoteControl.h new file mode 100644 index 0000000..bd88f82 --- /dev/null +++ b/lib/RemoteControl.h @@ -0,0 +1,249 @@ +/* + Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2019 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + + This module adds remote-control capability to some of the dabmux/dabmod modules. + */ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#if defined(HAVE_ZEROMQ) +# include "zmq.hpp" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Log.h" +#include "Socket.h" + +#define RC_ADD_PARAMETER(p, desc) { \ + std::vector p; \ + p.push_back(#p); \ + p.push_back(desc); \ + m_parameters.push_back(p); \ +} + +class ParameterError : public std::exception +{ + public: + ParameterError(std::string message) : m_message(message) {} + ~ParameterError() throw() {} + const char* what() const throw() { return m_message.c_str(); } + + private: + std::string m_message; +}; + +class RemoteControllable; + +/* Remote controllers (that recieve orders from the user) + * must implement BaseRemoteController + */ +class BaseRemoteController { + public: + /* When this returns one, the remote controller cannot be + * used anymore, and must be restarted + */ + virtual bool fault_detected() = 0; + + /* In case of a fault, the remote controller can be + * restarted. + */ + virtual void restart() = 0; + + virtual ~BaseRemoteController() {} +}; + +/* Objects that support remote control must implement the following class */ +class RemoteControllable { + public: + RemoteControllable(const std::string& name) : + m_rc_name(name) {} + + RemoteControllable(const RemoteControllable& other) = delete; + RemoteControllable& operator=(const RemoteControllable& other) = delete; + + virtual ~RemoteControllable(); + + /* return a short name used to identify the controllable. + * It might be used in the commands the user has to type, so keep + * it short + */ + virtual std::string get_rc_name() const { return m_rc_name; } + + /* Return a list of possible parameters that can be set */ + virtual std::list get_supported_parameters() const; + + /* Return a mapping of the descriptions of all parameters */ + virtual std::list< std::vector > + get_parameter_descriptions() const + { + return m_parameters; + } + + /* Base function to set parameters. */ + virtual void set_parameter( + const std::string& parameter, + const std::string& value) = 0; + + /* Getting a parameter always returns a string. */ + virtual const std::string get_parameter(const std::string& parameter) const = 0; + + protected: + std::string m_rc_name; + std::list< std::vector > m_parameters; +}; + +/* Holds all our remote controllers and controlled object. + */ +class RemoteControllers { + public: + void add_controller(std::shared_ptr rc); + void enrol(RemoteControllable *rc); + void remove_controllable(RemoteControllable *rc); + void check_faults(); + std::list< std::vector > get_param_list_values(const std::string& name); + std::string get_param(const std::string& name, const std::string& param); + + void set_param( + const std::string& name, + const std::string& param, + const std::string& value); + + std::list controllables; + + private: + RemoteControllable* get_controllable_(const std::string& name); + + std::list > m_controllers; +}; + +extern RemoteControllers rcs; + +/* Implements a Remote controller based on a simple telnet CLI + * that listens on localhost + */ +class RemoteControllerTelnet : public BaseRemoteController { + public: + RemoteControllerTelnet() + : m_active(false), + m_fault(false), + m_port(0) { } + + RemoteControllerTelnet(int port) + : m_active(port > 0), + m_fault(false), + m_port(port) + { + restart(); + } + + + RemoteControllerTelnet& operator=(const RemoteControllerTelnet& other) = delete; + RemoteControllerTelnet(const RemoteControllerTelnet& other) = delete; + + ~RemoteControllerTelnet(); + + virtual bool fault_detected() { return m_fault; } + + virtual void restart(); + + private: + void restart_thread(long); + + void process(long); + + void dispatch_command(Socket::TCPSocket& socket, std::string command); + void reply(Socket::TCPSocket& socket, std::string message); + void handle_accept(Socket::TCPSocket&& socket); + + std::atomic m_active; + + /* This is set to true if a fault occurred */ + std::atomic m_fault; + std::thread m_restarter_thread; + + std::thread m_child_thread; + + Socket::TCPSocket m_socket; + int m_port; +}; + +#if defined(HAVE_ZEROMQ) +/* Implements a Remote controller using ZMQ transportlayer + * that listens on localhost + */ +class RemoteControllerZmq : public BaseRemoteController { + public: + RemoteControllerZmq() + : m_active(false), m_fault(false), + m_zmqContext(1), + m_endpoint("") { } + + RemoteControllerZmq(const std::string& endpoint) + : m_active(not endpoint.empty()), m_fault(false), + m_zmqContext(1), + m_endpoint(endpoint), + m_child_thread(&RemoteControllerZmq::process, this) { } + + RemoteControllerZmq& operator=(const RemoteControllerZmq& other) = delete; + RemoteControllerZmq(const RemoteControllerZmq& other) = delete; + + ~RemoteControllerZmq(); + + virtual bool fault_detected() { return m_fault; } + + virtual void restart(); + + private: + void restart_thread(); + + void recv_all(zmq::socket_t &pSocket, std::vector &message); + void send_ok_reply(zmq::socket_t &pSocket); + void send_fail_reply(zmq::socket_t &pSocket, const std::string &error); + void process(); + + std::atomic m_active; + + /* This is set to true if a fault occurred */ + std::atomic m_fault; + std::thread m_restarter_thread; + + zmq::context_t m_zmqContext; + + std::string m_endpoint; + std::thread m_child_thread; +}; +#endif + -- cgit v1.2.3 From cb8cd168a26477b5c01e73e53b727bf8f5b225db Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 2 Sep 2019 14:23:12 +0200 Subject: Apply b1fc144 from common --- Makefile.am | 1 + lib/Globals.cpp | 36 ++++++++++++++++++++++++++++++++++++ lib/Log.cpp | 33 +++++++++++++++++++++++---------- lib/Log.h | 14 +++++--------- lib/RemoteControl.cpp | 2 -- lib/RemoteControl.h | 2 ++ 6 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 lib/Globals.cpp (limited to 'lib/RemoteControl.h') diff --git a/Makefile.am b/Makefile.am index 6691137..34d2d91 100644 --- a/Makefile.am +++ b/Makefile.am @@ -99,6 +99,7 @@ odr_dabmod_SOURCES = src/DabMod.cpp \ lib/RemoteControl.h \ lib/Log.cpp \ lib/Log.h \ + lib/Globals.cpp \ lib/INIReader.h \ lib/crc.h \ lib/crc.c \ diff --git a/lib/Globals.cpp b/lib/Globals.cpp new file mode 100644 index 0000000..6be26ec --- /dev/null +++ b/lib/Globals.cpp @@ -0,0 +1,36 @@ +/* + Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 + Her Majesty the Queen in Right of Canada (Communications Research + Center Canada) + + Copyright (C) 2019 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org + */ +/* + This file is part of the ODR-mmbTools. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +/* Ensure construction and destruction of static globals in the right order */ + +#include "Log.h" +#include "RemoteControl.h" + +// the RC needs logging, and needs to be initialised later. +Logger etiLog; +RemoteControllers rcs; + diff --git a/lib/Log.cpp b/lib/Log.cpp index 2417f3a..abbd69a 100644 --- a/lib/Log.cpp +++ b/lib/Log.cpp @@ -34,12 +34,23 @@ using namespace std; -/* etiLog is a singleton used in all parts of the program to output log messages. - */ -Logger etiLog; + +Logger::Logger() +{ + m_io_thread = std::thread(&Logger::io_process, this); +} + +Logger::~Logger() { + m_message_queue.trigger_wakeup(); + m_io_thread.join(); + + std::lock_guard guard(m_backend_mutex); + backends.clear(); +} void Logger::register_backend(std::shared_ptr backend) { + std::lock_guard guard(m_backend_mutex); backends.push_back(backend); } @@ -75,7 +86,7 @@ void Logger::logstr(log_level_t level, std::string&& message) { if (level == discard) { return; - } + } log_message_t m(level, move(message)); m_message_queue.push(move(m)); @@ -101,13 +112,15 @@ void Logger::io_process() message.resize(message.length()-1); } - for (auto &backend : backends) { - backend->log(m.level, message); - } + { + std::lock_guard guard(m_backend_mutex); + for (auto &backend : backends) { + backend->log(m.level, message); + } - if (m.level != log_level_t::trace) { - std::lock_guard guard(m_cerr_mutex); - std::cerr << levels_as_str[m.level] << " " << message << std::endl; + if (m.level != log_level_t::trace) { + std::cerr << levels_as_str[m.level] << " " << message << std::endl; + } } } } diff --git a/lib/Log.h b/lib/Log.h index d5c39e0..f20e698 100644 --- a/lib/Log.h +++ b/lib/Log.h @@ -133,16 +133,10 @@ struct log_message_t { class Logger { public: - Logger() { - m_io_thread = std::thread(&Logger::io_process, this); - } - + Logger(); Logger(const Logger& other) = delete; const Logger& operator=(const Logger& other) = delete; - ~Logger() { - m_message_queue.trigger_wakeup(); - m_io_thread.join(); - } + ~Logger(); void register_backend(std::shared_ptr backend); @@ -163,9 +157,11 @@ class Logger { ThreadsafeQueue m_message_queue; std::thread m_io_thread; - std::mutex m_cerr_mutex; + std::mutex m_backend_mutex; }; +/* etiLog is a singleton used in all parts of the program to output log messages. + * It is constructed in Globals.cpp */ extern Logger etiLog; // Accumulate a line of logs, using same syntax as stringstream diff --git a/lib/RemoteControl.cpp b/lib/RemoteControl.cpp index 878af59..4adb90c 100644 --- a/lib/RemoteControl.cpp +++ b/lib/RemoteControl.cpp @@ -32,8 +32,6 @@ using namespace std; -RemoteControllers rcs; - RemoteControllerTelnet::~RemoteControllerTelnet() { m_active = false; diff --git a/lib/RemoteControl.h b/lib/RemoteControl.h index bd88f82..2358b3a 100644 --- a/lib/RemoteControl.h +++ b/lib/RemoteControl.h @@ -149,6 +149,8 @@ class RemoteControllers { std::list > m_controllers; }; +/* rcs is a singleton used in all parts of the program to interact with the RC. + * It is constructed in Globals.cpp */ extern RemoteControllers rcs; /* Implements a Remote controller based on a simple telnet CLI -- cgit v1.2.3