From 8b8247f117cb75fe9674f6c5975f482c6cc2e1ce Mon Sep 17 00:00:00 2001 From: Giulio Moro Date: Fri, 31 May 2024 19:04:54 -0500 Subject: [PATCH] Scope,WSServer: check return values, return them and warn on error --- core/SchedulableThread.cpp | 3 +++ libraries/Gui/Gui.cpp | 7 ++++++- libraries/Scope/Scope.cpp | 17 ++++++++++++++--- libraries/Scope/Scope.h | 2 +- libraries/WSServer/WSServer.cpp | 6 ++++-- libraries/WSServer/WSServer.h | 2 +- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/core/SchedulableThread.cpp b/core/SchedulableThread.cpp index 1d5163014..3868ee270 100644 --- a/core/SchedulableThread.cpp +++ b/core/SchedulableThread.cpp @@ -37,7 +37,10 @@ int SchedulableThread::create(const std::string& _name, int priority) name = _name; int ret = commsInit(); if(ret) + { + fprintf(stderr, "SchedulableThread: unable to initialize communication %s: %i\n", name.c_str(), ret); return 1; + } // start the xenomai task int stackSize = 0; diff --git a/libraries/Gui/Gui.cpp b/libraries/Gui/Gui.cpp index c5990be50..5899c4a59 100644 --- a/libraries/Gui/Gui.cpp +++ b/libraries/Gui/Gui.cpp @@ -19,7 +19,12 @@ int Gui::setup(unsigned int port, std::string address) // Set up the websocket server ws_server = std::unique_ptr(new WSServer()); - ws_server->setup(port); + int ret = ws_server->setup(port); + if(ret) + { + fprintf(stderr, "Gui: unable to create websocket on port %u\n", port); + return 1; + } ws_server->addAddress(_addressData, [this](const std::string& address, const WSServerDetails* id, const unsigned char* buf, size_t size) { diff --git a/libraries/Scope/Scope.cpp b/libraries/Scope/Scope.cpp index 28ca13eb6..afd3d9460 100644 --- a/libraries/Scope/Scope.cpp +++ b/libraries/Scope/Scope.cpp @@ -52,7 +52,8 @@ void Scope::ClientInstance::triggerTask(){ } } -void Scope::setup(unsigned int _numChannels, float _sampleRate){ +int Scope::setup(unsigned int _numChannels, float _sampleRate) +{ if(_numChannels > 50) throw std::runtime_error(std::string("Scope::setup(): too many channels (")+std::to_string(_numChannels)+std::string(").")); @@ -61,7 +62,12 @@ void Scope::setup(unsigned int _numChannels, float _sampleRate){ // set up the websocket server ws_server = std::unique_ptr(new WSServer()); - ws_server->setup(5432); + int ret; + if((ret = ws_server->setup(5432))) + { + fprintf(stderr, "Scope: failed to create server: %d\n", ret); + return 1; + } ws_server->addAddress("scope_data", nullptr, nullptr, nullptr, true); ws_server->addAddress("scope_control", [this](const std::string& address, const WSServerDetails* id, const unsigned char* buf, size_t size){ @@ -76,7 +82,12 @@ void Scope::setup(unsigned int _numChannels, float _sampleRate){ // setup the auxiliary tasks scopeTriggerTask = std::unique_ptr(new AuxTaskRT()); - scopeTriggerTask->create("scope-trigger-task", [this](){ c.triggerTask(); }); + if((ret = scopeTriggerTask->create("scope-trigger-task", [this](){ c.triggerTask(); }))) + { + fprintf(stderr, "Scope: failed to create trigger task: %d\n", ret); + return 1; + } + return 0; } void Scope::ClientInstance::start(){ diff --git a/libraries/Scope/Scope.h b/libraries/Scope/Scope.h index 1c1f4f675..d7c681f84 100644 --- a/libraries/Scope/Scope.h +++ b/libraries/Scope/Scope.h @@ -50,7 +50,7 @@ class Scope { * @param numChannels number of channels displayed by the scope. * @param sampleRate sample rate of the data passed in. */ - void setup(unsigned int numChannels, float sampleRate); + int setup(unsigned int numChannels, float sampleRate); void cleanup(); /** diff --git a/libraries/WSServer/WSServer.cpp b/libraries/WSServer/WSServer.cpp index 336a06625..cab245d08 100644 --- a/libraries/WSServer/WSServer.cpp +++ b/libraries/WSServer/WSServer.cpp @@ -77,15 +77,17 @@ void WSServer::sendToAllConnections(std::shared_ptr handler } } -void WSServer::setup(int _port) { +int WSServer::setup(int _port) { port = _port; auto logger = std::make_shared(); server = std::make_shared(logger); server_task = std::unique_ptr(new AuxTaskNonRT()); - server_task->create(std::string("WSServer_")+std::to_string(_port), [this](){ server->serve("/dev/null", port); }); + if(server_task->create(std::string("WSServer_")+std::to_string(_port), [this](){ server->serve("/dev/null", port); })) + return 1; server_task->schedule(); + return 0; } void WSServer::addAddress(const std::string& _address, diff --git a/libraries/WSServer/WSServer.h b/libraries/WSServer/WSServer.h index 2eda9b7d7..97dd509b7 100644 --- a/libraries/WSServer/WSServer.h +++ b/libraries/WSServer/WSServer.h @@ -26,7 +26,7 @@ class WSServer{ WSServer(int _port); ~WSServer(); - void setup(int port); + int setup(int port); void addAddress(const std::string& address, std::function on_receive = nullptr,