Cogs.Core
|
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.
Definition at line 30 of file MessageHub.h.
using Cogs::Network::MessageHub::HubList = std::vector< MessageHub* > |
Definition at line 33 of file MessageHub.h.
using Cogs::Network::MessageHub::MessageQueue = std::deque< Message::Ptr > |
Definition at line 32 of file MessageHub.h.
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. *============================================================================================
Definition at line 46 of file MessageHub.cpp.
|
virtual |
Removes this MessageHub from the global list and cleans up all connections to and from this hub.
*============================================================================================
Definition at line 58 of file MessageHub.cpp.
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. *============================================================================================
Definition at line 77 of file MessageHub.cpp.
References addListener(), and addSender().
Referenced by addListener(), listenTo(), and Cogs::Core::GuiSystem::update().
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. *============================================================================================
Definition at line 110 of file MessageHub.cpp.
|
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. *============================================================================================
Definition at line 324 of file MessageHub.cpp.
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. *============================================================================================
Definition at line 214 of file MessageHub.cpp.
void Cogs::Network::MessageHub::disconnectFromAllSenders | ( | ) |
Disconnects this hub from all hubs to which it is listening.
*============================================================================================
Definition at line 197 of file MessageHub.cpp.
void Cogs::Network::MessageHub::disconnectFromSender | ( | uint32_t | hubid | ) |
Disconnects this hub from the specified hub to which it is listening.
*============================================================================================
Definition at line 179 of file MessageHub.cpp.
void Cogs::Network::MessageHub::flushQueuedMessages | ( | ) |
Flushes all pending messages from this hub without processing them.
*============================================================================================
Definition at line 295 of file MessageHub.cpp.
|
inline |
Definition at line 57 of file MessageHub.h.
|
protected |
Retrieves the next pending message.
Called by processMessages. *============================================================================================
Definition at line 306 of file MessageHub.cpp.
size_t Cogs::Network::MessageHub::getNoOfQueuedMessages | ( | ) |
Retrieves the number of messages currently awaiting processing by this hub.
*============================================================================================
Definition at line 286 of file MessageHub.cpp.
|
inlinevirtual |
Definition at line 55 of file MessageHub.h.
bool Cogs::Network::MessageHub::listenTo | ( | MessageHub * | hub, |
bool | bidirectional = false |
||
) |
Sets this MessageHub up to listen for messages from the specified hub.
*============================================================================================
Definition at line 117 of file MessageHub.cpp.
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).
*============================================================================================
Definition at line 125 of file MessageHub.cpp.
|
inlineprivatevirtual |
Definition at line 73 of file MessageHub.h.
|
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. *============================================================================================
Definition at line 248 of file MessageHub.cpp.
|
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.
Definition at line 275 of file MessageHub.cpp.
void Cogs::Network::MessageHub::removeAllListeners | ( | ) |
Removes all listeners from this hub.
*============================================================================================
Definition at line 164 of file MessageHub.cpp.
bool Cogs::Network::MessageHub::removeListener | ( | MessageHub * | hub | ) |
Removes the specified hub as a listener to this hub.
*============================================================================================
Definition at line 132 of file MessageHub.cpp.
References removeSender().
bool Cogs::Network::MessageHub::removeListener | ( | uint32_t | hubid | ) |
Removes the hub with the given ID as a listener to this hub.
*============================================================================================
Definition at line 157 of file MessageHub.cpp.
|
private |
Removes the specified hub from this hub's list of senders.
*============================================================================================
Definition at line 338 of file MessageHub.cpp.
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. *============================================================================================
Definition at line 231 of file MessageHub.cpp.
|
private |
Mutex protecting the above lists.
Definition at line 70 of file MessageHub.h.
|
private |
Our ID.
Definition at line 71 of file MessageHub.h.
|
private |
List of hubs listening to our messages.
Definition at line 68 of file MessageHub.h.
|
private |
Messages waiting to be processed by this hub.
Definition at line 65 of file MessageHub.h.
|
private |
Mutex for the above queue.
Definition at line 66 of file MessageHub.h.
|
staticconstexpr |
Definition at line 35 of file MessageHub.h.
|
private |
List of hubs to which we are listening.
Definition at line 69 of file MessageHub.h.