Skip to content

Commit

Permalink
Extend API Server with WebSocket Control Endpoints
Browse files Browse the repository at this point in the history
- Added '/start_websocket' and '/stop_websocket' endpoints to allow remote control of the WebSocket server
- '/start_websocket': Starts the WebSocket server if not already running, using the path provided in the POST request
- '/stop_websocket': Stops the WebSocket server if it is running
- Added 'isWebSocketRunning' atomic flag to track the WebSocket server state and 'webSocketThread' to manage the server’s lifecycle
- Improved handling of WebSocket server status responses to ensure accurate feedback on server operations

These changes make the WebSocket server controllable through API calls, supporting remote server management in connected environments.
  • Loading branch information
arkadiusz committed Oct 30, 2024
1 parent 21bd053 commit 64eddad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
58 changes: 55 additions & 3 deletions src/api_server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "api_server.hpp"
// #include "handler.hpp"
#include <cstdlib>
#include <cpprest/ws_client.h>
#include <filesystem>
#include <iostream>
Expand All @@ -24,7 +25,7 @@ void ApiServer::stop() {
listener_.close().wait();
}

// Main GET handler, forwards requests to specific functions
// GET handler
void ApiServer::handleGet(http_request request) {
auto path = uri::decode(request.relative_uri().path());
if (path == "/status") {
Expand All @@ -51,12 +52,19 @@ void ApiServer::handleGet(http_request request) {
}
}

// POST handler forwards to `loadDll` function
// POST handler
void ApiServer::handlePost(http_request request) {
auto path = uri::decode(request.relative_uri().path());
if (path == "/load_dll") {
loadDllEndpoint(request);
} else {
}
else if (path == "/start_websocket") {
startWebSocket(request);
}
else if (path == "/stop_websocket") {
stopWebSocket(request);
}
else {
request.reply(status_codes::NotFound, "Endpoint not found");
}
}
Expand Down Expand Up @@ -177,6 +185,50 @@ void ApiServer::loadDllEndpoint(const http_request& request) {
}).wait();
}

// Starts the WebSocket server by executing the provided path to `wsDll` executable.
void ApiServer::startWebSocket(const http_request& request) {
request.extract_json().then([this, &request](json::value jsonData) {
if (!jsonData.has_field(U("path"))) {
request.reply(status_codes::BadRequest, "Missing path parameter for WebSocket executable");
return;
}

std::string wsPath = jsonData[U("path")].as_string();
if (isWebSocketRunning.load()) {
request.reply(status_codes::BadRequest, "WebSocket server is already running");
return;
}

webSocketThread = std::thread([wsPath, this]() {
std::string command = wsPath + " &";
std::cout << "Starting WebSocket server with command: " << command << std::endl;
std::system(command.c_str());
});

isWebSocketRunning = true;
request.reply(status_codes::OK, "WebSocket server started successfully");
}).wait();
}

// Stop the WebSocket server
void ApiServer::stopWebSocket(const http_request& request) {
if (!isWebSocketRunning.load()) {
request.reply(status_codes::BadRequest, "WebSocket server is not running");
return;
}

// Stoppen des WebSocket-Servers durch Beenden des Threads
if (webSocketThread.joinable()) {
std::cout << "Stopping WebSocket server..." << std::endl;
std::system("pkill -f wsDll");
webSocketThread.join();
isWebSocketRunning = false;
request.reply(status_codes::OK, "WebSocket server stopped successfully");
} else {
request.reply(status_codes::InternalError, "Failed to stop WebSocket server");
}
}


/*
// Sehe Header-Datei
Expand Down
11 changes: 11 additions & 0 deletions src/api_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <thread>
#include <atomic>
#include <string>

using namespace web;
Expand All @@ -22,6 +24,9 @@ class ApiServer {
private:
http_listener listener_;

std::atomic<bool> isWebSocketRunning{false};
std::thread webSocketThread;

// Handler for GET-Requests
void handleGet(http_request request);

Expand All @@ -37,6 +42,12 @@ class ApiServer {
// Function for loading the DLL via the WebSocket server
void loadDllEndpoint(const http_request& request);

// Starts the WebSocket server
void startWebSocket(const http_request& request);

// Stops the WebSocket server
void stopWebSocket(const http_request& request);

/*
// Konflikt mit fmt aus der OmniscopeSampler.hpp. Nicht lösbar ind er Kurzen Zeit
// Function for searching for devices
Expand Down

0 comments on commit 64eddad

Please sign in to comment.