]> git.vomp.tv Git - vompclient.git/commitdiff
VDPC: Remove any duplicate server entry from the IPv not preferred
authorChris Tallon <chris@vomp.tv>
Wed, 15 Jun 2022 15:39:38 +0000 (15:39 +0000)
committerChris Tallon <chris@vomp.tv>
Wed, 15 Jun 2022 15:39:38 +0000 (15:39 +0000)
src/vconnect.cc
src/vdpc.cc
src/vdpc.h

index a98368903ccd918689dd53306aa638e71f804720..fc07c93dc3f193826b57357b9a0332ee771461a1 100644 (file)
@@ -174,13 +174,16 @@ void VConnect::threadMethod()
 
     // Multiple servers found
 
+
+    /*
+
     // Special case: If there are two servers returned and they only differ by IP version and IP, select one and connect automatically
 
     if (     (numServers == 2)
           && !vdpc[0].name.compare(vdpc[1].name)       // if the names are the same...
           && (vdpc[0].port == vdpc[1].port)            // and the port number is the same...
           && (vdpc[0].ipVersion != vdpc[1].ipVersion)  // and the IP versions DO NOT match...
-          /* and the IPs don't match - they can't if ipVersions are different */
+          / * and the IPs don't match - they can't if ipVersions are different * /
        )
     {
       int which = 6;
@@ -205,6 +208,9 @@ void VConnect::threadMethod()
       continue; // restart discovery
     }
 
+    */
+
+
     // Now numServers > 1
 
     VServerSelect* vs = new VServerSelect(vdpc, this); // deleted by handleCommand returning BoxStack::DELETE_ME
index 079fa674bd91c809c091ab924f7195a12c264770..e954567e821cd51565f65c1d4aab75e1945eeef4 100644 (file)
@@ -27,6 +27,7 @@
 #include <algorithm>
 
 #include "log.h"
+#include "config.h"
 #include "wol.h"
 
 #include "vdpc.h"
@@ -35,6 +36,8 @@ static const char* TAG = "VDPC";
 
 bool VDPC::init()
 {
+  logger = LogNT::getInstance();
+
   AddServerCallback addFunc( [this] (int ipVersion, const char* ip, const char* name, USHORT port, ULONG version)
   {
     std::lock_guard<std::mutex> lg(serversLock);
@@ -72,7 +75,7 @@ int VDPC::go()
 
   do
   {
-    LogNT::getInstance()->debug(TAG, "Sending broadcasts");
+    logger->debug(TAG, "Sending broadcasts");
 
 #ifdef IPV4
     vdpc4.sendRequest();
@@ -82,7 +85,7 @@ int VDPC::go()
 #endif
 
     waitCond.wait_for(ul, std::chrono::milliseconds(1500), [this]{ return stopNow == true; });
-    LogNT::getInstance()->debug(TAG, "go() wait finished");
+    logger->debug(TAG, "go() wait finished");
 
     if (stopNow) break;
     if (servers.size() == 0) Wol::getInstance()->doWakeUp();
@@ -96,6 +99,8 @@ int VDPC::go()
     vdpc6.stop();
 #endif
 
+  filterServers();
+
   std::sort(servers.begin(), servers.end(), ServerSorter());
   return servers.size();
 }
@@ -118,6 +123,61 @@ const VDRServer& VDPC::operator[](ULONG index) const
   return servers[index];
 }
 
+void VDPC::filterServers()
+{
+  // If the same VDR instance has replied on both IPv4 and IPv6,
+  // remove one based on the config option server-discovery.prefer-ipv
+
+  // It is assumed that we can only get a maximum of two responses from
+  // the same server. The only way this doesn't happen is if two different
+  // VDR-vompservers are running with the same server name in the config.
+  // This is not supported. Results will not be as you would expect.
+
+  Config* config = Config::getInstance();
+  int which = 6;
+  config->getInt("server-discovery", "prefer-ipv", which);
+  logger->debug(TAG, "Auto select IPv{}", which);
+
+  bool removed;
+  do
+  {
+    removed = false;
+
+    for(auto i = servers.begin(); i != servers.end(); i++)
+    {
+      for(auto j = i + 1; j != servers.end(); j++)
+      {
+        if (i->name == j->name)
+        {
+          if (i->ipVersion != which)
+          {
+
+            logger->debug(TAG, "{} found twice, removing {}", i->name, i->ipVersion);
+
+            servers.erase(i);
+            removed = true;
+            break;
+          }
+
+          if (j->ipVersion != which)
+          {
+
+            logger->debug(TAG, "{} found twice, removing {}", j->name, j->ipVersion);
+
+            servers.erase(j);
+            removed = true;
+            break;
+          }
+        }
+      }
+
+      if (removed) break;
+    }
+
+  } while (removed);
+}
+
+
 #ifdef IPV4
 
 // ======================================= VDPC4
index aad164c03315fe8c86d3e671286f373c57bd84cb..6dd058310d33b420dc39d53edd1fff1a6fa1d0c3 100644 (file)
@@ -85,9 +85,12 @@ class ServerSorter
     }
 };
 
+class LogNT;
+
 class VDPC
 {
   private:
+    LogNT* logger{};
     typedef std::function<void(int, const char*, const char*, USHORT, ULONG)> AddServerCallback;
 
   // Nested classes
@@ -154,6 +157,8 @@ class VDPC
     std::mutex waitMutex;
     bool stopNow{};
 
+    void filterServers();
+
 #ifdef IPV4
     VDPC4 vdpc4;
 #endif