diff --git a/src/api_server.cpp b/src/api_server.cpp index e51338b2..fc7db6e5 100644 --- a/src/api_server.cpp +++ b/src/api_server.cpp @@ -58,6 +58,9 @@ void ApiServer::handlePost(http_request request) { if (path == "/load_dll") { loadDllEndpoint(request); } + else if (path == "/unload_dll") { + unloadDllEndpoint(request); + } else if (path == "/start_websocket") { startWebSocket(request); } @@ -185,6 +188,56 @@ void ApiServer::loadDllEndpoint(const http_request& request) { }).wait(); } +// Function for unloading the DLL from the WebSocket server +void ApiServer::unloadDllEndpoint(const http_request& request) { + std::cout << "Unload DLL endpoint called" << std::endl; + + request.extract_json().then([=](json::value jsonData) { + if (!jsonData.has_field(U("dllPaths")) || !jsonData[U("dllPaths")].is_array()) { + request.reply(status_codes::BadRequest, "Missing dllPaths parameter or it is not an array"); + return; + } + + // Collect DLL paths + std::vector dllPathVector; + for (const auto& dllPath : jsonData[U("dllPaths")].as_array()) { + dllPathVector.push_back(dllPath); + } + + std::string wsUri = "ws://127.0.0.1:8081"; // Standard-WebSocket-URI + if (jsonData.has_field(U("wsUri"))) { + wsUri = jsonData[U("wsUri")].as_string(); + } + + // Connect to the WebSocket server + web::websockets::client::websocket_client ws_client; + + try { + ws_client.connect(wsUri).wait(); + + // Send JSON message to unload the DLLs + json::value message; + message[U("unloadDllPaths")] = json::value::array(dllPathVector); + + web::websockets::client::websocket_outgoing_message msg; + msg.set_utf8_message(message.serialize()); + + ws_client.send(msg).wait(); + ws_client.close().wait(); + + json::value response; + response[U("status")] = json::value::string(U("DLL paths sent for unloading")); + request.reply(status_codes::OK, response); + } + catch (const web::websockets::client::websocket_exception& e) { + json::value response; + response[U("status")] = json::value::string(U("Failed to send DLL paths for unloading")); + response[U("error")] = json::value::string(e.what()); + request.reply(status_codes::InternalError, response); + } + }).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) { diff --git a/src/api_server.hpp b/src/api_server.hpp index f5843629..d0caf2eb 100644 --- a/src/api_server.hpp +++ b/src/api_server.hpp @@ -42,6 +42,9 @@ class ApiServer { // Function for loading the DLL via the WebSocket server void loadDllEndpoint(const http_request& request); + // Function for unloading the DLL from the WebSocket server + void unloadDllEndpoint(const http_request& request); + // Starts the WebSocket server void startWebSocket(const http_request& request);