From d6a99d0edd96dca2779df4a69fa384334cb24747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Thu, 3 Nov 2022 15:15:11 -0400 Subject: [PATCH] proxyclient: add possibility to set the platform This allow android to change the platform to "unifiedpush". --- c/opendht.cpp | 1 + c/opendht_c.h | 1 + include/opendht/dht_interface.h | 1 + include/opendht/dht_proxy_client.h | 22 ++++++++++++++++++++++ include/opendht/dhtrunner.h | 6 ++++++ rust/src/dhtrunner.rs | 5 +++++ rust/src/ffi.rs | 1 + src/dht_proxy_client.cpp | 7 +------ src/dhtrunner.cpp | 14 ++++++++++++++ 9 files changed, 52 insertions(+), 6 deletions(-) diff --git a/c/opendht.cpp b/c/opendht.cpp index dd61b3cf..7ba15ea9 100644 --- a/c/opendht.cpp +++ b/c/opendht.cpp @@ -343,6 +343,7 @@ int dht_runner_run_config(dht_runner* r, in_port_t port, const dht_runner_config config.push_node_id = conf->push_node_id ? std::string(conf->push_node_id) : std::string{}; config.push_token = conf->push_token ? std::string(conf->push_token) : std::string{}; config.push_topic = conf->push_topic ? std::string(conf->push_topic) : std::string{}; + config.push_platform = conf->push_platform ? std::string(conf->push_platform) : std::string{}; config.peer_discovery = conf->peer_discovery; config.peer_publish = conf->peer_publish; diff --git a/c/opendht_c.h b/c/opendht_c.h index 5cdad798..5e9a9aed 100644 --- a/c/opendht_c.h +++ b/c/opendht_c.h @@ -147,6 +147,7 @@ struct OPENDHT_PUBLIC dht_runner_config { const char* push_node_id; const char* push_token; const char* push_topic; + const char* push_platform; bool peer_discovery; bool peer_publish; dht_certificate* server_ca; diff --git a/include/opendht/dht_interface.h b/include/opendht/dht_interface.h index 41ab734d..e621b161 100644 --- a/include/opendht/dht_interface.h +++ b/include/opendht/dht_interface.h @@ -266,6 +266,7 @@ public: virtual void setPushNotificationToken(const std::string&) {}; virtual void setPushNotificationTopic(const std::string&) {}; + virtual void setPushNotificationPlatform(const std::string&) {}; /** * Call linked callback with a push notification diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index 08bfb05f..02f8b2c5 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -75,6 +75,14 @@ public: #endif } + virtual void setPushNotificationPlatform(const std::string& platform) override { +#ifdef OPENDHT_PUSH_NOTIFICATIONS + platform_ = platform; +#else + (void) platform; +#endif + } + virtual ~DhtProxyClient(); /** @@ -415,6 +423,20 @@ private: */ std::string notificationTopic_ {}; + /** + * Platform for push notifications (supported android, ios, unifiedpush) + */ + std::string platform_ +#ifdef __ANDROID__ + {"android"}; +#else +#ifdef __APPLE__ + {"ios"}; +#else + {}; +#endif +#endif + const std::function<void()> loopSignal_; #ifdef OPENDHT_PUSH_NOTIFICATIONS diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 2ea3c4f8..450b3158 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -62,6 +62,7 @@ public: std::string push_node_id {}; std::string push_token {}; std::string push_topic {}; + std::string push_platform {}; bool peer_discovery {false}; bool peer_publish {false}; std::shared_ptr<dht::crypto::Certificate> server_ca; @@ -453,6 +454,11 @@ public: */ void setPushNotificationTopic(const std::string& topic); + /** + * Sets the push notification platform + */ + void setPushNotificationPlatform(const std::string& platform); + /** * Insert a push notification to process for OpenDHT */ diff --git a/rust/src/dhtrunner.rs b/rust/src/dhtrunner.rs index 3470e3db..ecac8f06 100644 --- a/rust/src/dhtrunner.rs +++ b/rust/src/dhtrunner.rs @@ -48,6 +48,7 @@ impl DhtRunnerConfig { push_node_id: ptr::null(), push_token: ptr::null(), push_topic: ptr::null(), + push_platform: ptr::null(), peer_discovery: false, peer_publish: false, server_ca: ptr::null_mut(), @@ -78,6 +79,10 @@ impl DhtRunnerConfig { self.push_topic = CString::new(push_topic).unwrap().as_ptr(); } + pub fn set_push_platform(&mut self, push_platform: &str) { + self.push_platform = CString::new(push_platform).unwrap().as_ptr(); + } + pub fn set_identity(&mut self, certificate: Box<DhtCertificate>, privatekey: Box<PrivateKey>) { self.dht_config.id.privatekey = Box::into_raw(privatekey); self.dht_config.id.certificate = Box::into_raw(certificate); diff --git a/rust/src/ffi.rs b/rust/src/ffi.rs index 97cac9ac..8e7eefb5 100644 --- a/rust/src/ffi.rs +++ b/rust/src/ffi.rs @@ -119,6 +119,7 @@ pub struct DhtRunnerConfig pub push_node_id: *const c_char, pub push_token: *const c_char, pub push_topic: *const c_char, + pub push_platform: *const c_char, pub peer_discovery: bool, pub peer_publish: bool, pub server_ca: *mut DhtCertificate, diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index b91f2824..d53b7693 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -1325,13 +1325,8 @@ DhtProxyClient::getPushRequest(Json::Value& body) const body["key"] = deviceKey_; body["client_id"] = pushClientId_; body["session_id"] = pushSessionId_; -#ifdef __ANDROID__ - body["platform"] = "android"; -#endif -#ifdef __APPLE__ body["topic"] = notificationTopic_; - body["platform"] = "ios"; -#endif + body["platform"] = platform_; } std::string diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index a73897ff..f687fdd7 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -1115,6 +1115,8 @@ DhtRunner::enableProxy(bool proxify) dht_via_proxy->setPushNotificationToken(config_.push_token); if (not config_.push_topic.empty()) dht_via_proxy->setPushNotificationTopic(config_.push_topic); + if (not config_.push_platform.empty()) + dht_via_proxy->setPushNotificationPlatform(config_.push_platform); dht_ = std::make_unique<SecureDht>(std::move(dht_via_proxy), config_.dht_config, identityAnnouncedCb_, logger_); // and use it use_proxy = proxify; @@ -1162,6 +1164,18 @@ DhtRunner::setPushNotificationTopic(const std::string& topic) { #endif } +void +DhtRunner::setPushNotificationPlatform(const std::string& platform) { + std::lock_guard<std::mutex> lck(dht_mtx); +#if defined(OPENDHT_PROXY_CLIENT) && defined(OPENDHT_PUSH_NOTIFICATIONS) + config_.push_platform = platform; + if (dht_) + dht_->setPushNotificationPlatform(platform); +#else + (void) platform; +#endif +} + void DhtRunner::pushNotificationReceived(const std::map<std::string, std::string>& data) { -- GitLab