xref: /openbmc/telemetry/src/utils/messanger.hpp (revision e6d48874)
1*e6d48874SKrzysztof Grobelny #pragma once
2*e6d48874SKrzysztof Grobelny 
3*e6d48874SKrzysztof Grobelny #include "messanger_service.hpp"
4*e6d48874SKrzysztof Grobelny 
5*e6d48874SKrzysztof Grobelny #include <boost/asio.hpp>
6*e6d48874SKrzysztof Grobelny 
7*e6d48874SKrzysztof Grobelny namespace utils
8*e6d48874SKrzysztof Grobelny {
9*e6d48874SKrzysztof Grobelny 
10*e6d48874SKrzysztof Grobelny template <class Service>
11*e6d48874SKrzysztof Grobelny class MessangerT
12*e6d48874SKrzysztof Grobelny {
13*e6d48874SKrzysztof Grobelny   public:
MessangerT(boost::asio::execution_context & execution_context)14*e6d48874SKrzysztof Grobelny     explicit MessangerT(boost::asio::execution_context& execution_context) :
15*e6d48874SKrzysztof Grobelny         service_(boost::asio::use_service<Service>(execution_context)),
16*e6d48874SKrzysztof Grobelny         context_(service_.create())
17*e6d48874SKrzysztof Grobelny     {}
18*e6d48874SKrzysztof Grobelny 
~MessangerT()19*e6d48874SKrzysztof Grobelny     ~MessangerT()
20*e6d48874SKrzysztof Grobelny     {
21*e6d48874SKrzysztof Grobelny         service_.destroy(context_);
22*e6d48874SKrzysztof Grobelny     }
23*e6d48874SKrzysztof Grobelny 
24*e6d48874SKrzysztof Grobelny     template <class EventType>
on_receive(std::function<void (const EventType &)> handler)25*e6d48874SKrzysztof Grobelny     void on_receive(std::function<void(const EventType&)> handler)
26*e6d48874SKrzysztof Grobelny     {
27*e6d48874SKrzysztof Grobelny         context_.handlers.emplace_back(std::move(handler));
28*e6d48874SKrzysztof Grobelny     }
29*e6d48874SKrzysztof Grobelny 
30*e6d48874SKrzysztof Grobelny     template <class EventType>
send(const EventType & event)31*e6d48874SKrzysztof Grobelny     void send(const EventType& event)
32*e6d48874SKrzysztof Grobelny     {
33*e6d48874SKrzysztof Grobelny         service_.send(event);
34*e6d48874SKrzysztof Grobelny     }
35*e6d48874SKrzysztof Grobelny 
36*e6d48874SKrzysztof Grobelny   private:
37*e6d48874SKrzysztof Grobelny     Service& service_;
38*e6d48874SKrzysztof Grobelny     typename Service::Context& context_;
39*e6d48874SKrzysztof Grobelny };
40*e6d48874SKrzysztof Grobelny 
41*e6d48874SKrzysztof Grobelny using Messanger = MessangerT<MessangerService>;
42*e6d48874SKrzysztof Grobelny 
43*e6d48874SKrzysztof Grobelny } // namespace utils
44