2 #include <g3log/g3log.hpp> 11 CHECK(endpoint !=
nullptr) <<
"No endpoint specified";
14 data::Message request;
15 request.mutable_ev_notification()->CopyFrom(event);
18 LOG(DBUG) <<
"Event '" <<
event.name() <<
"' will be triggered globally";
21 endpoint->
Send(&request,
nullptr);
27 CHECK(endpoint !=
nullptr) <<
"No endpoint specified";
28 CHECK(node !=
nullptr) <<
"No target node specified";
31 data::Message request;
32 request.mutable_header()->set_reliability(data::Reliability::ACK_REQUESTED);
33 request.mutable_ev_notification()->CopyFrom(event);
36 LOG(DBUG) <<
"Event '" <<
event.name() <<
"' will be triggered on node [" << node->
GetUUID() <<
"]";
39 endpoint->
Tag(&request);
40 ErrorAwaiter awaiter(endpoint, request.header().identifier());
41 endpoint->
Send(&request, node);
48 CHECK(sender !=
nullptr) <<
"Sender address missing";
49 CHECK(message !=
nullptr) <<
"Message is missing";
52 if (message->content_case() == data::Message::ContentCase::kEvNotification)
55 LOG(DBUG) <<
"Event '" << message->ev_notification().name() <<
"' was received";
58 std::lock_guard<std::mutex> guard(
_mutex);
59 auto handler =
_handlers.find(message->ev_notification().name());
62 handler->second->EventWasTriggered(sender, message->ev_notification());
67 LOG(DBUG) <<
"Event '" << message->ev_notification().name() <<
"' was not handled by any of the registered event handlers";
80 CHECK(handler !=
nullptr) <<
"Nullptr was supplied in shared pointer";
83 std::lock_guard<std::mutex> guard(
_mutex);
90 throw Exception(
"Event handler for this event is already registered");
97 std::lock_guard<std::mutex> guard(
_mutex);
103 std::lock_guard<std::mutex> guard(
_mutex);
106 auto& fields = *descriptor.mutable_event_schema()->mutable_fields();
109 *fields[handler.first].mutable_schema() = handler.second->DescribeEvent(handler.first);
static void Trigger(Endpoint *endpoint, const data::event::Notification &event)
Trigger an event globally.
std::mutex _mutex
Mutex to protect handler list.
virtual void Tag(data::Message *message)=0
Set the message identifier for a message.
std::map< std::string, Handler * > _handlers
Map of handlers.
Exception class thrown by all library classes.
void RegisterHandler(const std::string &name, Handler *handler)
Subscribe to events.
virtual bool ReceiveMessage(const Node *sender, const data::Message *message) override
Delivery point of all messages.
virtual void Send(data::Message *message, const Node *node)=0
Send a message to a specific member of the swarm. Call with node set to nullptr to send a message to ...
An Awaiter that checks whether the operation was a success.
Abstract base class for Endpoint implementations.
virtual void DescribeService(data::discovery::Response &descriptor) override
Add descriptors for the service to the discovery descriptor.
virtual const std::string & GetUUID() const =0
Returns the unique identifier of the node.
Abstract base class for Event handlers.
Represents a Node the Endpoint knows about and can send messages to.
void UnregisterHandler(const std::string &name)
Unsubscribe from events.