aboutsummaryrefslogtreecommitdiff
path: root/src/Database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Database.cpp')
-rw-r--r--src/Database.cpp50
1 files changed, 47 insertions, 3 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);
+ }
}