gramods
Classes | Public Member Functions | Static Public Attributes | List of all members
gramods::gmNetwork::SyncNode Class Reference

A handler of network communication with multiple peers. More...

#include <SyncNode.hh>

Inheritance diagram for gramods::gmNetwork::SyncNode:
Inheritance graph
[legend]
Collaboration diagram for gramods::gmNetwork::SyncNode:
Collaboration graph
[legend]

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...
 
ProtocolgetProtocol (std::string name)
 Returns the protocol instance associated with the specified name, or nullptr if there is no protocol with that name. More...
 
ProtocolgetProtocol (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...
 
- Public Member Functions inherited from gramods::gmCore::Object
 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
 

Detailed Description

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.

Member Function Documentation

◆ addPeer()

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.

  • nnn.nnn.nnn.nnn
  • nnn.nnn.nnn.nnn:port
  • nnnn:nnn::nnnn:nn:nnnn
  • [nnnn:nnn:nnnn::nnn:nnn:nnnn:nnn]:port

XML-attribute: peer

◆ addProtocol()

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.

◆ getConnectedPeers()

std::set< std::size_t > gramods::gmNetwork::SyncNode::getConnectedPeers ( )

Return the indices of the currently connected peers.

◆ getDefaultKey()

virtual std::string gramods::gmNetwork::SyncNode::getDefaultKey ( )
inlineoverridevirtual

Returns the default key, in Configuration, for the Object.

Reimplemented from gramods::gmCore::Object.

◆ getLocalPeerIdx()

size_t gramods::gmNetwork::SyncNode::getLocalPeerIdx ( )

Returns the index of the local peer.

◆ getPeersCount()

std::size_t gramods::gmNetwork::SyncNode::getPeersCount ( )

Return the number of peers, not including the local peer.

◆ getProtocol() [1/3]

template<typename TYPE >
TYPE* gramods::gmNetwork::SyncNode::getProtocol ( )
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.

◆ getProtocol() [2/3]

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.

◆ getProtocol() [3/3]

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.

◆ getTimeoutDelay()

float gramods::gmNetwork::SyncNode::getTimeoutDelay ( )

Returns the current timeout delay.

See setTimeoutDelay for details.

◆ initialize()

void gramods::gmNetwork::SyncNode::initialize ( )
overridevirtual

Called to initialize the Object.

This should be called once only!

Reimplemented from gramods::gmCore::Object.

◆ isConnected()

bool gramods::gmNetwork::SyncNode::isConnected ( )

Check if all listed peers have connected.

◆ sendMessage()

void gramods::gmNetwork::SyncNode::sendMessage ( Protocol::Message  mess)

Sends the specified message to all peers.

◆ setExitWhenAPeerIsDisconnected()

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.

◆ setLocalPeerIdx()

void gramods::gmNetwork::SyncNode::setLocalPeerIdx ( size_t  idx)

Sets which of the peers that represents the local address.

XML-attribute: localPeerIdx

◆ setTimeoutDelay()

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.

◆ waitForConnection()

void gramods::gmNetwork::SyncNode::waitForConnection ( )

Wait until all listed peers have connected.

This can safely be called more than once without side effects.


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