From 035c7a25a8a0fef5aa74b4c449acc525fbfbf5cd Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 4 Sep 2021 18:30:05 +0100 Subject: [PATCH] Server address/port to config. Clean up VConnect --- config.cc | 22 ++++++----- config.json.sample | 12 +++++- control.cc | 6 +-- event.cc | 2 +- log.h | 12 +++++- main.cc | 2 +- vconnect.cc | 94 +++++++++++++++++++++++++++++----------------- vdpc.cc | 6 --- vdpc.h | 4 +- 9 files changed, 100 insertions(+), 60 deletions(-) diff --git a/config.cc b/config.cc index e76593f..d58601a 100644 --- a/config.cc +++ b/config.cc @@ -54,17 +54,19 @@ bool Config::loadFile() jconfig.clear(); std::ifstream configFile("config.json"); - - Json::CharReaderBuilder builder; - builder["collectComments"] = false; - builder["allowTrailingCommas"] = true; // FIXME - std::string errs; - - bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs); - if (!ok) + if (configFile.is_open()) { - std::cout << errs << std::endl; - return false; + Json::CharReaderBuilder builder; + builder["collectComments"] = false; + builder["allowTrailingCommas"] = true; // FIXME + std::string errs; + + bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs); + if (!ok) + { + std::cout << errs << std::endl; + return false; + } } applyDefaults(); diff --git a/config.json.sample b/config.json.sample index 8bc0a98..72cbab1 100644 --- a/config.json.sample +++ b/config.json.sample @@ -23,6 +23,16 @@ "level": "debug" }, + "server": + { + // "server" has no defaults + // Normally vompclient will use IPv4 UDP broadcasts and IPv6 multicast to find servers + // Entering an address and optionally a port will disable discovery + + "address": "vdrserver.example.com", + "port": 3024 + } + "input": { "mod_cec_enabled": true, @@ -32,7 +42,7 @@ "input_lirc": { - // This is an example input_lirc section + // input_lirc has no defaults "lirc_ip": "192.0.2.0", "lirc_port": 8765, diff --git a/control.cc b/control.cc index adbb67d..434e1f5 100644 --- a/control.cc +++ b/control.cc @@ -399,7 +399,7 @@ void Control::run() } else { - VConnect* vconnect = new VConnect(); + VConnect* vconnect = new VConnect(); // FIXME never deleted? boxstack->add(vconnect); vconnect->run(); } @@ -469,7 +469,7 @@ void Control::processMessage(Message* m) case Message::VDR_CONNECTED: { - doJustConnected(static_cast(m->from)); + doJustConnected(static_cast(m->from)); // FIXME delete from here? break; } case Message::SCREENSHOT: @@ -726,7 +726,7 @@ void Control::doFromTheTop(bool which) // at this point, everything should be reset to first-go - VConnect* vconnect = new VConnect(); + VConnect* vconnect = new VConnect(); // FIXME never deleted? boxstack->add(vconnect); vconnect->run(); } diff --git a/event.cc b/event.cc index c22a3a4..47f2ba1 100644 --- a/event.cc +++ b/event.cc @@ -42,7 +42,7 @@ void Event::loadinfos(UINT channelid) if (movieID == 0 && seriesID == 0) { vdr->getScraperEventType(channelid, id, movieID, seriesID, episodeID, epgImage); - LogNT::getInstance()->debug(TAG, "Got Scraper EventType {} {}, {} {} {} {}", + LogNT::getInstance()->debug(TAG, "Got Scraper EventType {} {}, {} {} {}", id, channelid, movieID, seriesID, episodeID); } diff --git a/log.h b/log.h index b21e26d..bc68481 100644 --- a/log.h +++ b/log.h @@ -91,7 +91,17 @@ class LogNT } *outstream << " " << std::this_thread::get_id() << " " << TAG << " - "; - fmt::print(*outstream, fmtString, args...); + + try + { + fmt::print(*outstream, fmtString, args...); + } + catch (std::exception& e) + { + fmt::print(*outstream, "<<< EXCEPTION GENERATED BY THIS LOG MESSAGE >>>"); + } + + *outstream << std::endl; } diff --git a/main.cc b/main.cc index 1ac7256..af2dae8 100644 --- a/main.cc +++ b/main.cc @@ -103,7 +103,7 @@ int main(int argc, char** argv) config->set("main", "daemonize", false); break; case 's': - config->set("main", "argv_server", optarg); + config->set("server", "address", optarg); break; case '?': printf("Vompclient\n\n"); diff --git a/vconnect.cc b/vconnect.cc index e4e0caa..1528931 100644 --- a/vconnect.cc +++ b/vconnect.cc @@ -106,58 +106,82 @@ void VConnect::threadMethod() std::unique_lock ul(threadMutex, std::defer_lock); - std::string commandLineServer; - Config::getInstance()->getString("main", "argv_server", commandLineServer); + VDRServer configServerStr; + const VDRServer* selectedServerStr = NULL; - if (!vdpc.init()) + bool serverFromConfig{}; // FIXME Redesign all this. + + if (Config::getInstance()->getString("server", "address", configServerStr.ip)) { - logger->crit(TAG, "Failed to init VDPC"); - return; - } + configServerStr.port = 3024; + int newPort; + if (Config::getInstance()->getInt("server", "port", newPort)) + { + configServerStr.port = newPort; + } - do + selectedServerStr = &configServerStr; + serverFromConfig = true; + } + else // No server specified in config. Start VDPC { - if (!commandLineServer.empty()) // Server is specified, fake a servers array + if (!vdpc.init()) { - //servers.emplace_back(commandLineServer, "", 3024, 0); - vdpc.TEMPaddCLIServer(commandLineServer); // FIXME move to new config system NCONFIG + logger->crit(TAG, "Failed to init VDPC"); + return; } - else + } + + + do + { + if (!selectedServerStr) { setOneLiner(tr("Locating server")); draw(); boxstack->update(this); vdpc.go(); if (threadReqQuit) return; - } - for (ULONG i = 0; i < vdpc.numServers(); i++) - logger->info(TAG, "Found server: {} {} {} {}", vdpc[i].ipVersion, vdpc[i].ip.c_str(), vdpc[i].name.c_str(), vdpc[i].port, vdpc[i].version); + for (ULONG i = 0; i < vdpc.numServers(); i++) + logger->info(TAG, "Found server: {} {} {} {}", vdpc[i].ipVersion, vdpc[i].ip.c_str(), vdpc[i].name.c_str(), vdpc[i].port, vdpc[i].version); - if (vdpc.numServers() == 1) - { - selectedServer = 0; - } - else - { - selectedServer = -1; - VServerSelect* vs = new VServerSelect(vdpc, this); - vs->draw(); - boxstack->add(vs); - boxstack->update(vs); - - ul.lock(); - if (threadReqQuit) { ul.unlock(); return; } - threadCond.wait(ul); - ul.unlock(); + if (vdpc.numServers() == 1) + { + selectedServer = 0; + selectedServerStr = &vdpc[0]; + } + else + { + selectedServer = -1; + VServerSelect* vs = new VServerSelect(vdpc, this); // FIXME is this deleted? + vs->draw(); + boxstack->add(vs); + boxstack->update(vs); + + ul.lock(); + if (threadReqQuit) { ul.unlock(); return; } + threadCond.wait(ul); + + // This thread has been notified by VServerSelect. selectedServer is now an index into vdpc of the chosen server + + selectedServerStr = &vdpc[selectedServer]; + + ul.unlock(); + } + + if (threadReqQuit) return; } - if (threadReqQuit) return; - logger->info(TAG, "Connecting to server at {} {}", vdpc[selectedServer].ip.c_str(), vdpc[selectedServer].port); - Wol::getInstance()->setWakeUpIP(vdpc[selectedServer].ip.c_str()); - vdr->setServerIP(vdpc[selectedServer].ip.c_str()); - vdr->setServerPort(vdpc[selectedServer].port); + logger->info(TAG, "Connecting to server at {} {}", selectedServerStr->ip, selectedServerStr->port); + Wol::getInstance()->setWakeUpIP(selectedServerStr->ip.c_str()); + vdr->setServerIP(selectedServerStr->ip.c_str()); + vdr->setServerPort(selectedServerStr->port); + + // In case of go around, clear selectedServerStr here if it was discovered + if (!serverFromConfig) selectedServerStr = NULL; + setOneLiner(tr("Connecting to VDR")); draw(); diff --git a/vdpc.cc b/vdpc.cc index 23e4552..3d60d78 100644 --- a/vdpc.cc +++ b/vdpc.cc @@ -33,12 +33,6 @@ static const char* TAG = "VDPC"; -void VDPC::TEMPaddCLIServer(const std::string& cliServer) -{ - std::lock_guard lg(serversLock); - servers.emplace_back(4, cliServer, "", 3024, 0); -} - bool VDPC::init() { AddServerCallback addFunc( [this] (int ipVersion, const char* ip, const char* name, USHORT port, ULONG version) diff --git a/vdpc.h b/vdpc.h index 9bb837f..119ece9 100644 --- a/vdpc.h +++ b/vdpc.h @@ -50,6 +50,8 @@ struct VDRServer USHORT port; ULONG version; + VDRServer() {} + VDRServer(int tipVersion, const std::string tip, const std::string tname, USHORT tport, ULONG tversion) : ipVersion(tipVersion), ip(tip), name(tname), port(tport), version(tversion) {} }; @@ -130,8 +132,6 @@ class VDPC ULONG numServers() const; const VDRServer& operator[](ULONG index) const; - void TEMPaddCLIServer(const std::string&); // FIXME NCONFIG - private: std::vector servers; std::mutex serversLock; -- 2.39.2