From 309d39eab70295187c74fd35d4b026ff9b313a78 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 9 Sep 2019 13:31:20 +0200 Subject: [PATCH] Backported UDP API from Pro edition --- lib/defines.h | 9 +++++++++ src/controller/controller.cpp | 4 ++++ src/controller/controller_api.cpp | 26 ++++++++++++++++++++++++++ src/controller/controller_api.h | 1 + 4 files changed, 40 insertions(+) diff --git a/lib/defines.h b/lib/defines.h index 01988d20..7a6abfdc 100644 --- a/lib/defines.h +++ b/lib/defines.h @@ -157,3 +157,12 @@ static inline void show_stackframe(){} #define SIMUL_TRACKS 20 + +#ifndef UDP_API_HOST +#define UDP_API_HOST "localhost" +#endif + +#ifndef UDP_API_PORT +#define UDP_API_PORT 4242 +#endif + diff --git a/src/controller/controller.cpp b/src/controller/controller.cpp index 6db2e2c5..06c8b075 100644 --- a/src/controller/controller.cpp +++ b/src/controller/controller.cpp @@ -360,6 +360,8 @@ int main_loop(int argc, char **argv){ tthread::thread statsThread(Controller::SharedMemStats, &Controller::conf); // start monitoring thread tthread::thread monitorThread(statusMonitor, 0); + // start UDP API thread + tthread::thread UDPAPIThread(Controller::handleUDPAPI, 0); // start main loop while (Controller::conf.is_active){ @@ -380,6 +382,8 @@ int main_loop(int argc, char **argv){ statsThread.join(); HIGH_MSG("Joining monitor thread..."); monitorThread.join(); + HIGH_MSG("Joining UDP API thread..."); + UDPAPIThread.join(); // write config tthread::lock_guard guard(Controller::logMutex); Controller::writeConfigToDisk(); diff --git a/src/controller/controller_api.cpp b/src/controller/controller_api.cpp index 669f4acd..6346f037 100644 --- a/src/controller/controller_api.cpp +++ b/src/controller/controller_api.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "controller_api.h" #include "controller_storage.h" #include "controller_streams.h" @@ -360,6 +361,31 @@ int Controller::handleAPIConnection(Socket::Connection & conn){ return 0; } +void Controller::handleUDPAPI(void * np){ + Socket::UDPConnection uSock(true); + if (!uSock.bind(UDP_API_PORT, UDP_API_HOST)){ + FAIL_MSG("Could not open local API UDP socket - not all functionality will be available"); + return; + } + Util::Procs::socketList.insert(uSock.getSock()); + while (Controller::conf.is_active){ + if (uSock.Receive()){ + MEDIUM_MSG("UDP API: %s", uSock.data); + JSON::Value Request = JSON::fromString(uSock.data, uSock.data_len); + Request["minimal"] = true; + JSON::Value Response; + if (Request.isObject()){ + tthread::lock_guard guard(configMutex); + handleAPICommands(Request, Response); + }else{ + WARN_MSG("Invalid API command received over UDP: %s", uSock.data); + } + }else{ + Util::sleep(500); + } + } +} + /// Local-only helper function that checks for duplicate protocols and removes them static void removeDuplicateProtocols(){ JSON::Value & P = Controller::Storage["config"]["protocols"]; diff --git a/src/controller/controller_api.h b/src/controller/controller_api.h index e274b178..6c348716 100644 --- a/src/controller/controller_api.h +++ b/src/controller/controller_api.h @@ -8,4 +8,5 @@ namespace Controller { int handleAPIConnection(Socket::Connection & conn); void handleAPICommands(JSON::Value & Request, JSON::Value & Response); void handleWebSocket(HTTP::Parser & H, Socket::Connection & C); + void handleUDPAPI(void * np); }