coral
A C++ library for distributed co-simulation
Public Types | Public Member Functions | List of all members
coral::net::service::Tracker Class Reference

A class for keeping track of services on a network. More...

#include "coral/net/service.hpp"

Public Types

using AppearedHandler = std::function< void(const ip::Address &, const std::string &, const std::string &, const char *, std::size_t)>
 The type for functions that are called when a service is discovered. More...
 
using PayloadChangedHandler = AppearedHandler
 The type for functions that are called when a service changes its data payload. More...
 
using DisappearedHandler = std::function< void(const std::string &, const std::string &)>
 The type for functions that are called when a service disappears. More...
 

Public Member Functions

 Tracker (coral::net::Reactor &reactor, std::uint32_t partitionID, const ip::Endpoint &endpoint)
 Constructor. More...
 
 ~Tracker () CORAL_NOEXCEPT
 Destructor.
 
 Tracker (Tracker &&) CORAL_NOEXCEPT
 Move constructor.
 
Trackeroperator= (Tracker &&) CORAL_NOEXCEPT
 Move assignment operator.
 
void AddTrackedServiceType (const std::string &serviceType, std::chrono::milliseconds expiryTime, AppearedHandler onAppearance, PayloadChangedHandler onPayloadChange, DisappearedHandler onDisappearance)
 Adds (or updates the settings for) a tracked service type. More...
 

Detailed Description

A class for keeping track of services on a network.

An object of this class can be used to keep track of services that announce their presence using Beacon. It is built on top of Listener, but rather than forwarding "raw" beacon pings, it translates these into events that indicate whether a new service has appeared on the network, whether one has disappeared, or whether one has changed its data payload.

Unlike Beacon, this class does not create a background thread; rather it uses the reactor pattern (specifically, coral::net::Reactor) to deal with incoming data in the current thread.

Member Typedef Documentation

The type for functions that are called when a service is discovered.

Such a function must have the following signature:

void handler(
const coral::net::ip::Address& address, // the service's IP address
const std::string& serviceType, // the service type (see Beacon)
const std::string& serviceID, // the service name (see Beacon)
const char* payload, // data payload (or null if none)
std::size_t payloadSize); // data payload size

The payload array is not guaranteed to exist beyond this function call, so a copy must be made if the data is to be kept around.

The type for functions that are called when a service disappears.

Such a function must have the following signature:

void handler(
const std::string& serviceType, // the service type (see Beacon)
const std::string& serviceID); // the service name (see Beacon)

The type for functions that are called when a service changes its data payload.

Such a function must have the following signature:

void handler(
const coral::net::ip::Address& address, // the service's IP address
const std::string& serviceType, // the service type (see Beacon)
const std::string& serviceID, // the service name (see Beacon)
const char* payload, // data payload (or null if none)
std::size_t payloadSize); // data payload size

The payload array is not guaranteed to exist beyond this function call, so a copy must be made if the data is to be kept around.

Constructor & Destructor Documentation

coral::net::service::Tracker::Tracker ( coral::net::Reactor reactor,
std::uint32_t  partitionID,
const ip::Endpoint endpoint 
)

Constructor.

Parameters
[in]reactorUsed to listen for incoming data.
[in]partitionIDThis must match the partition ID of any Beacon one wishes to detect.
[in]endpointThe name or IP address of the network interface, together with the UDP port, to listen on. The name may be "*" to listen on all interfaces. The port number must match the port used in the Beacon.
Exceptions
std::runtime_erroron network error.

Member Function Documentation

void coral::net::service::Tracker::AddTrackedServiceType ( const std::string serviceType,
std::chrono::milliseconds  expiryTime,
AppearedHandler  onAppearance,
PayloadChangedHandler  onPayloadChange,
DisappearedHandler  onDisappearance 
)

Adds (or updates the settings for) a tracked service type.

Parameters
[in]serviceTypeThe service type to listen for.
[in]expiryTimeHow long a period of silence from a particular service of this type must pass before it is considered to have disappeared. This should be at least a few times larger than the services' beacon period.
[in]onAppearanceA function that should be called when a new service of this type is discovered, or nullptr if this event is not to be handled.
[in]onPayloadChangeA function that should be called when a previously discoverd service of this type changes its data "payload", or nullptr if this event is not to be handled.
[in]onDisappearanceA function that should be called when a previously discovered service of this type disappears again (i.e. a period of timeout has passed without any pings having been received from it), or nullptr if this event is not to be handled.

The documentation for this class was generated from the following file: