From 9a40ffa58bcf0c98284bf3b8c1e420bc5c40da07 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 28 Jan 2006 00:25:58 +0000 Subject: [PATCH] New structure --- defines.h | 4 -- dsock.c | 25 ++++++++----- dsock.h | 3 +- log.c | 22 ++++------- log.h | 3 +- mvpclient.c | 65 ++++++++++++++------------------- mvpclient.h | 5 ++- mvpserver.c | 101 +++++++++++++++++++++------------------------------ mvpserver.h | 3 +- udpreplier.c | 28 ++++++++++---- udpreplier.h | 2 +- vompserver.c | 19 +++++----- 12 files changed, 130 insertions(+), 150 deletions(-) diff --git a/defines.h b/defines.h index 230f0ee..440ca3d 100644 --- a/defines.h +++ b/defines.h @@ -27,8 +27,4 @@ typedef unsigned int UINT; typedef unsigned long ULONG; typedef unsigned long long ULLONG; -ULLONG htonll(ULLONG a); -ULLONG ntohll(ULLONG a); - - #endif diff --git a/dsock.c b/dsock.c index 8a1ee53..8c257f9 100644 --- a/dsock.c +++ b/dsock.c @@ -20,17 +20,26 @@ #include "dsock.h" -DatagramSocket::DatagramSocket(short port) +DatagramSocket::DatagramSocket() { - myPort = port; addrlen = sizeof(struct sockaddr); log = Log::getInstance(); initted = 0; +} + +DatagramSocket::~DatagramSocket() +{ + if (initted) close(socketnum); + initted = 0; +} +bool DatagramSocket::init(short port) +{ + myPort = port; if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { log->log("UDP", Log::CRIT, "Socket error"); - return; + return false; } myAddr.sin_family = AF_INET; // host byte order @@ -41,20 +50,16 @@ DatagramSocket::DatagramSocket(short port) { log->log("UDP", Log::CRIT, "Bind error"); close(socketnum); - return; + return false; } - initted = 1; FD_ZERO(&readfds); FD_SET(socketnum, &readfds); tv.tv_sec = 0; tv.tv_usec = 0; -} -DatagramSocket::~DatagramSocket() -{ - if (initted) close(socketnum); - initted = 0; + initted = 1; + return true; } unsigned char DatagramSocket::waitforMessage(unsigned char how) diff --git a/dsock.h b/dsock.h index 29389b9..33c75dd 100644 --- a/dsock.h +++ b/dsock.h @@ -41,8 +41,9 @@ typedef unsigned char uchar; class DatagramSocket { public: - DatagramSocket(short); // port + DatagramSocket(); ~DatagramSocket(); + bool init(short); // port unsigned char waitforMessage(unsigned char); // int =0-block =1-new wait =2-continue wait int getDataLength(void) const; char *getData(void); // returns a pointer to the data diff --git a/log.c b/log.c index 1f36bb3..a3cb8ce 100755 --- a/log.c +++ b/log.c @@ -44,6 +44,8 @@ Log* Log::getInstance() void Log::upLogLevel() { + if (!initted) return; + if (logLevel == Log::DEBUG) { log("Log", logLevel, "Log level is at its highest already"); @@ -56,6 +58,8 @@ void Log::upLogLevel() void Log::downLogLevel() { + if (!initted) return; + if (logLevel == Log::CRAZY) { log("Log", logLevel, "Log level is at its lowest already"); @@ -66,22 +70,18 @@ void Log::downLogLevel() log("Log", logLevel, "Log level is now %i", logLevel); } -int Log::init(int startLogLevel, char* fileName, int tenabled) +int Log::init(int startLogLevel, char* fileName) { - initted = 1; logLevel = startLogLevel; - enabled = tenabled; - - if (!enabled) return 1; logfile = fopen(fileName, "a"); if (logfile) { + initted = 1; return 1; } else { - enabled = 0; return 0; } } @@ -89,15 +89,15 @@ int Log::init(int startLogLevel, char* fileName, int tenabled) int Log::shutdown() { if (!initted) return 1; + initted = 0; if (logfile) fclose(logfile); return 1; } int Log::log(char *fromModule, int level, char* message, ...) { - if (!instance || !logfile) return 0; + if (!initted) return 0; - if (!enabled) return 1; if (level > logLevel) return 1; char buffer[151]; @@ -149,9 +149,3 @@ int Log::log(char *fromModule, int level, char* message, ...) return 0; } - -int Log::status() -{ - if (instance && logfile) return 1; - else return 0; -} diff --git a/log.h b/log.h index bf45f42..6e4316b 100755 --- a/log.h +++ b/log.h @@ -35,10 +35,9 @@ class Log ~Log(); static Log* getInstance(); - int init(int defaultLevel, char* fileName, int enabled); + int init(int defaultLevel, char* fileName); int shutdown(); int log(char *fromModule, int level, char *message, ...); - int status(); void upLogLevel(); void downLogLevel(); diff --git a/mvpclient.c b/mvpclient.c index 7f24e9a..d3c49c5 100644 --- a/mvpclient.c +++ b/mvpclient.c @@ -28,41 +28,6 @@ MVPClient::MVPClient(int tsocket) recordingManager = NULL; log = Log::getInstance(); loggedIn = false; - -/* - // Get IP address of client for config module - - char ipa[20]; - struct sockaddr_in peer; - socklen_t salen = sizeof(struct sockaddr); - if(getpeername(tsocket, (struct sockaddr*)&peer, &salen) == 0) - { - strcpy(ipa, inet_ntoa(peer.sin_addr)); - } - else - { - ipa[0] = '\0'; - log->log("Client", Log::DEBUG, "Cannot get peer name!"); - } - - const char* configDir = cPlugin::ConfigDirectory(); - if (!configDir) - { - log->log("Client", Log::DEBUG, "No config dir!"); - return; - } - - char configFileName[PATH_MAX]; - snprintf(configFileName, PATH_MAX - strlen(configDir) - strlen(ipa) - 20, "%s/vomp-%s.conf", configDir, ipa); - config.init(configFileName); - - log->log("Client", Log::DEBUG, "Config file name: %s", configFileName); -*/ - -//test(14); - - test2(); - } MVPClient::~MVPClient() @@ -86,6 +51,31 @@ MVPClient::~MVPClient() if (loggedIn) cleanConfig(); } +ULLONG MVPClient::ntohll(ULLONG a) +{ + return htonll(a); +} + +ULLONG MVPClient::htonll(ULLONG a) +{ + #if BYTE_ORDER == BIG_ENDIAN + return a; + #else + ULLONG b = 0; + + b = ((a << 56) & 0xFF00000000000000ULL) + | ((a << 40) & 0x00FF000000000000ULL) + | ((a << 24) & 0x0000FF0000000000ULL) + | ((a << 8) & 0x000000FF00000000ULL) + | ((a >> 8) & 0x00000000FF000000ULL) + | ((a >> 24) & 0x0000000000FF0000ULL) + | ((a >> 40) & 0x000000000000FF00ULL) + | ((a >> 56) & 0x00000000000000FFULL) ; + + return b; + #endif +} + cChannel* MVPClient::channelFromNumber(ULONG channelNumber) { cChannel* channel = NULL; @@ -118,7 +108,6 @@ cChannel* MVPClient::channelFromNumber(ULONG channelNumber) return channel; } - void MVPClient::writeResumeData() { config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition()); @@ -1172,7 +1161,7 @@ IsPresent ? easy to work out tho. Oh it doesn't always work */ - +/* void MVPClient::test2() { log->log("-", Log::DEBUG, "Timers List"); @@ -1195,7 +1184,7 @@ void MVPClient::test2() // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())), // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : ""); } - +*/ /* Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time diff --git a/mvpclient.h b/mvpclient.h index b069f57..55d6835 100644 --- a/mvpclient.h +++ b/mvpclient.h @@ -23,8 +23,8 @@ #include #include -//#include #include +#include #include // sleep @@ -83,6 +83,9 @@ class MVPClient void cleanConfig(); void sendULONG(ULONG ul); + ULLONG ntohll(ULLONG a); + ULLONG htonll(ULLONG a); + void test(int num); void test2(); }; diff --git a/mvpserver.c b/mvpserver.c index c02e75e..63d75f7 100644 --- a/mvpserver.c +++ b/mvpserver.c @@ -26,20 +26,20 @@ MVPServer::MVPServer() MVPServer::~MVPServer() { - if (threadIsActive()) stop(); + stop(); } int MVPServer::stop() { - if (!threadIsActive()) return 0; + if (threadIsActive()) threadCancel(); + close(listeningSocket); - threadCancel(); - log.log("MVPServer", Log::INFO, "Stopped MVPServer thread"); + udpr.shutdown(); - udpr.stop(); + log.log("Main", Log::INFO, "Stopped main server thread"); log.shutdown(); - close(listeningSocket); + config.shutdown(); return 1; } @@ -48,63 +48,70 @@ int MVPServer::run() { if (threadIsActive()) return 1; - log.init(Log::DEBUG, "/tmp/vompserver.log", 1); - - char serverName[1024]; - bool nameSuccess = false; - - // Try to get a server name from vomp.conf + // Start config const char* configDir = cPlugin::ConfigDirectory(); if (!configDir) { - log.log("Client", Log::DEBUG, "No config dir!"); + dsyslog("VOMP: Could not get config dir from VDR"); } else { char configFileName[PATH_MAX]; snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir); - - Config c; - if (c.init(configFileName)) + if (config.init(configFileName)) { - char* fc = c.getValueString("General", "Server name"); - if (fc) - { - strncpy(serverName, fc, 1024); - delete[] fc; - nameSuccess = true; - } + dsyslog("VOMP: Config file found"); + } + else + { + dsyslog("VOMP: Config file not found"); } - c.shutdown(); } - if (!nameSuccess) + // Start logging + + char* cfgLogFilename = config.getValueString("General", "Log file"); + if (cfgLogFilename) { - if (gethostname(serverName, 1024)) // if fail + log.init(Log::DEBUG, cfgLogFilename); + delete[] cfgLogFilename; + } + + // Work out a name for this server + + char* serverName; + + // Try to get from vomp.conf + serverName = config.getValueString("General", "Server name"); + if (!serverName) // If not, get the hostname + { + serverName = new char[1024]; + if (gethostname(serverName, 1024)) // if not, just use "-" { strcpy(serverName, "-"); } } - serverName[1023] = '\0'; + int udpSuccess = udpr.run(serverName); + + delete[] serverName; - if (!udpr.run(serverName)) + if (!udpSuccess) { - log.log("MVPServer", Log::CRIT, "Could not start UDP replier"); - log.shutdown(); + log.log("Main", Log::CRIT, "Could not start UDP replier"); + stop(); return 0; } // start thread here if (!threadStart()) { - log.log("MVPServer", Log::CRIT, "Could not start MVPServer thread"); - udpr.stop(); - log.shutdown(); + log.log("Main", Log::CRIT, "Could not start MVPServer thread"); + stop(); return 0; } - log.log("MVPServer", Log::DEBUG, "MVPServer run success"); + log.log("Main", Log::DEBUG, "MVPServer run success"); return 1; } @@ -148,29 +155,3 @@ void MVPServer::threadMethod() m->run(); } } - - -ULLONG ntohll(ULLONG a) -{ - return htonll(a); -} - -ULLONG htonll(ULLONG a) -{ - #if BYTE_ORDER == BIG_ENDIAN - return a; - #else - ULLONG b = 0; - - b = ((a << 56) & 0xFF00000000000000ULL) - | ((a << 40) & 0x00FF000000000000ULL) - | ((a << 24) & 0x0000FF0000000000ULL) - | ((a << 8) & 0x000000FF00000000ULL) - | ((a >> 8) & 0x00000000FF000000ULL) - | ((a >> 24) & 0x0000000000FF0000ULL) - | ((a >> 40) & 0x000000000000FF00ULL) - | ((a >> 56) & 0x00000000000000FFULL) ; - - return b; - #endif -} diff --git a/mvpserver.h b/mvpserver.h index d64268d..d6d9da8 100644 --- a/mvpserver.h +++ b/mvpserver.h @@ -24,12 +24,12 @@ #include #include #include -#include #include "defines.h" #include "udpreplier.h" #include "mvpclient.h" #include "thread.h" +#include "config.h" class MVPServer : public Thread { @@ -43,6 +43,7 @@ class MVPServer : public Thread private: void threadMethod(); + Config config; Log log; UDPReplier udpr; int listeningSocket; diff --git a/udpreplier.c b/udpreplier.c index ed3bf84..c4b7436 100644 --- a/udpreplier.c +++ b/udpreplier.c @@ -21,22 +21,21 @@ #include "udpreplier.h" UDPReplier::UDPReplier() - : ds(3024) { serverName = NULL; } UDPReplier::~UDPReplier() { - if (threadIsActive()) stop(); - if (serverName) delete[] serverName; - serverName = NULL; + shutdown(); } -int UDPReplier::stop() +int UDPReplier::shutdown() { - if (!threadIsActive()) return 0; - threadCancel(); + if (threadIsActive()) threadCancel(); + + if (serverName) delete[] serverName; + serverName = NULL; return 1; } @@ -47,7 +46,17 @@ int UDPReplier::run(char* tserverName) serverName = new char[strlen(tserverName)+1]; strcpy(serverName, tserverName); - if (!threadStart()) return 0; + if (!ds.init(3024)) + { + shutdown(); + return 0; + } + + if (!threadStart()) + { + shutdown(); + return 0; + } Log::getInstance()->log("UDP", Log::DEBUG, "UDP replier started"); return 1; @@ -62,6 +71,9 @@ void UDPReplier::threadMethod() if (retval == 1) continue; if (!strcmp(ds.getData(), "VOMP")) + { + Log::getInstance()->log("UDP", Log::DEBUG, "UDP request from %s", ds.getFromIPA()); ds.send(ds.getFromIPA(), 3024, serverName, strlen(serverName)); + } } } diff --git a/udpreplier.h b/udpreplier.h index 371e5a1..a16af54 100644 --- a/udpreplier.h +++ b/udpreplier.h @@ -35,7 +35,7 @@ class UDPReplier : public Thread virtual ~UDPReplier(); int run(char* tserverName); - int stop(); + int shutdown(); private: void threadMethod(); diff --git a/vompserver.c b/vompserver.c index 45792f2..23e290a 100644 --- a/vompserver.c +++ b/vompserver.c @@ -30,7 +30,7 @@ #include "mvpserver.h" -static const char *VERSION = "0.0.1"; +static const char *VERSION = "0.1.2"; static const char *DESCRIPTION = "VDR on MVP plugin by Chris Tallon"; class cPluginVompserver : public cPlugin @@ -58,10 +58,17 @@ cPluginVompserver::cPluginVompserver(void) // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT! } +bool cPluginVompserver::Start(void) +{ + // Start any background activities the plugin shall perform. + int success = mvpserver.run(); + if (success) return true; + else return false; +} + cPluginVompserver::~cPluginVompserver() { // Clean up after yourself! - mvpserver.stop(); } @@ -83,14 +90,6 @@ bool cPluginVompserver::Initialize(void) return true; } -bool cPluginVompserver::Start(void) -{ - // Start any background activities the plugin shall perform. - int success = mvpserver.run(); - if (success) return true; - else return false; -} - bool cPluginVompserver::SetupParse(const char *Name, const char *Value) { // Parse your own setup parameters and store their values. -- 2.39.2