From 9421984a8949e2151b581de44aa59cfa34b1e87c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 20 Apr 2018 16:37:09 -0400
Subject: [PATCH] proxy: cancelPushListeners using clientId instead of token

All listeners uses a token equals to 0, we need to identify listeners
with client_id instead of a token.
---
 include/opendht/dht_proxy_server.h |  8 +++++++-
 src/dht_proxy_server.cpp           | 14 +++++++-------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/include/opendht/dht_proxy_server.h b/include/opendht/dht_proxy_server.h
index d0af6a87..b5216ad4 100644
--- a/include/opendht/dht_proxy_server.h
+++ b/include/opendht/dht_proxy_server.h
@@ -213,7 +213,13 @@ private:
      */
     void sendPushNotification(const std::string& key, const Json::Value& json, bool isAndroid) const;
 
-    void cancelPushListen(const std::string& pushToken, const InfoHash& key, proxy::ListenToken token);
+    /**
+     * Remove a push listener between a client and a hash
+     * @param pushToken
+     * @param key
+     * @param clientId
+     */
+    void cancelPushListen(const std::string& pushToken, const InfoHash& key, const std::string& clientId);
 
 
 #endif //OPENDHT_PUSH_NOTIFICATIONS
diff --git a/src/dht_proxy_server.cpp b/src/dht_proxy_server.cpp
index f9ebf042..efd4e2c8 100644
--- a/src/dht_proxy_server.cpp
+++ b/src/dht_proxy_server.cpp
@@ -411,8 +411,8 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
                         }
                     );
                     listener.expireJob = scheduler_.add(timeout,
-                        [this, token, infoHash, pushToken] {
-                            cancelPushListen(pushToken, infoHash, *token);
+                        [this, clientId, infoHash, pushToken] {
+                            cancelPushListen(pushToken, infoHash, clientId);
                         }
                     );
                     listener.expireNotifyJob = scheduler_.add(timeout - proxy::OP_MARGIN,
@@ -461,9 +461,9 @@ DhtProxyServer::unsubscribe(const std::shared_ptr<restbed::Session>& session)
                 }
                 auto pushToken = root["key"].asString();
                 if (pushToken.empty()) return;
-                auto token = unpackId(root, "token");
+                auto clientId = root["client_id"].asString();
 
-                cancelPushListen(pushToken, infoHash, token);
+                cancelPushListen(pushToken, infoHash, clientId);
                 s->close(restbed::OK);
             } catch (...) {
                 s->close(restbed::INTERNAL_SERVER_ERROR, "{\"err\":\"Internal server error\"}");
@@ -473,9 +473,9 @@ DhtProxyServer::unsubscribe(const std::shared_ptr<restbed::Session>& session)
 }
 
 void
-DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHash& key, proxy::ListenToken token)
+DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHash& key, const std::string& clientId)
 {
-    std::cout << "cancelPushListen: " << key << " token:" << token << std::endl;
+    std::cout << "cancelPushListen: " << key << " clientId:" << clientId << std::endl;
     std::lock_guard<std::mutex> lock(lockListener_);
     auto pushListener = pushListeners_.find(pushToken);
     if (pushListener == pushListeners_.end())
@@ -484,7 +484,7 @@ DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHa
     if (listeners == pushListener->second.listeners.end())
         return;
     for (auto listener = listeners->second.begin(); listener != listeners->second.end();) {
-        if (*listener->token == token) {
+        if (listener->clientId == clientId) {
             if (dht_)
                 dht_->cancelListen(key, std::move(listener->internalToken));
             listener = listeners->second.erase(listener);
-- 
GitLab