aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-16 07:25:22 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commit92d6393a34dac4b3d623a5169e2b50a9518b4976 (patch)
treea318cc84e3837f61c03cd4a0533c3380bb5fdbde /src
parentc0574f16ca7960a3301230ba36ba96148818e229 (diff)
Add functions to send/receive custom messages
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp50
-rw-r--r--src/DhtKey.cpp9
2 files changed, 54 insertions, 5 deletions
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> &value)
+ auto newDataListenerFuture = node.listen(dhtKey.getNewDataListenerKey(), [this, nodeToSeed](const shared_ptr<Value> 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> &value)
+ auto responseKeyFuture = node.listen(*responseKeyShared, [this, nodeToSeed](const shared_ptr<Value> 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> &value)
+ auto requestOldDataListenerFuture = node.listen(dhtKey.getRequestOldDataKey(), [this, nodeToSeed, responseKeyShared](const shared_ptr<Value> 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> 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<u8> &&data, SendCustomMessageCallbackFunc callbackFunc)
+ {
+ dht::InfoHash responseKey = requestKey;
+ ++responseKey[0];
+
+ node.listen(responseKey, [callbackFunc](const shared_ptr<Value> 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;