]> git.vomp.tv Git - vompclient.git/commitdiff
Server address/port to config. Clean up VConnect
authorChris Tallon <chris@vomp.tv>
Sat, 4 Sep 2021 17:30:05 +0000 (18:30 +0100)
committerChris Tallon <chris@vomp.tv>
Sat, 4 Sep 2021 17:30:05 +0000 (18:30 +0100)
config.cc
config.json.sample
control.cc
event.cc
log.h
main.cc
vconnect.cc
vdpc.cc
vdpc.h

index e76593ff6e81a8225e9256abc015f410d7f4d6f4..d58601aefb008c8a1dc56e98a0c13f7bd08aaf29 100644 (file)
--- 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();
index 8bc0a98804a708687ae6ed379a34119ecf358896..72cbab18ceb4ea79b981e0cac2a959a99af0f2d7 100644 (file)
     "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,
index adbb67d883252ce486a2f92a199fe281186559f5..434e1f5a2f89cbb16f9747f0919196292d710e57 100644 (file)
@@ -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<VConnect*>(m->from));
+        doJustConnected(static_cast<VConnect*>(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();
   }
index c22a3a42008b64989f61ad1a585ae904c4e2f32b..47f2ba1a60311010acef63a1954c4e2c11ee3c8a 100644 (file)
--- 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 b21e26d9bd9d5f38e761f5833933d8a7e92d3924..bc684813243dc9a9939c0da5b615bc353781c309 100644 (file)
--- 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 1ac72562d12c73eedef627d6d6cd109504d7155d..af2dae89558c8fa1728e5cef940dca4113509704 100644 (file)
--- 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");
index e4e0caafe6d2b98c9ccab428fd6fed8e375e2ed0..1528931bbea722ada4c1895eb1b9c988e93698f5 100644 (file)
@@ -106,58 +106,82 @@ void VConnect::threadMethod()
 
   std::unique_lock<std::mutex> 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 23e4552f68ba6d52621c6f98cf7dbb91a7dc3459..3d60d7863d8a3d98272f1dba2c9fd427b38d620c 100644 (file)
--- a/vdpc.cc
+++ b/vdpc.cc
 
 static const char* TAG = "VDPC";
 
-void VDPC::TEMPaddCLIServer(const std::string& cliServer)
-{
-  std::lock_guard<std::mutex> 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 9bb837febd3a0623cae87c8dc0f3b9e3636d3f1e..119ece9468eb56c429208c239138ac16b6d0c092 100644 (file)
--- 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<VDRServer> servers;
     std::mutex serversLock;