coral
A C++ library for distributed co-simulation
service.hpp
Go to the documentation of this file.
1 
10 #ifndef CORAL_NET_SERVICE_HPP
11 #define CORAL_NET_SERVICE_HPP
12 
13 #include <chrono>
14 #include <cstdint>
15 #include <functional>
16 #include <string>
17 #include <thread>
18 
19 #include "zmq.hpp"
20 
21 #include "coral/config.h"
22 #include "coral/net.hpp"
23 #include "coral/net/reactor.hpp"
24 
25 
26 namespace coral
27 {
28 namespace net
29 {
30 
32 namespace service
33 {
34 
35 
47 class Beacon
48 {
49 public:
87  Beacon(
88  std::uint32_t partitionID,
89  const std::string& serviceType,
90  const std::string& serviceIdentifier,
91  const char* payload,
92  std::size_t payloadSize,
94  const ip::Address& networkInterface,
95  ip::Port port);
96 
106  ~Beacon() CORAL_NOEXCEPT;
107 
108  Beacon(const Beacon&) = delete;
109  Beacon& operator=(const Beacon&) = delete;
110 
111  CORAL_DEFINE_DEFAULT_MOVE(Beacon, m_thread, m_socket);
112 
114  void Stop();
115 
116 private:
117  std::thread m_thread;
118  zmq::socket_t m_socket;
119 };
120 
121 
133 class Listener
134 {
135 public:
151  using NotificationHandler = std::function<void (
152  const ip::Address&,
153  const std::string&,
154  const std::string&,
155  const char*,
157 
176  Listener(
177  coral::net::Reactor& reactor,
178  std::uint32_t partitionID,
179  const ip::Endpoint& endpoint,
180  NotificationHandler onNotification);
181 
183  ~Listener() CORAL_NOEXCEPT;
184 
185  Listener(const Listener&) = delete;
186  Listener& operator=(const Listener&) = delete;
187 
189  Listener(Listener&&) CORAL_NOEXCEPT;
190 
192  Listener& operator=(Listener&&) CORAL_NOEXCEPT;
193 
194 private:
195  class Impl;
196  std::unique_ptr<Impl> m_impl;
197 };
198 
199 
213 class Tracker
214 {
215 public:
231  using AppearedHandler = std::function<void (
232  const ip::Address&,
233  const std::string&,
234  const std::string&,
235  const char*,
237 
255 
266  using DisappearedHandler =
268 
284  Tracker(
285  coral::net::Reactor& reactor,
286  std::uint32_t partitionID,
287  const ip::Endpoint& endpoint);
288 
290  ~Tracker() CORAL_NOEXCEPT;
291 
292  Tracker(const Tracker&) = delete;
293  Tracker& operator=(const Tracker&) = delete;
294 
296  Tracker(Tracker&&) CORAL_NOEXCEPT;
297 
299  Tracker& operator=(Tracker&&) CORAL_NOEXCEPT;
300 
323  void AddTrackedServiceType(
324  const std::string& serviceType,
325  std::chrono::milliseconds expiryTime,
326  AppearedHandler onAppearance,
327  PayloadChangedHandler onPayloadChange,
328  DisappearedHandler onDisappearance);
329 
330 private:
331  class Impl;
332  std::unique_ptr<Impl> m_impl;
333 };
334 
335 
336 }}} // namespace
337 #endif // header guard
An object which represents an internet port number.
Definition: net.hpp:156
Beacon(std::uint32_t partitionID, const std::string &serviceType, const std::string &serviceIdentifier, const char *payload, std::size_t payloadSize, std::chrono::milliseconds period, const ip::Address &networkInterface, ip::Port port)
Constructor.
Contains the coral::net::Reactor class and related functionality.
Main module header for coral::net.
A class for broadcasting information about a service, so it can be automatically detected on a networ...
Definition: service.hpp:47
A class for keeping track of services on a network.
Definition: service.hpp:213
STL namespace.
~Beacon() CORAL_NOEXCEPT
Destructor.
STL class.
An object which identifies an internet host or network interface as either an IPv4 address or a textu...
Definition: net.hpp:80
An object which identifies an endpoint for Internet communication as a combination of an address and ...
Definition: net.hpp:209
Definition: variable_io.hpp:28
A class for detecting services on a network.
Definition: service.hpp:133
void Stop()
Stops broadcasting service information.
An implementation of the reactor pattern.
Definition: reactor.hpp:41
STL class.