UDP API port is now stored in and read from global config

This commit is contained in:
Thulinma 2023-05-01 02:09:21 +02:00
parent 40df03439c
commit 0af85de22d
13 changed files with 61 additions and 39 deletions

View file

@ -3,6 +3,7 @@
#include "defines.h"
#include "procs.h"
#include "stream.h"
#include <signal.h>
#include <string.h>
#include <sys/types.h>
@ -465,9 +466,7 @@ pid_t Util::Procs::StartPiped(const char *const *argv, int *fdin, int *fdout, in
ERROR_MSG("%s trigger failed to execute %s: %s", trggr, argv[0], strerror(errno));
JSON::Value j;
j["trigger_fail"] = trggr;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(j.toString());
Util::sendUDPApi(j);
std::cout << getenv("MIST_TRIG_DEF");
_exit(42);
}

View file

@ -744,6 +744,18 @@ JSON::Value Util::getInputBySource(const std::string &filename, bool isProvider)
return ret;
}
/// Sends a message to the local UDP API port
void Util::sendUDPApi(JSON::Value & cmd){
HTTP::URL UDPAddr(getGlobalConfig("udpApi").asStringRef());
if (UDPAddr.protocol != "udp"){
FAIL_MSG("Local UDP API address not defined; can't send command to MistController!");
return;
}
Socket::UDPConnection uSock;
uSock.SetDestination(UDPAddr.host, UDPAddr.getPort());
uSock.SendNow(cmd.toString());
}
/// Attempt to start a push for streamname to target.
/// streamname MUST be pre-sanitized
/// target gets variables replaced and may be altered by the PUSH_OUT_START trigger response.

View file

@ -26,6 +26,7 @@ namespace Util{
JSON::Value getStreamConfig(const std::string &streamname);
JSON::Value getGlobalConfig(const std::string &optionName);
JSON::Value getInputBySource(const std::string &filename, bool isProvider = false);
void sendUDPApi(JSON::Value & cmd);
uint8_t getStreamStatus(const std::string &streamname);
uint8_t getStreamStatusPercentage(const std::string &streamname);
bool checkException(const JSON::Value &ex, const std::string &useragent);

View file

@ -25,6 +25,7 @@
#include "triggers.h"
#include "util.h"
#include "json.h"
#include "stream.h"
#include <string.h> //for strncmp
namespace Triggers{
@ -34,9 +35,7 @@ namespace Triggers{
j["trigger_stat"]["name"] = trigger;
j["trigger_stat"]["ms"] = Util::bootMS() - millis;
j["trigger_stat"]["ok"] = ok;
Socket::UDPConnection uSock;
uSock.SetDestination(UDP_API_HOST, UDP_API_PORT);
uSock.SendNow(j.toString());
Util::sendUDPApi(j);
}
///\brief Handles a trigger by sending a payload to a destination.

View file

@ -4,6 +4,7 @@
#include "defines.h"
#include "encode.h"
#include "url.h"
#include <sstream>
/// Helper function to check if the given c-string is numeric or not
static bool is_numeric(const char *str){
@ -161,6 +162,13 @@ uint16_t HTTP::URL::getPort() const{
return atoi(port.c_str());
}
/// Sets the port in numeric format
void HTTP::URL::setPort(uint16_t newPort){
std::stringstream st;
st << newPort;
port = st.str();
}
/// Returns the default port for the protocol in numeric format
uint16_t HTTP::URL::getDefaultPort() const{
if (protocol == "http"){return 80;}

View file

@ -14,6 +14,7 @@ namespace HTTP{
public:
URL(const std::string &url = "");
uint16_t getPort() const;
void setPort(uint16_t newPort);
uint16_t getDefaultPort() const;
std::string getExt() const;
std::string getUrl() const;