gramods
Classes | Public Member Functions | List of all members
gramods::gmNetwork::DataSync Class Reference

Synchronization of simple data, such as boolean, float or integer values, over network. More...

#include <DataSync.hh>

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

Classes

struct  Impl
 

Public Member Functions

void addData (std::shared_ptr< SyncData > d)
 Adds a data container to be synchronized by the instance. More...
 
void addData (SyncData *d)
 Adds a data container to be synchronized by the instance. More...
 
void update ()
 Exchanges old data with newly received data in all associated data containers. More...
 
void processMessage (Message m) override
 Receives data values and sets the associated container. More...
 
void send (SyncData *d)
 Synchronizes the specified data object. More...
 
char getProtocolFlag () override
 Returns the header byte associated with the protocol, sent in the header of messages to indicate which protocol instance to call for interpretation and processing. More...
 
- Public Member Functions inherited from gramods::gmNetwork::Protocol
virtual void lostPeer (size_t idx)
 Called by the sync node when connection to one of the peers has been broken. More...
 
void setSyncNode (SyncNode *sync_node)
 

Additional Inherited Members

- Static Public Attributes inherited from gramods::gmNetwork::Protocol
static const size_t HEADER_LENGTH = Message().getHeader().size()
 
- Protected Member Functions inherited from gramods::gmNetwork::Protocol
void sendMessage (std::vector< char > data)
 Convenience method for creating a message and sending this to all peers. More...
 
size_t getLocalPeerIdx ()
 Convenience method for quering the SyncNode for the local peer idx. More...
 
std::set< size_t > getConnectedPeers ()
 Convenience method for quering the SyncNode for the currently connected peers. More...
 
- Protected Attributes inherited from gramods::gmNetwork::Protocol
SyncNodesync_node
 The SyncNode instance this protocol communicates through or nullptr if it has gone out of scope. More...
 
std::mutex sync_node_lock
 Lock for synchronizing the sync_node pointer. More...
 

Detailed Description

Synchronization of simple data, such as boolean, float or integer values, over network.

OBSERVE: The data sharing currently assumes, but does not test, that the peers have the same endianness.

The data synchronization applies these principles:

  1. The data containers are double buffered - data are set to the back buffer and read off the front buffer.
  2. Data are sent over network to all peers as soon as they are set and it is up to the client code to make sure that the peers do not overwrite each other's data.
  3. It is up to the client code to call update() when it is done writing new data and want to use these data, and it is also up to the client code to make sure that all peers are in the same stage of execution so that this update leads to consistent behaviour.

Typical usage

Global variables:

std::shared_ptr<gmNetwork::SyncNode> sync_node;
std::shared_ptr<gmNetwork::SyncSFloat> shared_time;
std::shared_ptr<gmNetwork::SyncSBool> shared_button;
SyncNode * sync_node
The SyncNode instance this protocol communicates through or nullptr if it has gone out of scope.
Definition: Protocol.hh:142

Initialize:

shared_time = std::make_shared<gmNetwork::SyncSFloat>();
shared_button = std::make_shared<gmNetwork::SyncSBool>();
gmCore::Configuration config(argc, argv);
if (!config.getObject(sync_node))
exit(1);
gmNetwork::DataSync * data_sync
= sync_node->getProtocol<gmNetwork::DataSync>();
data_sync->addData(shared_time);
data_sync->addData(shared_button);
TYPE * getProtocol()
Returns an instance of the templated protocol.
Definition: SyncNode.hh:129

Use:

// Set the back buffer and send data
*shared_time = 14.32;
*shared_button = false;
sync_node->getProtocol<gmNetwork::RunSync>()
->waitForAll();
sync_node->getProtocol<gmNetwork::DataSync>()
->update(); // <- moves received data to the front
// Read off front buffer
std::cerr << *shared_time << std::endl;
void update()
Exchanges old data with newly received data in all associated data containers.
Definition: DataSync.cpp:63

Member Function Documentation

◆ addData() [1/2]

void gramods::gmNetwork::DataSync::addData ( std::shared_ptr< SyncData d)

Adds a data container to be synchronized by the instance.

Subsequently setting the value of the specified container will send that value to the connected peers, and a received value will be set. Call update to make received values current.

Observe that all connected peers must have the exact same associated data containers.

◆ addData() [2/2]

void gramods::gmNetwork::DataSync::addData ( SyncData d)

Adds a data container to be synchronized by the instance.

Subsequently setting the value of the specified container will send that value to the connected peers, and a received value will be set. Call update to make received values current.

Observe that all connected peers must have the exact same associated data containers.

WARNING: the caller MUST make sure that the pointer is valid during the lifetime of this object.

◆ getProtocolFlag()

char gramods::gmNetwork::DataSync::getProtocolFlag ( )
inlineoverridevirtual

Returns the header byte associated with the protocol, sent in the header of messages to indicate which protocol instance to call for interpretation and processing.

Reimplemented from gramods::gmNetwork::Protocol.

◆ processMessage()

void gramods::gmNetwork::DataSync::processMessage ( Message  m)
overridevirtual

Receives data values and sets the associated container.

Reimplemented from gramods::gmNetwork::Protocol.

◆ send()

void gramods::gmNetwork::DataSync::send ( SyncData d)

Synchronizes the specified data object.

This method is automatically called when the value of a SyncData is set and should not be used by client code.

◆ update()

void gramods::gmNetwork::DataSync::update ( )

Exchanges old data with newly received data in all associated data containers.


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