Cogs.Foundation
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Cogs::Network::MessageHub Class Reference

A MessageHub connects to other hubs to form a simple peer-to-peer application-internal networking system for sending and receiving messages. More...

#include <MessageHub.h>

Inheritance diagram for Cogs::Network::MessageHub:
Cogs::Network::ConnectionHub Cogs::Network::SendOnlyMessageHub

Public Types

using MessageQueue = std::deque< Message::Ptr >
 
using HubList = std::vector< MessageHub * >
 

Public Member Functions

 MessageHub (uint32_t ident=0)
 Constructs a new MessageHub instance and adds it to the global hub list.
 
virtual ~MessageHub ()
 Removes this MessageHub from the global list and cleans up all connections to and from this hub.
 
bool addListener (MessageHub *hub, bool bidirectional=false)
 Adds the specified hub as a listener to this hub.
 
bool addListener (uint32_t hubid, bool bidirectional=false)
 Attempts to add the hub with the given ID as a listener to this hub.
 
bool listenTo (MessageHub *hub, bool bidirectional=false)
 Sets this MessageHub up to listen for messages from the specified hub.
 
bool listenTo (uint32_t hubid, bool bidirectional=false)
 Sets up this MessageHub to listen for messages from the hub with the specified ID (if a matching hub can be found).
 
bool removeListener (MessageHub *hub)
 Removes the specified hub as a listener to this hub.
 
bool removeListener (uint32_t hubid)
 Removes the hub with the given ID as a listener to this hub.
 
void removeAllListeners ()
 Removes all listeners from this hub.
 
void disconnectFromSender (uint32_t hubid)
 Disconnects this hub from the specified hub to which it is listening.
 
void disconnectFromAllSenders ()
 Disconnects this hub from all hubs to which it is listening.
 
virtual void broadcastMessage (const Message::Ptr &message)
 Broadcast message to all hubs.
 
virtual void sendMessage (const Message::Ptr &message)
 Send message to all hubs listening to this one.
 
virtual void queueMessage (const Message::Ptr &message)
 Queue a message for this hub to process.
 
virtual bool processMessages (size_t limit=noLimit)
 Process all queued messages for this hub.
 
virtual bool isSendOnly () const
 
uint32_t getID () const
 
size_t getNoOfQueuedMessages ()
 Retrieves the number of messages currently awaiting processing by this hub.
 
void flushQueuedMessages ()
 Flushes all pending messages from this hub without processing them.
 

Static Public Attributes

static constexpr size_t noLimit = 10000000
 

Protected Member Functions

Message::Ptr getNextMessage ()
 Retrieves the next pending message.
 

Private Member Functions

virtual void processMessage (Message &)
 
void addSender (MessageHub *sender)
 Adds the specified hub as a sender to which this hub is listening.
 
void removeSender (const MessageHub *sender)
 Removes the specified hub from this hub's list of senders.
 

Private Attributes

MessageQueue messages
 Messages waiting to be processed by this hub.
 
Mutex messagesMutex
 Mutex for the above queue.
 
HubList listeners
 List of hubs listening to our messages.
 
HubList senders
 List of hubs to which we are listening.
 
Mutex hubsMutex
 Mutex protecting the above lists.
 
uint32_t id
 Our ID.
 

Detailed Description

A MessageHub connects to other hubs to form a simple peer-to-peer application-internal networking system for sending and receiving messages.

Messages can be sent from a hub to all listening hubs by calling sendMessage, or can be broadcast to all hubs with broadcastMessage. Occasionally it is necessary to insert a message directly into the queue of another hub (or even your own hub). This can be done by calling queueMessage directly.

Message processing by a hub must be performed periodically by explicitly calling processMessages which, in turn, will call the virtual function processMessage for each pending message. It therefore follows that processMessage must be implemented by a derived class to actually handle any incoming messages.

Member Typedef Documentation

◆ HubList

◆ MessageQueue

Constructor & Destructor Documentation

◆ MessageHub()

Cogs::Network::MessageHub::MessageHub ( uint32_t  ident = 0)

Constructs a new MessageHub instance and adds it to the global hub list.

Instances of MessageHub can send messages, but to receive messages a derived class must be created that overrides and implements the processMessage function. *============================================================================================

References Cogs::Network::allHubs(), and Cogs::Network::allHubsMutex().

◆ ~MessageHub()

Cogs::Network::MessageHub::~MessageHub ( )
virtual

Removes this MessageHub from the global list and cleans up all connections to and from this hub.

*============================================================================================

References Cogs::Network::allHubs(), and Cogs::Network::allHubsMutex().

Member Function Documentation

◆ addListener() [1/2]

bool Cogs::Network::MessageHub::addListener ( MessageHub hub,
bool  bidirectional = false 
)

Adds the specified hub as a listener to this hub.

If biDirectional is true, this hub will also be added as a listener to the other hub, thus creating a two way communication channel. *============================================================================================

References addListener(), and addSender().

Referenced by addListener(), and listenTo().

◆ addListener() [2/2]

bool Cogs::Network::MessageHub::addListener ( uint32_t  hubid,
bool  bidirectional = false 
)

Attempts to add the hub with the given ID as a listener to this hub.

If biDirectional is true, this hub will also be added as a listener to the other hub, thus creating a two way communication channel. *============================================================================================

References Cogs::Network::find().

◆ addSender()

void Cogs::Network::MessageHub::addSender ( MessageHub sender)
private

Adds the specified hub as a sender to which this hub is listening.

Senders are stored so that a hub can gracefully disconnect itself from hubs to which it is listening when required. *============================================================================================

Referenced by addListener().

◆ broadcastMessage()

void Cogs::Network::MessageHub::broadcastMessage ( const Message::Ptr message)
virtual

Broadcast message to all hubs.

Broadcasts a message globally to all hubs currently active.

This hub will be excluded from the broadcast. *============================================================================================

References Cogs::Network::allHubs(), and Cogs::Network::allHubsMutex().

◆ disconnectFromAllSenders()

void Cogs::Network::MessageHub::disconnectFromAllSenders ( )

Disconnects this hub from all hubs to which it is listening.

*============================================================================================

◆ disconnectFromSender()

void Cogs::Network::MessageHub::disconnectFromSender ( uint32_t  hubid)

Disconnects this hub from the specified hub to which it is listening.

*============================================================================================

◆ flushQueuedMessages()

void Cogs::Network::MessageHub::flushQueuedMessages ( )

Flushes all pending messages from this hub without processing them.

*============================================================================================

◆ getID()

uint32_t Cogs::Network::MessageHub::getID ( ) const
inline

◆ getNextMessage()

Cogs::Network::Message::Ptr Cogs::Network::MessageHub::getNextMessage ( )
protected

Retrieves the next pending message.

Called by processMessages. *============================================================================================

Referenced by Cogs::Network::ConnectionHub::processOutgoing().

◆ getNoOfQueuedMessages()

size_t Cogs::Network::MessageHub::getNoOfQueuedMessages ( )

Retrieves the number of messages currently awaiting processing by this hub.

*============================================================================================

◆ isSendOnly()

virtual bool Cogs::Network::MessageHub::isSendOnly ( ) const
inlinevirtual

◆ listenTo() [1/2]

bool Cogs::Network::MessageHub::listenTo ( MessageHub hub,
bool  bidirectional = false 
)

Sets this MessageHub up to listen for messages from the specified hub.

*============================================================================================

References addListener().

◆ listenTo() [2/2]

bool Cogs::Network::MessageHub::listenTo ( uint32_t  hubid,
bool  bidirectional = false 
)

Sets up this MessageHub to listen for messages from the hub with the specified ID (if a matching hub can be found).

*============================================================================================

References Cogs::Network::find().

◆ processMessage()

virtual void Cogs::Network::MessageHub::processMessage ( Message )
inlineprivatevirtual

◆ processMessages()

bool Cogs::Network::MessageHub::processMessages ( size_t  limit = noLimit)
virtual

Process all queued messages for this hub.

Processes all messages that have been received by this MessageHub.

Returns true if any messages were processed.

This function should be called periodically by the owner of a MessageHub to ensure received messages are actually handled.

A MessageHub implementation must override the processdMessage function to receive and process incoming messages. *============================================================================================

◆ queueMessage()

void Cogs::Network::MessageHub::queueMessage ( const Message::Ptr message)
virtual

Queue a message for this hub to process.

Directly add the specified message to the incoming queue of this hub.

Normally messages are sent from a hub to all its listeners using the sendMessage function. This function, however, allows you to directly input a message into the queue of a single hub. *============================================================================================

Reimplemented in Cogs::Network::SendOnlyMessageHub.

◆ removeAllListeners()

void Cogs::Network::MessageHub::removeAllListeners ( )

Removes all listeners from this hub.

*============================================================================================

◆ removeListener() [1/2]

bool Cogs::Network::MessageHub::removeListener ( MessageHub hub)

Removes the specified hub as a listener to this hub.

*============================================================================================

References removeSender().

◆ removeListener() [2/2]

bool Cogs::Network::MessageHub::removeListener ( uint32_t  hubid)

Removes the hub with the given ID as a listener to this hub.

*============================================================================================

References Cogs::Network::find().

◆ removeSender()

void Cogs::Network::MessageHub::removeSender ( const MessageHub sender)
private

Removes the specified hub from this hub's list of senders.

*============================================================================================

Referenced by removeListener().

◆ sendMessage()

void Cogs::Network::MessageHub::sendMessage ( const Message::Ptr message)
virtual

Send message to all hubs listening to this one.

Sends a message from this hub.

The message will be queued for later processing in all hubs that are listening to this hub. Hubs (or their owners) must call processMessages periodically to process any pending messages. *============================================================================================

Member Data Documentation

◆ hubsMutex

Mutex Cogs::Network::MessageHub::hubsMutex
private

Mutex protecting the above lists.

◆ id

uint32_t Cogs::Network::MessageHub::id
private

Our ID.

◆ listeners

HubList Cogs::Network::MessageHub::listeners
private

List of hubs listening to our messages.

◆ messages

MessageQueue Cogs::Network::MessageHub::messages
private

Messages waiting to be processed by this hub.

◆ messagesMutex

Mutex Cogs::Network::MessageHub::messagesMutex
private

Mutex for the above queue.

◆ noLimit

constexpr size_t Cogs::Network::MessageHub::noLimit = 10000000
staticconstexpr

◆ senders

HubList Cogs::Network::MessageHub::senders
private

List of hubs to which we are listening.


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