From f2cf653eb9967dc9ee26cbe7a43d1815bd526563 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Mon, 21 May 2018 08:26:02 +0200 Subject: Add methods to allow pinging --- src/Database.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/Database.cpp') 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 Database::receiveCustomMessage(const dht::InfoHash &receiveMessageKey, ReceiveCustomMessageCallbackFunc callbackFunc) { dht::InfoHash responseKey = receiveMessageKey; ++responseKey[0]; - node.listen(receiveMessageKey, [callbackFunc, this, responseKey](const shared_ptr value) + return node.listen(receiveMessageKey, [callbackFunc, this, responseKey](const shared_ptr 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 &&data, SendCustomMessageCallbackFunc callbackFunc) + void Database::sendCustomMessage(const dht::InfoHash &requestKey, vector &&data) + { + shared_ptr value = make_shared(move(data)); + nodePutWithRetry(&node, requestKey, value); + } + + std::future Database::sendCustomMessage(const dht::InfoHash &requestKey, vector &&data, SendCustomMessageCallbackFunc callbackFunc) { dht::InfoHash responseKey = requestKey; ++responseKey[0]; - node.listen(responseKey, [callbackFunc](const shared_ptr value) + auto listener = node.listen(responseKey, [callbackFunc](const shared_ptr value) { return callbackFunc(true, value->data.data(), value->data.size()); }); shared_ptr value = make_shared(move(data)); nodePutWithRetry(&node, requestKey, value); + return listener; + } + + void Database::cancelNodeListener(const dht::InfoHash &infoHash, std::future &nodeListener) + { + node.cancelListen(infoHash, nodeListener.get()); } int Database::clearCache() -- cgit v1.2.3