Skip to content

Commit

Permalink
Scope,WSServer: check return values, return them and warn on error
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliomoro committed Jun 1, 2024
1 parent ee51d59 commit 8b8247f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
3 changes: 3 additions & 0 deletions core/SchedulableThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion libraries/Gui/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ int Gui::setup(unsigned int port, std::string address)

// Set up the websocket server
ws_server = std::unique_ptr<WSServer>(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)
{
Expand Down
17 changes: 14 additions & 3 deletions libraries/Scope/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(")."));

Expand All @@ -61,7 +62,12 @@ void Scope::setup(unsigned int _numChannels, float _sampleRate){

// set up the websocket server
ws_server = std::unique_ptr<WSServer>(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){
Expand All @@ -76,7 +82,12 @@ void Scope::setup(unsigned int _numChannels, float _sampleRate){

// setup the auxiliary tasks
scopeTriggerTask = std::unique_ptr<AuxTaskRT>(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(){
Expand Down
2 changes: 1 addition & 1 deletion libraries/Scope/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
/**
Expand Down
6 changes: 4 additions & 2 deletions libraries/WSServer/WSServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ void WSServer::sendToAllConnections(std::shared_ptr<WSServerDataHandler> handler
}
}

void WSServer::setup(int _port) {
int WSServer::setup(int _port) {
port = _port;

auto logger = std::make_shared<seasocks::IgnoringLogger>();
server = std::make_shared<seasocks::Server>(logger);

server_task = std::unique_ptr<AuxTaskNonRT>(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,
Expand Down
2 changes: 1 addition & 1 deletion libraries/WSServer/WSServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const std::string&, const WSServerDetails*, const unsigned char*, size_t)> on_receive = nullptr,
Expand Down

0 comments on commit 8b8247f

Please sign in to comment.