ConnectionManager: add connectDevice(InfoHash)

Change-Id: I5179a05b63b4576f468d76c00f1aa9fe3c2e4083
diff --git a/include/connectionmanager.h b/include/connectionmanager.h
index e459eb3..17300dc 100644
--- a/include/connectionmanager.h
+++ b/include/connectionmanager.h
@@ -72,6 +72,8 @@
  * Used by connectDevice, when the socket is ready
  */
 using ConnectCallback = std::function<void(const std::shared_ptr<ChannelSocket>&, const DeviceId&)>;
+using ConnectCallbackLegacy = std::function<void(const std::shared_ptr<ChannelSocket>&, const dht::InfoHash&)>;
+
 /**
  * Used when an incoming connection is ready
  */
@@ -111,6 +113,13 @@
                        bool noNewSocket = false,
                        bool forceNewSocket = false,
                        const std::string& connType = "");
+    void connectDevice(const dht::InfoHash& deviceId,
+                       const std::string& name,
+                       ConnectCallbackLegacy cb,
+                       bool noNewSocket = false,
+                       bool forceNewSocket = false,
+                       const std::string& connType = "");
+
     void connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
                        const std::string& name,
                        ConnectCallback cb,
diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp
index 6c6de31..507a297 100644
--- a/src/connectionmanager.cpp
+++ b/src/connectionmanager.cpp
@@ -148,6 +148,13 @@
                        bool noNewSocket = false,
                        bool forceNewSocket = false,
                        const std::string& connType = "");
+    void connectDevice(const dht::InfoHash& deviceId,
+                       const std::string& uri,
+                       ConnectCallbackLegacy cb,
+                       bool noNewSocket = false,
+                       bool forceNewSocket = false,
+                       const std::string& connType = "");
+
     void connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
                        const std::string& name,
                        ConnectCallback cb,
@@ -603,6 +610,53 @@
 }
 
 void
+ConnectionManager::Impl::connectDevice(const dht::InfoHash& deviceId,
+                                       const std::string& name,
+                                       ConnectCallbackLegacy cb,
+                                       bool noNewSocket,
+                                       bool forceNewSocket,
+                                       const std::string& connType)
+{
+    if (!dht()) {
+        cb(nullptr, deviceId);
+        return;
+    }
+    if (deviceId.toString() == identity().second->getLongId().toString()) {
+        cb(nullptr, deviceId);
+        return;
+    }
+    findCertificate(deviceId,
+                    [w = weak(),
+                     deviceId,
+                     name,
+                     cb = std::move(cb),
+                     noNewSocket,
+                     forceNewSocket,
+                     connType](const std::shared_ptr<dht::crypto::Certificate>& cert) {
+                        if (!cert) {
+                            if (auto shared = w.lock())
+                                if (shared->config_->logger)
+                                    shared->config_->logger->error(
+                                        "No valid certificate found for device {}",
+                                        deviceId);
+                            cb(nullptr, deviceId);
+                            return;
+                        }
+                        if (auto shared = w.lock()) {
+                            shared->connectDevice(cert,
+                                                  name,
+                                                  [cb, deviceId](const std::shared_ptr<ChannelSocket>& sock, const DeviceId& did){
+                                                     cb(sock, deviceId);
+                                                  },
+                                                  noNewSocket,
+                                                  forceNewSocket,
+                                                  connType);
+                        } else
+                            cb(nullptr, deviceId);
+                    });
+}
+
+void
 ConnectionManager::Impl::connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
                                        const std::string& name,
                                        ConnectCallback cb,
@@ -1535,6 +1589,18 @@
 }
 
 void
+ConnectionManager::connectDevice(const dht::InfoHash& deviceId,
+                                 const std::string& name,
+                                 ConnectCallbackLegacy cb,
+                                 bool noNewSocket,
+                                 bool forceNewSocket,
+                                 const std::string& connType)
+{
+    pimpl_->connectDevice(deviceId, name, std::move(cb), noNewSocket, forceNewSocket, connType);
+}
+
+
+void
 ConnectionManager::connectDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
                                  const std::string& name,
                                  ConnectCallback cb,