gramods
|
A handler of network communication with multiple peers. More...
#include <SyncNode.hh>
Classes | |
struct | Impl |
Public Member Functions | |
void | addPeer (std::string address) |
Adds a peer to the list of peers in the cluster. More... | |
void | setLocalPeerIdx (size_t idx) |
Sets which of the peers that represents the local address. More... | |
size_t | getLocalPeerIdx () |
Returns the index of the local peer. More... | |
void | setExitWhenAPeerIsDisconnected (bool on) |
Set whether or not the application should exit when connection to another node is lost. More... | |
void | setTimeoutDelay (float t) |
Sets the number of seconds to wait before assuming that a silent peer has disconnected. More... | |
float | getTimeoutDelay () |
Returns the current timeout delay. More... | |
void | waitForConnection () |
Wait until all listed peers have connected. More... | |
bool | isConnected () |
Check if all listed peers have connected. More... | |
std::size_t | getPeersCount () |
Return the number of peers, not including the local peer. More... | |
std::set< std::size_t > | getConnectedPeers () |
Return the indices of the currently connected peers. More... | |
template<typename TYPE > | |
TYPE * | getProtocol () |
Returns an instance of the templated protocol. More... | |
Protocol * | getProtocol (std::string name) |
Returns the protocol instance associated with the specified name, or nullptr if there is no protocol with that name. More... | |
Protocol * | getProtocol (char id) |
Returns the protocol instance associated with the specified protocol id, or nullptr if there is no protocol with that id. More... | |
void | addProtocol (std::string name, std::shared_ptr< Protocol > prot) |
Associates the specified protocol with the specified name. More... | |
void | sendMessage (Protocol::Message mess) |
Sends the specified message to all peers. More... | |
void | initialize () override |
Called to initialize the Object. More... | |
virtual std::string | getDefaultKey () override |
Returns the default key, in Configuration, for the Object. More... | |
![]() | |
Object () | |
Initializes internal data. More... | |
virtual | ~Object () |
Cleaning up internal data. More... | |
virtual void | accept (Visitor *visitor) |
Calls the visitors apply method with this as argument. More... | |
virtual void | traverse (Visitor *visitor) |
Calls the child Object's accept method, if such exist. More... | |
bool | isInitialized () |
Returns true if the Object is initialized. More... | |
Static Public Attributes | |
static gramods::gmCore::OFactory::OFactoryInformation | _gm_ofi |
A handler of network communication with multiple peers.
This class will forward messages from protocol instances to all or selected peers, and forward incoming messages from the peers to the protocol instances.
The SyncNode does not enforce a primary-replica or client-server architecture, but employ a peer-to-peer architecture. However, client software may use the local peer index (getLocalPeerIdx) and assume, for example, that the peer with local peer index of zero is the primary.
void gramods::gmNetwork::SyncNode::addPeer | ( | std::string | address | ) |
Adds a peer to the list of peers in the cluster.
This node's own address should also be added. It is assumed that this list is identical with all nodes in the cluster and that there is exactly one node with each local peer idx (0 - N-1).
Valid syntax is ipv4 or ipv6 with or without port/service (defaults to 20401) e.g.
XML-attribute: peer
void gramods::gmNetwork::SyncNode::addProtocol | ( | std::string | name, |
std::shared_ptr< Protocol > | prot | ||
) |
Associates the specified protocol with the specified name.
An exception will be thrown if there is already a protocol associated with that name.
std::set< std::size_t > gramods::gmNetwork::SyncNode::getConnectedPeers | ( | ) |
Return the indices of the currently connected peers.
|
inlineoverridevirtual |
Returns the default key, in Configuration, for the Object.
Reimplemented from gramods::gmCore::Object.
size_t gramods::gmNetwork::SyncNode::getLocalPeerIdx | ( | ) |
Returns the index of the local peer.
std::size_t gramods::gmNetwork::SyncNode::getPeersCount | ( | ) |
Return the number of peers, not including the local peer.
|
inline |
Returns an instance of the templated protocol.
If there is no instance of this protocol type already, then a new instance is constructed and returned. Caller should not have to check for nullptr since any (rare) error should cause an exception to be thrown.
Ownership of the protocol instance is kept by the sync node and all such instances will be deleted when the sync node is deleted, for example when going out of scope. Until then, however, the returned raw pointer is valid.
Protocol * gramods::gmNetwork::SyncNode::getProtocol | ( | char | id | ) |
Returns the protocol instance associated with the specified protocol id, or nullptr if there is no protocol with that id.
Protocol * gramods::gmNetwork::SyncNode::getProtocol | ( | std::string | name | ) |
Returns the protocol instance associated with the specified name, or nullptr if there is no protocol with that name.
float gramods::gmNetwork::SyncNode::getTimeoutDelay | ( | ) |
Returns the current timeout delay.
See setTimeoutDelay for details.
|
overridevirtual |
Called to initialize the Object.
This should be called once only!
Reimplemented from gramods::gmCore::Object.
bool gramods::gmNetwork::SyncNode::isConnected | ( | ) |
Check if all listed peers have connected.
void gramods::gmNetwork::SyncNode::sendMessage | ( | Protocol::Message | mess | ) |
Sends the specified message to all peers.
void gramods::gmNetwork::SyncNode::setExitWhenAPeerIsDisconnected | ( | bool | on | ) |
Set whether or not the application should exit when connection to another node is lost.
If set to true an ExitException is thrown from getConnectedPeers if a previously connected peer has lost its connection. Default is false, meaning that the node will not* exit.
void gramods::gmNetwork::SyncNode::setLocalPeerIdx | ( | size_t | idx | ) |
Sets which of the peers that represents the local address.
XML-attribute: localPeerIdx
void gramods::gmNetwork::SyncNode::setTimeoutDelay | ( | float | t | ) |
Sets the number of seconds to wait before assuming that a silent peer has disconnected.
If there has been no message after half this time, a keep-alive ping message is sent, that should produce a keep-alive pong message from the other peer. Default is a 1.0 seconds timeout.
void gramods::gmNetwork::SyncNode::waitForConnection | ( | ) |
Wait until all listed peers have connected.
This can safely be called more than once without side effects.