discovery/Service.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <swarmio/Mailbox.h>
7 #include <set>
8 #include <map>
9 #include <mutex>
10 
12 {
18  class SWARMIO_API Service final : public Mailbox
19  {
20  private:
21 
27 
32  std::map<const Node*, data::discovery::Response> _remotes;
33 
38  std::mutex _remotesMutex;
39 
44  std::set<Observer*> _observers;
45 
50  std::mutex _observersMutex;
51 
56  std::set<Discoverable*> _discoverables;
57 
62  std::mutex _discoverablesMutex;
63 
68  bool _cacheValid = true;
69 
74  data::discovery::Response _cachedResponse;
75 
82  void HandleDiscoveryRequest(const Node* node, const data::Message* message);
83 
90  void CacheDiscoveryResponse(const Node* node, const data::Message* message);
91 
99  void HandleInvalidationRequest(const Node* node, const data::Message* message);
100 
101  public:
102 
110  Service(Endpoint* endpoint, bool performActiveDiscovery)
111  : Mailbox(endpoint), _performActiveDiscovery(performActiveDiscovery)
112  {
113  FinishConstruction();
114  }
115 
122  static DiscoveryAwaiter Query(Endpoint* endpoint, const Node* node);
123 
131  DiscoveryAwaiter CachedQuery(const Node* node);
132 
137  void GlobalQuery();
138 
145  std::map<const Node*, data::discovery::Response> GetCachedNodeInformation();
146 
152  void RegisterObserver(Observer* observer)
153  {
154  std::unique_lock<std::mutex> guard(_observersMutex);
155  _observers.insert(observer);
156  }
162  void UnregisterObserver(Observer* observer)
163  {
164  std::unique_lock<std::mutex> guard(_observersMutex);
165  _observers.erase(observer);
166  }
167 
172  void Invalidate();
173 
179  void RegisterDiscoverable(Discoverable* discoverable);
180 
186  void UnregisterDiscoverable(Discoverable* discoverable);
187 
196  virtual bool ReceiveMessage(const Node* sender, const data::Message* message) override;
197 
203  virtual void NodeDidJoin(const Node* node) noexcept override;
204 
210  virtual void NodeWillLeave(const Node* node) noexcept override;
211  };
212 }
std::map< const Node *, data::discovery::Response > _remotes
Cache of remote node discovery responses.
Interface for discovery observers.
The Discovery Service makes it possible to query remote nodes for services.
Service(Endpoint *endpoint, bool performActiveDiscovery)
Construct a new Service object.
std::set< Observer * > _observers
List of discovery observers.
An Awaiter that returns discovery data on a remote node.
data::discovery::Response _cachedResponse
Cached discovery response.
std::mutex _remotesMutex
Mutex to protect remotes list.
std::mutex _observersMutex
Mutex to protect observer list.
Abstract base class for Endpoint implementations.
Definition: Endpoint.h:25
bool _performActiveDiscovery
Should we send discovery requests automatically?
std::mutex _discoverablesMutex
Mutex to protect discoverable list.
Interface for discoverable services.
Definition: Discoverable.h:12
void UnregisterObserver(Observer *observer)
Unregister a discovery observer.
Represents a Node the Endpoint knows about and can send messages to.
Abstract base class for Mailbox implementations.
Definition: Mailbox.h:13
void RegisterObserver(Observer *observer)
Register a new discovery observer.
std::set< Discoverable * > _discoverables
List of discoverable services.


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