summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2024-08-18 22:15:56 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2024-08-19 21:53:11 +0200
commita689af61eb91dcbc135890276a3c6281166d30f9 (patch)
tree7fd9583f8cc9e600a0d52bed83ff0d6ad56e9154 /src/test
parent0eb84d2c6d4657c482b3deff8d4114d676197099 (diff)
Test message service for sending arbitrary dataHEADmaster
Diffstat (limited to 'src/test')
-rw-r--r--src/test/service.cpp37
-rw-r--r--src/test/service.h35
2 files changed, 72 insertions, 0 deletions
diff --git a/src/test/service.cpp b/src/test/service.cpp
new file mode 100644
index 0000000..32fca52
--- /dev/null
+++ b/src/test/service.cpp
@@ -0,0 +1,37 @@
+#include "service.h"
+
+#include <erebos/network.h>
+
+using namespace erebos;
+
+static const UUID myUUID("cb46b92c-9203-4694-8370-8742d8ac9dc8");
+
+TestService::TestService( Config && c, const Server & ):
+ config( move(c) )
+{
+}
+
+TestService::~TestService() = default;
+
+UUID TestService::uuid() const
+{
+ return myUUID;
+}
+
+void TestService::handle( Context & ctx )
+{
+ auto msg = Stored< Object >::load( ctx.ref() );
+ for (const auto & w : config.watchers)
+ w( msg );
+}
+
+void TestService::send( const Peer & peer, const Ref & msg )
+{
+ peer.send( myUUID, msg );
+}
+
+TestService::Config & TestService::Config::onMessage( MessageWatcher w )
+{
+ watchers.push_back(w);
+ return *this;
+}
diff --git a/src/test/service.h b/src/test/service.h
new file mode 100644
index 0000000..c693ce4
--- /dev/null
+++ b/src/test/service.h
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <erebos/service.h>
+
+namespace erebos
+{
+
+class TestService : public Service
+{
+public:
+ using MessageWatcher = std::function<void( const Stored< Object > & )>;
+
+ class Config
+ {
+ public:
+ Config & onMessage( MessageWatcher );
+
+ private:
+ friend class TestService;
+ vector< MessageWatcher > watchers;
+ };
+
+ TestService( Config &&, const Server & );
+ virtual ~TestService();
+
+ UUID uuid() const override;
+ void handle( Context & ) override;
+
+ static void send( const Peer &, const Ref & );
+
+private:
+ const Config config;
+};
+
+}