From ac626d47ebd49188458d70f98c6d871b1b7e3f50 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Mon, 21 May 2018 08:26:02 +0200
Subject: Add methods to allow pinging

---
 include/odhtdb/Database.hpp |  8 ++++++--
 include/odhtdb/DhtKey.hpp   |  1 +
 src/Database.cpp            | 20 ++++++++++++++++----
 src/DhtKey.cpp              |  6 ++++++
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp
index 6874fe0..a5fcb60 100644
--- a/include/odhtdb/Database.hpp
+++ b/include/odhtdb/Database.hpp
@@ -196,10 +196,14 @@ namespace odhtdb
         
         std::vector<OwnedByteArray> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
         
-        void receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc);
+        std::future<size_t> receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc);
+        
+        void sendCustomMessage(const dht::InfoHash &key, std::vector<u8> &&data);
         
         // Return true in @callbackFunc if you want to continue listening for responses, otherwise return false
-        void sendCustomMessage(const dht::InfoHash &key, std::vector<u8> &&data, SendCustomMessageCallbackFunc callbackFunc);
+        std::future<size_t> sendCustomMessage(const dht::InfoHash &key, std::vector<u8> &&data, SendCustomMessageCallbackFunc callbackFunc);
+        
+        void cancelNodeListener(const dht::InfoHash &infoHash, std::future<size_t> &nodeListener);
         
         int clearCache();
         
diff --git a/include/odhtdb/DhtKey.hpp b/include/odhtdb/DhtKey.hpp
index c3398bf..959bb65 100644
--- a/include/odhtdb/DhtKey.hpp
+++ b/include/odhtdb/DhtKey.hpp
@@ -13,6 +13,7 @@ namespace odhtdb
         
         dht::InfoHash getNewDataListenerKey();
         dht::InfoHash getRequestOldDataKey();
+        dht::InfoHash getPingKey();
     private:
         dht::InfoHash infoHash;
         unsigned char firstByteOriginalValue;
diff --git a/src/Database.cpp b/src/Database.cpp
index 155e8f5..8d32240 100644
--- a/src/Database.cpp
+++ b/src/Database.cpp
@@ -715,12 +715,12 @@ namespace odhtdb
         return databaseStorage.getUserGroups(nodeHash, userPublicKey);
     }
     
-    void Database::receiveCustomMessage(const dht::InfoHash &receiveMessageKey, ReceiveCustomMessageCallbackFunc callbackFunc)
+    std::future<size_t> Database::receiveCustomMessage(const dht::InfoHash &receiveMessageKey, ReceiveCustomMessageCallbackFunc callbackFunc)
     {
         dht::InfoHash responseKey = receiveMessageKey;
         ++responseKey[0];
         
-        node.listen(receiveMessageKey, [callbackFunc, this, responseKey](const shared_ptr<Value> value)
+        return node.listen(receiveMessageKey, [callbackFunc, this, responseKey](const shared_ptr<Value> value)
         {
             sibs::SafeSerializer serializer = callbackFunc(value->data.data(), value->data.size());
             if(!serializer.getBuffer().empty())
@@ -732,18 +732,30 @@ namespace odhtdb
         });
     }
     
-    void Database::sendCustomMessage(const dht::InfoHash &requestKey, vector<u8> &&data, SendCustomMessageCallbackFunc callbackFunc)
+    void Database::sendCustomMessage(const dht::InfoHash &requestKey, vector<u8> &&data)
+    {
+        shared_ptr<Value> value = make_shared<Value>(move(data));
+        nodePutWithRetry(&node, requestKey, value);
+    }
+    
+    std::future<size_t> Database::sendCustomMessage(const dht::InfoHash &requestKey, vector<u8> &&data, SendCustomMessageCallbackFunc callbackFunc)
     {
         dht::InfoHash responseKey = requestKey;
         ++responseKey[0];
         
-        node.listen(responseKey, [callbackFunc](const shared_ptr<Value> value)
+        auto listener = node.listen(responseKey, [callbackFunc](const shared_ptr<Value> value)
         {
             return callbackFunc(true, value->data.data(), value->data.size());
         });
         
         shared_ptr<Value> value = make_shared<Value>(move(data));
         nodePutWithRetry(&node, requestKey, value);
+        return listener;
+    }
+    
+    void Database::cancelNodeListener(const dht::InfoHash &infoHash, std::future<size_t> &nodeListener)
+    {
+        node.cancelListen(infoHash, nodeListener.get());
     }
     
     int Database::clearCache()
diff --git a/src/DhtKey.cpp b/src/DhtKey.cpp
index 422b715..1508657 100644
--- a/src/DhtKey.cpp
+++ b/src/DhtKey.cpp
@@ -24,4 +24,10 @@ namespace odhtdb
         infoHash[0] = firstByteOriginalValue + 1;
         return infoHash;
     }
+    
+    dht::InfoHash DhtKey::getPingKey()
+    {
+        infoHash[0] = firstByteOriginalValue + 10;
+        return infoHash;
+    }
 }
-- 
cgit v1.2.3-70-g09d2