Backported UDP API from Pro edition

This commit is contained in:
Thulinma 2019-09-09 13:31:20 +02:00
parent 4c3dfa829f
commit 309d39eab7
4 changed files with 40 additions and 0 deletions

View file

@ -157,3 +157,12 @@ static inline void show_stackframe(){}
#define SIMUL_TRACKS 20 #define SIMUL_TRACKS 20
#ifndef UDP_API_HOST
#define UDP_API_HOST "localhost"
#endif
#ifndef UDP_API_PORT
#define UDP_API_PORT 4242
#endif

View file

@ -360,6 +360,8 @@ int main_loop(int argc, char **argv){
tthread::thread statsThread(Controller::SharedMemStats, &Controller::conf); tthread::thread statsThread(Controller::SharedMemStats, &Controller::conf);
// start monitoring thread // start monitoring thread
tthread::thread monitorThread(statusMonitor, 0); tthread::thread monitorThread(statusMonitor, 0);
// start UDP API thread
tthread::thread UDPAPIThread(Controller::handleUDPAPI, 0);
// start main loop // start main loop
while (Controller::conf.is_active){ while (Controller::conf.is_active){
@ -380,6 +382,8 @@ int main_loop(int argc, char **argv){
statsThread.join(); statsThread.join();
HIGH_MSG("Joining monitor thread..."); HIGH_MSG("Joining monitor thread...");
monitorThread.join(); monitorThread.join();
HIGH_MSG("Joining UDP API thread...");
UDPAPIThread.join();
// write config // write config
tthread::lock_guard<tthread::mutex> guard(Controller::logMutex); tthread::lock_guard<tthread::mutex> guard(Controller::logMutex);
Controller::writeConfigToDisk(); Controller::writeConfigToDisk();

View file

@ -5,6 +5,7 @@
#include <mist/config.h> #include <mist/config.h>
#include <mist/defines.h> #include <mist/defines.h>
#include <mist/timing.h> #include <mist/timing.h>
#include <mist/procs.h>
#include "controller_api.h" #include "controller_api.h"
#include "controller_storage.h" #include "controller_storage.h"
#include "controller_streams.h" #include "controller_streams.h"
@ -360,6 +361,31 @@ int Controller::handleAPIConnection(Socket::Connection & conn){
return 0; 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<tthread::mutex> 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 /// Local-only helper function that checks for duplicate protocols and removes them
static void removeDuplicateProtocols(){ static void removeDuplicateProtocols(){
JSON::Value & P = Controller::Storage["config"]["protocols"]; JSON::Value & P = Controller::Storage["config"]["protocols"];

View file

@ -8,4 +8,5 @@ namespace Controller {
int handleAPIConnection(Socket::Connection & conn); int handleAPIConnection(Socket::Connection & conn);
void handleAPICommands(JSON::Value & Request, JSON::Value & Response); void handleAPICommands(JSON::Value & Request, JSON::Value & Response);
void handleWebSocket(HTTP::Parser & H, Socket::Connection & C); void handleWebSocket(HTTP::Parser & H, Socket::Connection & C);
void handleUDPAPI(void * np);
} }