From ca693a6ac11fa88987537197fee52522489a40ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 12 Jan 2018 13:19:55 -0500
Subject: [PATCH] pushNotificationReceived: take a map<string, string>

JSON is used for the DHT proxy protocol and to communicate with gorush.
Push notifications services might use any format, not necessarily JSON.
Push values are received as a map<string, string>, so receive them in
that format.
---
 include/opendht/dht.h              |  8 +-------
 include/opendht/dht_interface.h    | 11 +++--------
 include/opendht/dht_proxy_client.h |  2 +-
 include/opendht/dhtrunner.h        |  3 +--
 include/opendht/securedht.h        |  2 +-
 src/dht_proxy_client.cpp           | 25 ++++++++++++++-----------
 src/dhtrunner.cpp                  | 21 +++++----------------
 tools/dhtnode.cpp                  |  2 +-
 8 files changed, 27 insertions(+), 47 deletions(-)

diff --git a/include/opendht/dht.h b/include/opendht/dht.h
index 3cbd885a..0b1228c4 100644
--- a/include/opendht/dht.h
+++ b/include/opendht/dht.h
@@ -291,13 +291,7 @@ public:
     std::vector<SockAddr> getPublicAddress(sa_family_t family = 0);
 
 #if OPENDHT_PUSH_NOTIFICATIONS
-    /**
-     * Call linked callback with a push notification
-     * @param notification to process
-     */
-    void pushNotificationReceived(const Json::Value&) {
-        // Ignore this
-    }
+    void pushNotificationReceived(const std::map<std::string, std::string>&) {}
     void resubscribe(const unsigned) {}
 #endif // OPENDHT_PUSH_NOTIFICATIONS
 
diff --git a/include/opendht/dht_interface.h b/include/opendht/dht_interface.h
index c56476fe..0d3a9b2b 100644
--- a/include/opendht/dht_interface.h
+++ b/include/opendht/dht_interface.h
@@ -28,13 +28,6 @@ public:
     DhtInterface() = default;
     virtual ~DhtInterface() = default;
 
-#if OPENDHT_PROXY_CLIENT
-    //virtual void startProxy() {};
-#if OPENDHT_PUSH_NOTIFICATIONS
-    virtual void setPushNotificationToken(const std::string& token) {};
-#endif
-#endif
-
     // [[deprecated]]
     using Status = NodeStatus;
     // [[deprecated]]
@@ -236,11 +229,13 @@ public:
     }
 
 #if OPENDHT_PUSH_NOTIFICATIONS
+    virtual void setPushNotificationToken(const std::string&) {};
+
     /**
      * Call linked callback with a push notification
      * @param notification to process
      */
-    virtual void pushNotificationReceived(const Json::Value& notification) = 0;
+    virtual void pushNotificationReceived(const std::map<std::string, std::string>& data) = 0;
     /**
      * Refresh a listen via a token
      * @param token
diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h
index afa97513..bf8aa4ff 100644
--- a/include/opendht/dht_proxy_client.h
+++ b/include/opendht/dht_proxy_client.h
@@ -174,7 +174,7 @@ public:
      * Call linked callback with a push notification
      * @param notification to process
      */
-    void pushNotificationReceived(const Json::Value& notification);
+    void pushNotificationReceived(const std::map<std::string, std::string>& notification);
     /**
      * Refresh a listen via a token
      * @param token
diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h
index 67c0cbf0..5f955cd8 100644
--- a/include/opendht/dhtrunner.h
+++ b/include/opendht/dhtrunner.h
@@ -402,8 +402,7 @@ public:
     /**
      * Insert a push notification to process for OpenDHT
      */
-    void pushNotificationReceived(const std::string& notification) const;
-    void pushNotificationReceived(const Json::Value& notification) const;
+    void pushNotificationReceived(const std::map<std::string, std::string>& data) const;
     /**
      * Refresh a listen via a token
      * @param token
diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h
index 0f0b96c2..732e2b97 100644
--- a/include/opendht/securedht.h
+++ b/include/opendht/securedht.h
@@ -312,7 +312,7 @@ public:
      * Call linked callback with push_notification
      * @param notification to process
      */
-    void pushNotificationReceived(const Json::Value& notification) {
+    void pushNotificationReceived(const std::map<std::string, std::string>& notification) {
         dht_->pushNotificationReceived(notification);
     }
     /**
diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp
index f8528782..3b3e62ec 100644
--- a/src/dht_proxy_client.cpp
+++ b/src/dht_proxy_client.cpp
@@ -42,7 +42,6 @@ DhtProxyClient::DhtProxyClient(const std::string& serverHost, const std::string&
 void
 DhtProxyClient::confirmProxy()
 {
-    std::cout << "confirmProxy" << std::endl;
     if (serverHost_.empty()) return;
     getConnectivityStatus();
 }
@@ -643,21 +642,25 @@ DhtProxyClient::restartListeners()
 
 #if OPENDHT_PUSH_NOTIFICATIONS
 void
-DhtProxyClient::pushNotificationReceived(const Json::Value& notification)
+DhtProxyClient::pushNotificationReceived(const std::map<std::string, std::string>& notification)
 {
-    if (!notification.isMember("token")) return;
-    auto token = notification["token"].asLargestUInt();
-    // Find listener
-    for (const auto& listener: listeners_)
-        if (*(listener.pushNotifToken) == token) {
-            if (notification.isMember("timeout")) {
-                // A timeout has occured, we need to relaunch the listener
-                resubscribe(token);
-            } else {
+    try {
+        auto token = std::stoul(notification.at("token"));
+        for (const auto& listener: listeners_) {
+            if (*(listener.pushNotifToken) != token)
+                continue;
+            if (notification.find("timeout") == notification.cend()) {
                 // Wake up daemon and get values
                 get(InfoHash(listener.key), listener.cb, {}, listener.filterChain);
+            } else {
+                // A timeout has occured, we need to relaunch the listener
+                resubscribe(token);
             }
+
         }
+    } catch (...) {
+
+    }
 }
 
 void
diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp
index c8a990f1..68b48965 100644
--- a/src/dhtrunner.cpp
+++ b/src/dhtrunner.cpp
@@ -928,30 +928,19 @@ DhtRunner::forwardAllMessages(bool forward)
 #endif // OPENDHT_PROXY_SERVER
 
 #if OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
-void
-DhtRunner::pushNotificationReceived(const std::string& notification) const
-{
-    try {
-        std::string err;
-        Json::Value root;
-        Json::CharReaderBuilder rbuilder;
-        auto* char_data = reinterpret_cast<const char*>(&notification[0]);
-        auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader());
-        if (reader->parse(char_data, char_data + notification.size(), &root, &err))
-            pushNotificationReceived(root);
-    } catch (...) { }
-}
 
 void
-DhtRunner::pushNotificationReceived(const Json::Value& notification) const
+DhtRunner::pushNotificationReceived(const std::map<std::string, std::string>& data) const
 {
-    dht_via_proxy_->pushNotificationReceived(notification);
+    if (dht_via_proxy_)
+        dht_via_proxy_->pushNotificationReceived(data);
 }
 
 void
 DhtRunner::resubscribe(const unsigned token)
 {
-    dht_via_proxy_->resubscribe(token);
+    if (dht_via_proxy_)
+        dht_via_proxy_->resubscribe(token);
 }
 
 #endif // OPENDHT_PUSH_NOTIFICATIONS && OPENDHT_PROXY_CLIENT
diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp
index 53d1cd29..6b9a8db3 100644
--- a/tools/dhtnode.cpp
+++ b/tools/dhtnode.cpp
@@ -232,7 +232,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& dht, dht_params& params
 #if OPENDHT_PUSH_NOTIFICATIONS
         else if (op == "rp") {
             iss >> value;
-            dht->pushNotificationReceived(value);
+            dht->pushNotificationReceived({{"token", value}});
             continue;
         } else if (op == "re") {
             iss >> value;
-- 
GitLab