event/Service.cpp
Go to the documentation of this file.
2 #include <g3log/g3log.hpp>
3 
4 using namespace swarmio;
5 using namespace swarmio::services;
6 using namespace swarmio::services::event;
7 
8 void Service::Trigger(Endpoint* endpoint, const data::event::Notification& event)
9 {
10  // Sanity checks
11  CHECK(endpoint != nullptr) << "No endpoint specified";
12 
13  // Build message
14  data::Message request;
15  request.mutable_ev_notification()->CopyFrom(event);
16 
17  // Log outgoing event
18  LOG(DBUG) << "Event '" << event.name() << "' will be triggered globally";
19 
20  // Send message
21  endpoint->Send(&request, nullptr);
22 }
23 
24 ErrorAwaiter Service::Trigger(Endpoint* endpoint, const data::event::Notification& event, const Node* node)
25 {
26  // Sanity checks
27  CHECK(endpoint != nullptr) << "No endpoint specified";
28  CHECK(node != nullptr) << "No target node specified";
29 
30  // Build message
31  data::Message request;
32  request.mutable_header()->set_reliability(data::Reliability::ACK_REQUESTED);
33  request.mutable_ev_notification()->CopyFrom(event);
34 
35  // Log outgoing event
36  LOG(DBUG) << "Event '" << event.name() << "' will be triggered on node [" << node->GetUUID() << "]";
37 
38  // Send and await response
39  endpoint->Tag(&request);
40  ErrorAwaiter awaiter(endpoint, request.header().identifier());
41  endpoint->Send(&request, node);
42  return awaiter;
43 }
44 
45 bool Service::ReceiveMessage(const Node* sender, const data::Message* message)
46 {
47  // Sanity checks
48  CHECK(sender != nullptr) << "Sender address missing";
49  CHECK(message != nullptr) << "Message is missing";
50 
51  // Check type
52  if (message->content_case() == data::Message::ContentCase::kEvNotification)
53  {
54  // Log incoming event
55  LOG(DBUG) << "Event '" << message->ev_notification().name() << "' was received";
56 
57  // Find handler
58  std::lock_guard<std::mutex> guard(_mutex);
59  auto handler = _handlers.find(message->ev_notification().name());
60  if (handler != _handlers.end())
61  {
62  handler->second->EventWasTriggered(sender, message->ev_notification());
63  return true;
64  }
65  else
66  {
67  LOG(DBUG) << "Event '" << message->ev_notification().name() << "' was not handled by any of the registered event handlers";
68  return false;
69  }
70  }
71  else
72  {
73  return false;
74  }
75 }
76 
77 void Service::RegisterHandler(const std::string& name, Handler* handler)
78 {
79  // Sanity checks
80  CHECK(handler != nullptr) << "Nullptr was supplied in shared pointer";
81 
82  // Add to map
83  std::lock_guard<std::mutex> guard(_mutex);
84  if (_handlers.find(name) == _handlers.end())
85  {
86  _handlers[name] = handler;
87  }
88  else
89  {
90  throw Exception("Event handler for this event is already registered");
91  }
92 }
93 
94 void Service::UnregisterHandler(const std::string& name)
95 {
96  // Remove from map
97  std::lock_guard<std::mutex> guard(_mutex);
98  _handlers.erase(name);
99 }
100 
101 void Service::DescribeService(data::discovery::Response& descriptor)
102 {
103  std::lock_guard<std::mutex> guard(_mutex);
104 
105  // Collect information from handlers
106  auto& fields = *descriptor.mutable_event_schema()->mutable_fields();
107  for (auto handler : _handlers)
108  {
109  *fields[handler.first].mutable_schema() = handler.second->DescribeEvent(handler.first);
110  }
111 }
static void Trigger(Endpoint *endpoint, const data::event::Notification &event)
Trigger an event globally.
std::mutex _mutex
Mutex to protect handler list.
Definition: event/Service.h:32
virtual void Tag(data::Message *message)=0
Set the message identifier for a message.
std::map< std::string, Handler * > _handlers
Map of handlers.
Definition: event/Service.h:26
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.
Definition: ErrorAwaiter.h:11
Abstract base class for Endpoint implementations.
Definition: Endpoint.h:25
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.
Definition: Handler.h:13
Represents a Node the Endpoint knows about and can send messages to.
void UnregisterHandler(const std::string &name)
Unsubscribe from events.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48