ping/Service.cpp
Go to the documentation of this file.
2 #include <swarmio/data/Message.pb.h>
3 #include <g3log/g3log.hpp>
4 #include <chrono>
5 
6 using namespace swarmio;
7 using namespace swarmio::services;
8 using namespace swarmio::services::ping;
9 
10 TimingAwaiter Service::Ping(Endpoint* endpoint, const Node* node, size_t size)
11 {
12  // Build message
13  data::Message message;
14  message.mutable_echo()->mutable_payload()->resize(size, 'P');
15 
16  // Log outgoing message
17  LOG(DBUG) << "A ping request will be sent to node [" << node->GetUUID() << "]";
18 
19  // Create awaiter and send message
20  endpoint->Tag(&message);
21  TimingAwaiter awaiter(endpoint, message.header().identifier());
22  endpoint->Send(&message, node);
23  return awaiter;
24 }
25 
26 bool Service::ReceiveMessage(const Node* sender, const data::Message* message)
27 {
28  // Sanity checks
29  CHECK(sender != nullptr) << "Sender address missing";
30  CHECK(message != nullptr) << "Message is missing";
31 
32  // Check message type
33  if (message->content_case() == data::Message::ContentCase::kEcho &&
34  message->header().reply_to() == 0)
35  {
36  // Log message
37  LOG(DBUG) << "A ping request was received from node [" << sender->GetUUID() << "] and will be answered";
38 
39  // Build message
40  data::Message reply;
41  reply.mutable_header()->set_reply_to(message->header().identifier());
42  reply.mutable_echo()->set_payload(message->echo().payload());
43 
44  // Send message
45  GetEndpoint()->Send(&reply, sender);
46 
47  // Mark as handled
48  return true;
49  }
50  else
51  {
52  return false;
53  }
54 }
55 
56 void Service::DescribeService(data::discovery::Response& descriptor)
57 {
58  // Mark the ECHO service as available
59  descriptor.set_echo_enabled(true);
60 }
virtual void Tag(data::Message *message)=0
Set the message identifier for a message.
static TimingAwaiter Ping(Endpoint *endpoint, const Node *node, size_t size)
Measure the latency to a remote node.
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 returns the roundtrip time of the Echo message.
Definition: TimingAwaiter.h:12
virtual bool ReceiveMessage(const Node *sender, const data::Message *message) override
Delivery point of all messages.
virtual void DescribeService(data::discovery::Response &descriptor) override
Add descriptors for the service to the discovery descriptor.
Abstract base class for Endpoint implementations.
Definition: Endpoint.h:25
Endpoint * GetEndpoint()
Get the associated Endpoint.
Definition: Mailbox.h:144
virtual const std::string & GetUUID() const =0
Returns the unique identifier of the node.
Represents a Node the Endpoint knows about and can send messages to.


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