Removed dependency to TriggerManager
introduces Messanger class which allows to send messages directly to
subscribed targets, which allows to break dependencies between classes.
Testes:
- All unit tests are passing
- Links are correctly updated
- Report is correctly updated by Trigger Action
Signed-off-by: Krzysztof Grobelny <krzysztof.grobelny@intel.com>
Change-Id: I32d3aaba22f9ec07e611f53fe553bd27e1c04c6d
diff --git a/src/utils/messanger.hpp b/src/utils/messanger.hpp
new file mode 100644
index 0000000..a2a2c22
--- /dev/null
+++ b/src/utils/messanger.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+#include "messanger_service.hpp"
+
+#include <boost/asio.hpp>
+
+namespace utils
+{
+
+template <class Service>
+class MessangerT
+{
+ public:
+ explicit MessangerT(boost::asio::execution_context& execution_context) :
+ service_(boost::asio::use_service<Service>(execution_context)),
+ context_(service_.create())
+ {}
+
+ ~MessangerT()
+ {
+ service_.destroy(context_);
+ }
+
+ template <class EventType>
+ void on_receive(std::function<void(const EventType&)> handler)
+ {
+ context_.handlers.emplace_back(std::move(handler));
+ }
+
+ template <class EventType>
+ void send(const EventType& event)
+ {
+ service_.send(event);
+ }
+
+ private:
+ Service& service_;
+ typename Service::Context& context_;
+};
+
+using Messanger = MessangerT<MessangerService>;
+
+} // namespace utils
diff --git a/src/utils/messanger_service.cpp b/src/utils/messanger_service.cpp
new file mode 100644
index 0000000..e04de34
--- /dev/null
+++ b/src/utils/messanger_service.cpp
@@ -0,0 +1,28 @@
+#include "messanger_service.hpp"
+
+namespace utils
+{
+
+MessangerService::MessangerService(
+ boost::asio::execution_context& execution_context) :
+ boost::asio::execution_context::service(execution_context)
+{}
+
+MessangerService::Context& MessangerService::create()
+{
+ contexts_.emplace_back(std::make_unique<Context>());
+ return *contexts_.back();
+}
+
+void MessangerService::destroy(MessangerService::Context& context)
+{
+ contexts_.erase(std::remove_if(contexts_.begin(), contexts_.end(),
+ [&context](const auto& item) {
+ return item.get() == &context;
+ }),
+ contexts_.end());
+}
+
+boost::asio::execution_context::id MessangerService::id = {};
+
+} // namespace utils
diff --git a/src/utils/messanger_service.hpp b/src/utils/messanger_service.hpp
new file mode 100644
index 0000000..86dd2ff
--- /dev/null
+++ b/src/utils/messanger_service.hpp
@@ -0,0 +1,53 @@
+#pragma once
+
+#include <boost/asio.hpp>
+
+#include <any>
+
+namespace utils
+{
+
+class MessangerService : public boost::asio::execution_context::service
+{
+ public:
+ using key_type = MessangerService;
+
+ struct Context
+ {
+ std::vector<std::any> handlers;
+ };
+
+ MessangerService(boost::asio::execution_context& execution_context);
+ ~MessangerService() = default;
+
+ void shutdown()
+ {}
+
+ Context& create();
+ void destroy(Context& context);
+
+ template <class T>
+ void send(const T& event)
+ {
+ using HandlerType = std::function<void(const T&)>;
+
+ for (const auto& context : contexts_)
+ {
+ for (const auto& any : context->handlers)
+ {
+ if (const HandlerType* handler =
+ std::any_cast<HandlerType>(&any))
+ {
+ (*handler)(event);
+ }
+ }
+ }
+ }
+
+ static boost::asio::execution_context::id id;
+
+ private:
+ std::vector<std::unique_ptr<Context>> contexts_;
+};
+
+} // namespace utils