Cogs.Foundation
|
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>
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. | |
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.
using Cogs::Network::MessageHub::HubList = std::vector< MessageHub* > |
using Cogs::Network::MessageHub::MessageQueue = std::deque< Message::Ptr > |
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().
|
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().
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().
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().
|
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().
|
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().
void Cogs::Network::MessageHub::disconnectFromAllSenders | ( | ) |
Disconnects this hub from all hubs to which it is listening.
*============================================================================================
void Cogs::Network::MessageHub::disconnectFromSender | ( | uint32_t | hubid | ) |
Disconnects this hub from the specified hub to which it is listening.
*============================================================================================
void Cogs::Network::MessageHub::flushQueuedMessages | ( | ) |
Flushes all pending messages from this hub without processing them.
*============================================================================================
|
inline |
|
protected |
Retrieves the next pending message.
Called by processMessages. *============================================================================================
Referenced by Cogs::Network::ConnectionHub::processOutgoing().
size_t Cogs::Network::MessageHub::getNoOfQueuedMessages | ( | ) |
Retrieves the number of messages currently awaiting processing by this hub.
*============================================================================================
|
inlinevirtual |
Reimplemented in Cogs::Network::SendOnlyMessageHub.
bool Cogs::Network::MessageHub::listenTo | ( | MessageHub * | hub, |
bool | bidirectional = false |
||
) |
Sets this MessageHub up to listen for messages from the specified hub.
*============================================================================================
References addListener().
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().
|
inlineprivatevirtual |
|
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. *============================================================================================
|
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.
void Cogs::Network::MessageHub::removeAllListeners | ( | ) |
Removes all listeners from this hub.
*============================================================================================
bool Cogs::Network::MessageHub::removeListener | ( | MessageHub * | hub | ) |
Removes the specified hub as a listener to this hub.
*============================================================================================
References removeSender().
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().
|
private |
Removes the specified hub from this hub's list of senders.
*============================================================================================
Referenced by removeListener().
|
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. *============================================================================================
|
private |
Mutex protecting the above lists.
|
private |
Our ID.
|
private |
List of hubs listening to our messages.
|
private |
Messages waiting to be processed by this hub.
|
private |
Mutex for the above queue.
|
staticconstexpr |
|
private |
List of hubs to which we are listening.