Cogs.Core
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::Core::GuiContainer 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.

Definition at line 30 of file MessageHub.h.

Member Typedef Documentation

◆ HubList

using Cogs::Network::MessageHub::HubList = std::vector< MessageHub* >

Definition at line 33 of file MessageHub.h.

◆ MessageQueue

using Cogs::Network::MessageHub::MessageQueue = std::deque< Message::Ptr >

Definition at line 32 of file MessageHub.h.

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. *============================================================================================

Definition at line 46 of file MessageHub.cpp.

◆ ~MessageHub()

Cogs::Network::MessageHub::~MessageHub ( )
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.

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. *============================================================================================

Definition at line 77 of file MessageHub.cpp.

References addListener(), and addSender().

Referenced by addListener(), listenTo(), and Cogs::Core::GuiSystem::update().

◆ 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. *============================================================================================

Definition at line 110 of file MessageHub.cpp.

◆ 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. *============================================================================================

Definition at line 324 of file MessageHub.cpp.

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. *============================================================================================

Definition at line 214 of file MessageHub.cpp.

◆ disconnectFromAllSenders()

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

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

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

Definition at line 197 of file MessageHub.cpp.

◆ disconnectFromSender()

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.

◆ flushQueuedMessages()

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

Flushes all pending messages from this hub without processing them.

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

Definition at line 295 of file MessageHub.cpp.

◆ getID()

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

Definition at line 57 of file MessageHub.h.

◆ getNextMessage()

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

Retrieves the next pending message.

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

Definition at line 306 of file MessageHub.cpp.

◆ getNoOfQueuedMessages()

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.

◆ isSendOnly()

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

Definition at line 55 of file MessageHub.h.

◆ 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.

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

Definition at line 117 of file MessageHub.cpp.

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).

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

Definition at line 125 of file MessageHub.cpp.

◆ processMessage()

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

Definition at line 73 of file MessageHub.h.

◆ 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. *============================================================================================

Definition at line 248 of file MessageHub.cpp.

◆ 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.

Definition at line 275 of file MessageHub.cpp.

◆ removeAllListeners()

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

Removes all listeners from this hub.

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

Definition at line 164 of file MessageHub.cpp.

◆ removeListener() [1/2]

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().

◆ removeListener() [2/2]

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.

◆ removeSender()

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

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

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

Definition at line 338 of file MessageHub.cpp.

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. *============================================================================================

Definition at line 231 of file MessageHub.cpp.

Member Data Documentation

◆ hubsMutex

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

Mutex protecting the above lists.

Definition at line 70 of file MessageHub.h.

◆ id

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

Our ID.

Definition at line 71 of file MessageHub.h.

◆ listeners

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

List of hubs listening to our messages.

Definition at line 68 of file MessageHub.h.

◆ messages

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

Messages waiting to be processed by this hub.

Definition at line 65 of file MessageHub.h.

◆ messagesMutex

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

Mutex for the above queue.

Definition at line 66 of file MessageHub.h.

◆ noLimit

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

Definition at line 35 of file MessageHub.h.

◆ senders

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

List of hubs to which we are listening.

Definition at line 69 of file MessageHub.h.


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