From ba8631c5f75df45fb07726e0aab3bb0834d6a5b6 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Wed, 16 May 2018 07:25:22 +0200 Subject: Add functions to send/receive custom messages --- tests/main.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests/main.cpp') diff --git a/tests/main.cpp b/tests/main.cpp index dd56164..e819684 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -266,6 +266,39 @@ int main() assertEquals(1, createNodeCounter); assertEquals(2, addDataCounter); assertEquals(1, addUserCounter); + + dht::InfoHash customMessageKey = Database::getInfoHash("asdf", 4); + sibs::SafeSerializer messageToSendSerializer; + messageToSendSerializer.add((u32)10); + + u32 receivedNumber = 0; + database.receiveCustomMessage(customMessageKey, [&receivedNumber](const void *data, usize size) + { + sibs::SafeDeserializer deserializer((const u8*)data, size); + receivedNumber = deserializer.extract(); + sibs::SafeSerializer serializer; + serializer.add((u32)20); + return serializer; + }); + + u32 sendCustomMessageResponseNumber = 0; + database.sendCustomMessage(customMessageKey, move(messageToSendSerializer.getBuffer()), [&sendCustomMessageResponseNumber](bool gotResponse, const void *data, usize size) + { + if(!gotResponse) + { + Log::error("Didn't get reponse!"); + return true; + } + + sibs::SafeDeserializer deserializer((const u8*)data, size); + sendCustomMessageResponseNumber = deserializer.extract(); + return false; + }); + + + this_thread::sleep_for(chrono::seconds(3)); + assertEquals((u32)10, receivedNumber); + assertEquals((u32)20, sendCustomMessageResponseNumber); } return 0; -- cgit v1.2.3