From e274ffd83b23dca7a23ac00a548382086eafc562 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Wed, 31 Jan 2018 18:07:43 +0100
Subject: [PATCH] headers: unify API across build options

---
 include/opendht/dht.h              |  4 +-
 include/opendht/dht_interface.h    |  4 +-
 include/opendht/dht_proxy_server.h |  7 +---
 include/opendht/dhtrunner.h        | 62 ++++++++++++++----------------
 include/opendht/securedht.h        | 10 -----
 src/dht_proxy_server.cpp           | 16 ++++----
 src/dhtrunner.cpp                  | 31 +++++++++------
 tools/dhtnode.cpp                  | 23 +++++------
 8 files changed, 67 insertions(+), 90 deletions(-)

diff --git a/include/opendht/dht.h b/include/opendht/dht.h
index 8baf1530..274a34cf 100644
--- a/include/opendht/dht.h
+++ b/include/opendht/dht.h
@@ -290,10 +290,8 @@ public:
 
     std::vector<SockAddr> getPublicAddress(sa_family_t family = 0);
 
-#if OPENDHT_PUSH_NOTIFICATIONS
     void pushNotificationReceived(const std::map<std::string, std::string>&) {}
-    void resubscribe(const unsigned) {}
-#endif // OPENDHT_PUSH_NOTIFICATIONS
+    void resubscribe(unsigned) {}
 
 private:
 
diff --git a/include/opendht/dht_interface.h b/include/opendht/dht_interface.h
index 46dfeda7..7f364196 100644
--- a/include/opendht/dht_interface.h
+++ b/include/opendht/dht_interface.h
@@ -228,7 +228,6 @@ public:
         DHT_LOG.setFilter(f);
     }
 
-#if OPENDHT_PUSH_NOTIFICATIONS
     virtual void setPushNotificationToken(const std::string&) {};
 
     /**
@@ -240,8 +239,7 @@ public:
      * Refresh a listen via a token
      * @param token
      */
-    virtual void resubscribe(const unsigned token) = 0;
-#endif // OPENDHT_PUSH_NOTIFICATIONS
+    virtual void resubscribe(unsigned token) = 0;
 
 protected:
     bool logFilerEnable_ {};
diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index f446d3c5..51dc56aa 100644
--- a/include/opendht/dht_proxy_server.h
+++ b/include/opendht/dht_proxy_server.h
@@ -51,12 +51,7 @@ public:
      * @note if the server fails to start (if port is already used or reserved),
      * it will fails silently
      */
-    DhtProxyServer(std::shared_ptr<DhtRunner> dht,
-                   in_port_t port = 8000.
-#if OPENDHT_PUSH_NOTIFICATIONS
-                   , const std::string& pushServer = ""
-#endif // OPENDHT_PUSH_NOTIFICATIONS
-                   );
+    DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port = 8000, const std::string& pushServer = "");
     virtual ~DhtProxyServer();
 
     DhtProxyServer(const DhtProxyServer& other) = delete;
diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h
index 92872ffe..6814f517 100644
--- a/include/opendht/dhtrunner.h
+++ b/include/opendht/dhtrunner.h
@@ -20,7 +20,6 @@
 
 #pragma once
 
-//#include "securedht.h"
 #include "infohash.h"
 #include "value.h"
 #include "callbacks.h"
@@ -37,10 +36,6 @@
 #include <queue>
 #include <chrono>
 
-#if OPENDHT_PROXY_CLIENT
-#include "dht_proxy_client.h"
-#endif // OPENDHT_PROXY_CLIENT
-
 namespace dht {
 
 struct Node;
@@ -58,6 +53,13 @@ class OPENDHT_PUBLIC DhtRunner {
 public:
     typedef std::function<void(NodeStatus, NodeStatus)> StatusCallback;
 
+    struct Config {
+        SecureDhtConfig dht_config;
+        bool threaded;
+        std::string proxy_server;
+        std::string push_node_id;
+    };
+
     DhtRunner();
     virtual ~DhtRunner();
 
@@ -299,13 +301,6 @@ public:
     void registerCertificate(std::shared_ptr<crypto::Certificate> cert);
     void setLocalCertificateStore(CertificateStoreQuery&& query_method);
 
-    struct Config {
-        SecureDhtConfig dht_config;
-        bool threaded;
-        std::string proxy_server;
-        std::string push_node_id;
-    };
-
     /**
      * @param port: Local port to bind. Both IPv4 and IPv6 will be tried (ANY).
      * @param identity: RSA key pair to use for cryptographic operations.
@@ -377,13 +372,14 @@ public:
      */
     void join();
 
-#if OPENDHT_PROXY_CLIENT
     void setProxyServer(const std::string& proxy, const std::string& pushNodeId = "") {
+#if OPENDHT_PROXY_CLIENT
         if (config_.proxy_server == proxy and config_.push_node_id == pushNodeId)
             return;
         config_.proxy_server = proxy;
         config_.push_node_id = pushNodeId;
         enableProxy(use_proxy and not config_.proxy_server.empty());
+#endif
     }
 
     /**
@@ -393,12 +389,8 @@ public:
      */
     void enableProxy(bool proxify);
 
-#endif // OPENDHT_PROXY_CLIENT
-#if OPENDHT_PROXY_SERVER
-    void forwardAllMessages(bool forward);
-#endif // OPENDHT_PROXY_SERVER
+    /* Push notification methods */
 
-#if OPENDHT_PUSH_NOTIFICATIONS
     /**
      * Updates the push notification device token
      */
@@ -412,8 +404,10 @@ public:
      * Refresh a listen via a token
      * @param token
      */
-    void resubscribe(const unsigned token);
-#endif // OPENDHT_PUSH_NOTIFICATIONS
+    void resubscribe(unsigned token);
+
+    /* Proxy server mothods */
+    void forwardAllMessages(bool forward);
 
 private:
     static constexpr std::chrono::seconds BOOTSTRAP_PERIOD {10};
@@ -433,7 +427,18 @@ private:
         return std::max(status4, status6);
     }
 
+    /** Local DHT instance */
     std::unique_ptr<SecureDht> dht_;
+
+    /** Proxy client instance */
+    std::unique_ptr<SecureDht> dht_via_proxy_;
+
+    /** true if we are currently using a proxy */
+    std::atomic_bool use_proxy {false};
+
+    /** Current configuration */
+    Config config_;
+
     /**
      * reset dht clients
      */
@@ -442,21 +447,7 @@ private:
      * @return the current active DHT
      */
     SecureDht* activeDht() const;
-#if OPENDHT_PROXY_CLIENT
-    /**
-     * true if we are currently using a proxy
-     */
-    std::atomic_bool use_proxy {false};
-    /**
-     * The current proxy client
-     */
-    std::unique_ptr<SecureDht> dht_via_proxy_;
-    Config config_;
 
-#if OPENDHT_PUSH_NOTIFICATIONS
-    std::string pushToken_;
-#endif
-#endif // OPENDHT_PROXY_CLIENT
     /**
      * Store current listeners and translates global tokens for each client.
      */
@@ -503,6 +494,9 @@ private:
     int s4 {-1}, s6 {-1};
     SockAddr bound4 {};
     SockAddr bound6 {};
+
+    /** Push notification token */
+    std::string pushToken_;
 };
 
 }
diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h
index 14c3015c..7c61c983 100644
--- a/include/opendht/securedht.h
+++ b/include/opendht/securedht.h
@@ -293,21 +293,14 @@ public:
         dht_->connectivityChanged();
     }
 
-#if OPENDHT_PROXY_CLIENT
-#if OPENDHT_PUSH_NOTIFICATIONS
     void setPushNotificationToken(const std::string& token = "") {
         dht_->setPushNotificationToken(token);
     }
-#endif
-#endif
 
-#if OPENDHT_PROXY_SERVER
     void forwardAllMessages(bool forward) {
         forward_all_ = forward;
     }
-#endif //OPENDHT_PROXY_SERVER
 
-#if OPENDHT_PUSH_NOTIFICATIONS
     /**
      * Call linked callback with push_notification
      * @param notification to process
@@ -322,7 +315,6 @@ public:
     void resubscribe(const unsigned token) {
         dht_->resubscribe(token);
     }
-#endif // OPENDHT_PUSH_NOTIFICATIONS
 
     void setLoggers(LogMethod error = NOLOG, LogMethod warn = NOLOG, LogMethod debug = NOLOG)
     {
@@ -360,9 +352,7 @@ private:
 
     std::uniform_int_distribution<Value::Id> rand_id {};
 
-#if OPENDHT_PROXY_SERVER
     std::atomic_bool forward_all_ {false};
-#endif //OPENDHT_PROXY_SERVER
 };
 
 const ValueType CERTIFICATE_TYPE = {
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index f25987ea..06582e02 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -37,19 +37,17 @@ using namespace std::placeholders;
 
 namespace dht {
 
-DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port
-#if OPENDHT_PUSH_NOTIFICATIONS
-                   , const std::string& pushServer
-#endif // OPENDHT_PUSH_NOTIFICATIONS
-)
-: dht_(dht)
-#if OPENDHT_PUSH_NOTIFICATIONS
-, pushServer_(pushServer)
-#endif // OPENDHT_PUSH_NOTIFICATIONS
+DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , const std::string& pushServer)
+: dht_(dht) , pushServer_(pushServer)
 {
     // NOTE in c++14, use make_unique
     service_ = std::unique_ptr<restbed::Service>(new restbed::Service());
 
+#if !OPENDHT_PUSH_NOTIFICATIONS
+    if (not pushServer.empty())
+        std::cerr << "Push server defined but built OpenDHT built without push notification support" << std::endl;
+#endif
+
     server_thread = std::thread([this, port]() {
         // Create endpoints
         auto resource = std::make_shared<restbed::Resource>();
diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index 2f354a8e..e511a1c2 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -21,6 +21,10 @@
 #include "dhtrunner.h"
 #include "securedht.h"
 
+#if OPENDHT_PROXY_CLIENT
+#include "dht_proxy_client.h"
+#endif
+
 #ifndef _WIN32
 #include <unistd.h>
 #else
@@ -92,8 +96,8 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, DhtRunner::Config
 
 #if OPENDHT_PROXY_CLIENT
     config_ = config;
-    enableProxy(not config_.proxy_server.empty());
 #endif
+    enableProxy(not config.proxy_server.empty());
 
     running = true;
     if (not config.threaded)
@@ -863,10 +867,10 @@ DhtRunner::activeDht() const
 #endif // OPENDHT_PROXY_CLIENT
 }
 
-#if OPENDHT_PROXY_CLIENT
 void
 DhtRunner::enableProxy(bool proxify)
 {
+#if OPENDHT_PROXY_CLIENT
     if (dht_via_proxy_) {
         dht_via_proxy_->shutdown({});
     }
@@ -908,51 +912,54 @@ DhtRunner::enableProxy(bool proxify)
             });
         }
     }
+#else
+    if (proxify)
+        std::cerr << "DHT proxy requested but OpenDHT built without proxy support." << std::endl;
+#endif
 }
 
-#if OPENDHT_PUSH_NOTIFICATIONS
 
 /**
  * Updates the push notification device token
  */
 void
 DhtRunner::setPushNotificationToken(const std::string& token) {
+#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
     pushToken_ = token;
     if (dht_via_proxy_)
         dht_via_proxy_->setPushNotificationToken(token);
+#endif
 }
 
-#endif // OPENDHT_PUSH_NOTIFICATIONS
-#endif // OPENDHT_PROXY_CLIENT
-
-#if OPENDHT_PROXY_SERVER
 void
 DhtRunner::forwardAllMessages(bool forward)
 {
+#if OPENDHT_PROXY_SERVER
 #if OPENDHT_PROXY_CLIENT
     if (dht_via_proxy_)
         dht_via_proxy_->forwardAllMessages(forward);
 #endif // OPENDHT_PROXY_CLIENT
     if (dht_)
         dht_->forwardAllMessages(forward);
-}
 #endif // OPENDHT_PROXY_SERVER
-
-#if OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
+}
 
 void
 DhtRunner::pushNotificationReceived(const std::map<std::string, std::string>& data) const
 {
+#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
     if (dht_via_proxy_)
         dht_via_proxy_->pushNotificationReceived(data);
+#endif
 }
 
 void
-DhtRunner::resubscribe(const unsigned token)
+DhtRunner::resubscribe(unsigned token)
 {
+#if OPENDHT_PROXY_CLIENT && OPENDHT_PUSH_NOTIFICATIONS
     if (dht_via_proxy_)
         dht_via_proxy_->resubscribe(token);
+#endif
 }
 
-#endif // OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
 }
diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp
index b90e6af5..5370e92c 100644
--- a/tools/dhtnode.cpp
+++ b/tools/dhtnode.cpp
@@ -491,14 +491,10 @@ main(int argc, char **argv)
         config.dht_config.node_config.network = params.network;
         config.dht_config.id = crt;
         config.threaded = true;
-#if OPENDHT_PROXY_CLIENT
         config.proxy_server = params.proxyclient;
         config.push_node_id = "dhtnode";
-#if OPENDHT_PUSH_NOTIFICATIONS
         if (not params.proxyclient.empty())
             dht->setPushNotificationToken(params.devicekey);
-#endif
-#endif //OPENDHT_PROXY_CLIENT
 
         dht->run(params.port, config);
 
@@ -517,15 +513,16 @@ main(int argc, char **argv)
         }
 
 #if OPENDHT_PROXY_SERVER
-            std::map<in_port_t, std::unique_ptr<DhtProxyServer>> proxies;
-            if (params.proxyserver != 0) {
-                proxies.emplace(params.proxyserver, new DhtProxyServer(dht, params.proxyserver
-#if OPENDHT_PUSH_NOTIFICATIONS
-                , params.pushserver
-#endif // OPENDHT_PUSH_NOTIFICATIONS
-                ));
-            }
-#endif //OPENDHT_PROXY_SERVER
+        std::map<in_port_t, std::unique_ptr<DhtProxyServer>> proxies;
+#endif
+        if (params.proxyserver != 0) {
+#if OPENDHT_PROXY_SERVER
+            proxies.emplace(params.proxyserver, new DhtProxyServer(dht, params.proxyserver, params.pushserver));
+#else
+            std::cerr << "DHT proxy server requested but OpenDHT built without proxy server support." << std::endl;
+            exit(EXIT_FAILURE);
+#endif
+        }
 
         if (params.daemonize or params.service)
             while (runner.wait());
-- 
GitLab