From a689af61eb91dcbc135890276a3c6281166d30f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 18 Aug 2024 22:15:56 +0200 Subject: Test message service for sending arbitrary data --- src/main.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index c92d630..68ebbd5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include #include "storage.h" +#include "test/service.h" #include #include @@ -316,6 +317,23 @@ void startServer(const vector &) config.service(); + config.service< TestService >() + .onMessage([]( const Stored< Object > & msg ) { + auto bytes = msg->encode(); + auto space = std::find( bytes.begin(), bytes.end(), ' ' ); + auto type = string( bytes.begin(), space ); + auto size = std::stoi( string( space + 1, std::find( space + 1, bytes.end(), '\n' ) ) ); + + ostringstream ss; + ss << "test-message-received" + << " " << type + << " " << size + << " " << string( msg.ref().digest() ) + ; + printLine(ss.str()); + }) + ; + server.emplace(*testHead, move(config)); server->peerList().onUpdate([](size_t idx, const Peer * peer) { @@ -367,6 +385,16 @@ void peerAdd(const vector & args) throw invalid_argument("usage: peer-add []"); } +void testMessageSend( const vector< string > & args) +{ + if (auto ref = st.ref(Digest(args.at(1)))) { + TestService::send(getPeer( args.at(0) ).peer, *ref ); + printLine("test-message-send done"); + } else { + printLine("test-message-send fail"); + } +} + void sharedStateGet(const vector &) { ostringstream ss; @@ -613,6 +641,7 @@ vector commands = { { "start-server", startServer }, { "stop-server", stopServer }, { "peer-add", peerAdd }, + { "test-message-send", testMessageSend }, { "shared-state-get", sharedStateGet }, { "shared-state-wait", sharedStateWait }, { "watch-local-identity", watchLocalIdentity }, -- cgit v1.2.3