diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 3cbd885a772368cc511cce603c8cad73d8bab20e..0b1228c4fcf1e57cef9064409e27295dc4b8b9ea 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 c56476fe17963b6c17f85b125248eb6ffadb57ad..0d3a9b2ba650f4888e60dd8dbfe4e11db6baad01 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 afa9751357e753c42d28697ae8a11e5f93791b02..bf8aa4ff58cd605a66b988ab8b9581704d810c95 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 67c0cbf06f3312e42b150ef309bcb2ac18f03837..5f955cd86927dac381982038ed123a548e818e4c 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 0f0b96c2f6677159cf1be9fd98d96173d77ccaef..732e2b97ca9e920fc2f43b4d87405b6349214a04 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 f8528782a9db79158becfbf66ef60b81894f1865..3b3e62ec85562c5931f515ccb02c052445a54dcc 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 c8a990f122b72b6cf48c8bcba78d75d8292a8879..68b48965eeccd7fb2f2058aed045bb9794cf2429 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*>(¬ification[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 53d1cd2995eecc1c2f386021be582b8bea8cc778..6b9a8db356750d0a3cdd942f855e76d479468c0f 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;