From 92d6393a34dac4b3d623a5169e2b50a9518b4976 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 16 May 2018 07:25:22 +0200 Subject: Add functions to send/receive custom messages --- src/Database.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- src/DhtKey.cpp | 9 +++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Database.cpp b/src/Database.cpp index 241d9a5..5b3f705 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -215,7 +215,7 @@ namespace odhtdb Log::debug("Seeding key: %s", nodeToSeed.getRequestHash()->toString().c_str()); DhtKey dhtKey(*nodeToSeed.getRequestHash()); - auto newDataListenerFuture = node.listen(dhtKey.getNewDataListenerKey(), [this, nodeToSeed](const shared_ptr &value) + auto newDataListenerFuture = node.listen(dhtKey.getNewDataListenerKey(), [this, nodeToSeed](const shared_ptr value) { Log::debug("Seed: New data listener received data..."); const Hash requestHash(value->data.data(), value->data.size()); @@ -233,7 +233,7 @@ namespace odhtdb newSeedInfo.reponseKeyInfoHash = responseKeyShared; // TODO: If this response key is spammed, generate a new one. - auto responseKeyFuture = node.listen(*responseKeyShared, [this, nodeToSeed](const shared_ptr &value) + auto responseKeyFuture = node.listen(*responseKeyShared, [this, nodeToSeed](const shared_ptr value) { const Hash requestHash(value->data.data(), value->data.size()); if(requestHash == *nodeToSeed.getRequestHash()) @@ -245,7 +245,7 @@ namespace odhtdb // TODO:!!! Before listening on this key, we should check how many remote peers are also providing this data. // This is to prevent too many peers from responding to a request to get old data. - auto requestOldDataListenerFuture = node.listen(dhtKey.getRequestOldDataKey(), [this, nodeToSeed, responseKeyShared](const shared_ptr &value) + auto requestOldDataListenerFuture = node.listen(dhtKey.getRequestOldDataKey(), [this, nodeToSeed, responseKeyShared](const shared_ptr value) { Log::debug("Request: Got request to send old data"); try @@ -670,4 +670,48 @@ namespace odhtdb { return databaseStorage.getUserGroups(nodeHash, userPublicKey); } + + void Database::receiveCustomMessage(const dht::InfoHash &receiveMessageKey, ReceiveCustomMessageCallbackFunc callbackFunc) + { + dht::InfoHash responseKey = receiveMessageKey; + ++responseKey[0]; + + node.listen(receiveMessageKey, [callbackFunc, this, responseKey](const shared_ptr value) + { + sibs::SafeSerializer serializer = callbackFunc(value->data.data(), value->data.size()); + if(!serializer.getBuffer().empty()) + { + Value responseValue(move(serializer.getBuffer())); + node.put(responseKey, move(responseValue), [](bool ok) + { + if(!ok) + Log::error("Failed to respond to custom message"); + }); + } + return true; + }); + } + + void Database::sendCustomMessage(const dht::InfoHash &requestKey, vector &&data, SendCustomMessageCallbackFunc callbackFunc) + { + dht::InfoHash responseKey = requestKey; + ++responseKey[0]; + + node.listen(responseKey, [callbackFunc](const shared_ptr value) + { + return callbackFunc(true, value->data.data(), value->data.size()); + }); + + Value value(move(data)); + node.put(requestKey, move(value), [](bool ok) + { + if(!ok) + Log::error("Failed to send custom message"); + }); + } + + dht::InfoHash Database::getInfoHash(const void *data, usize size) + { + return dht::InfoHash((const u8*)data, size); + } } diff --git a/src/DhtKey.cpp b/src/DhtKey.cpp index 7bd6acf..422b715 100644 --- a/src/DhtKey.cpp +++ b/src/DhtKey.cpp @@ -8,13 +8,18 @@ namespace odhtdb firstByteOriginalValue = infoHash[0]; } - const dht::InfoHash& DhtKey::getNewDataListenerKey() + DhtKey::DhtKey(const dht::InfoHash &_infoHash) : infoHash(_infoHash) + { + firstByteOriginalValue = infoHash[0]; + } + + dht::InfoHash DhtKey::getNewDataListenerKey() { infoHash[0] = firstByteOriginalValue; return infoHash; } - const dht::InfoHash& DhtKey::getRequestOldDataKey() + dht::InfoHash DhtKey::getRequestOldDataKey() { infoHash[0] = firstByteOriginalValue + 1; return infoHash; -- cgit v1.2.3