Ocean
Ocean::Network::MessageQueue Class Reference

This class implements a message queue. More...

Public Types

typedef unsigned int Id
 Definition of a message id. More...
 

Public Member Functions

 MessageQueue ()=default
 Creates a new message queue. More...
 
 MessageQueue (MessageQueue &&messageQueue)
 Move constructor. More...
 
 MessageQueue (const unsigned int oldMessageAge)
 Creates a new message queue. More...
 
 ~MessageQueue ()
 Destructs a message queue. More...
 
bool push (const Id id, const std::string &message, const std::string &value)
 Pushes a new message. More...
 
bool front (const Id id, std::string &message, std::string &value)
 Gets the front message for a given message id. More...
 
bool front (const Id id, const double timeout, std::string &message, std::string &value)
 Gets the front message for a given message id and waits a specified time if no message is available. More...
 
void pop (const Id id)
 Pops the front message with a specified message id from the queue. More...
 
std::string pop (const Id id, const double timeout)
 Returns and pops the front message for a given message id and waits a specified time if no message is available. More...
 
bool pop (const Id id, const double timeout, std::string &message, std::string &value)
 Returns and pops the front message for a given message id and waits a specified time if no message is available. More...
 
void clear ()
 Clears the entire message queue. More...
 
void clear (const Id id)
 Clears all messages with a specified message id. More...
 
Id uniqueId ()
 Returns a unique message id within this message queue. More...
 
Id lastUniqueId ()
 Returns the last unique message id again. More...
 
MessageQueueoperator= (MessageQueue &&messageQueue)
 Move operator. More...
 

Static Public Member Functions

static constexpr Id invalidId ()
 Returns an invalid message id. More...
 

Protected Attributes

MessageMap messageMap_
 Map holding all messages. More...
 
unsigned int oldMessageAge_ = 500u
 Definition of an message age of old and therefore unused messages. More...
 
Id idCounter_ = invalidId()
 Message id counter. More...
 
Lock lock_
 Map lock. More...
 

Private Types

typedef std::pair< std::string, std::string > Message
 Definition of a message pair. More...
 
typedef std::queue< MessageQueue
 Definition of a queue holding message pairs. More...
 
typedef std::unordered_map< Id, QueueMessageMap
 Definition of a map mapping message ids to message queues. More...
 

Detailed Description

This class implements a message queue.

Member Typedef Documentation

◆ Id

typedef unsigned int Ocean::Network::MessageQueue::Id

Definition of a message id.

◆ Message

typedef std::pair<std::string, std::string> Ocean::Network::MessageQueue::Message
private

Definition of a message pair.

◆ MessageMap

typedef std::unordered_map<Id, Queue> Ocean::Network::MessageQueue::MessageMap
private

Definition of a map mapping message ids to message queues.

◆ Queue

typedef std::queue<Message> Ocean::Network::MessageQueue::Queue
private

Definition of a queue holding message pairs.

Constructor & Destructor Documentation

◆ MessageQueue() [1/3]

Ocean::Network::MessageQueue::MessageQueue ( )
default

Creates a new message queue.

◆ MessageQueue() [2/3]

Ocean::Network::MessageQueue::MessageQueue ( MessageQueue &&  messageQueue)

Move constructor.

Parameters
messageQueueThe message queue object to be moved

◆ MessageQueue() [3/3]

Ocean::Network::MessageQueue::MessageQueue ( const unsigned int  oldMessageAge)
explicit

Creates a new message queue.

Parameters
oldMessageAgeThe capacity of the queue, with range [1, infinity)

◆ ~MessageQueue()

Ocean::Network::MessageQueue::~MessageQueue ( )

Destructs a message queue.

Member Function Documentation

◆ clear() [1/2]

void Ocean::Network::MessageQueue::clear ( )

Clears the entire message queue.

◆ clear() [2/2]

void Ocean::Network::MessageQueue::clear ( const Id  id)

Clears all messages with a specified message id.

Parameters
idMessage id to clear all messages for

◆ front() [1/2]

bool Ocean::Network::MessageQueue::front ( const Id  id,
const double  timeout,
std::string &  message,
std::string &  value 
)

Gets the front message for a given message id and waits a specified time if no message is available.

Parameters
idMessage id to get the front message for
timeoutTime to wait in seconds
messageResulting message
valueResulting optional value
Returns
True, if a message could be returned within the specified wait time

◆ front() [2/2]

bool Ocean::Network::MessageQueue::front ( const Id  id,
std::string &  message,
std::string &  value 
)

Gets the front message for a given message id.

Parameters
idMessage id to get the front message for
messageResulting message
valueResulting optional value
Returns
True, if succeeded

◆ invalidId()

constexpr MessageQueue::Id Ocean::Network::MessageQueue::invalidId ( )
staticconstexpr

Returns an invalid message id.

Returns
Invalid message id

◆ lastUniqueId()

Id Ocean::Network::MessageQueue::lastUniqueId ( )

Returns the last unique message id again.

Beware: Do no use this id directly, use it for control purpose only!

Returns
Last unique id

◆ operator=()

MessageQueue& Ocean::Network::MessageQueue::operator= ( MessageQueue &&  messageQueue)

Move operator.

Parameters
messageQueueThe message queue to be moved
Returns
Reference to this object

◆ pop() [1/3]

void Ocean::Network::MessageQueue::pop ( const Id  id)

Pops the front message with a specified message id from the queue.

Parameters
idMessage id to pop the front message for

◆ pop() [2/3]

std::string Ocean::Network::MessageQueue::pop ( const Id  id,
const double  timeout 
)

Returns and pops the front message for a given message id and waits a specified time if no message is available.

Parameters
idMessage id to get the front message for
timeoutTime to wait in seconds
Returns
Resulting message, if any

◆ pop() [3/3]

bool Ocean::Network::MessageQueue::pop ( const Id  id,
const double  timeout,
std::string &  message,
std::string &  value 
)

Returns and pops the front message for a given message id and waits a specified time if no message is available.

Parameters
idMessage id to get the front message for
timeoutTime to wait in seconds
messageResulting message
valueResulting optional value
Returns
True, if a message could be popped within the specified wait time

◆ push()

bool Ocean::Network::MessageQueue::push ( const Id  id,
const std::string &  message,
const std::string &  value 
)

Pushes a new message.

Beware: To avoid a message overflow all messages older than the defined 'age' will be released.

Parameters
idMessage id
messageThe message to push
valueOptional message value to push
Returns
True, if succeeded

◆ uniqueId()

Id Ocean::Network::MessageQueue::uniqueId ( )

Returns a unique message id within this message queue.

Returns
Unique message id

Field Documentation

◆ idCounter_

Id Ocean::Network::MessageQueue::idCounter_ = invalidId()
protected

Message id counter.

◆ lock_

Lock Ocean::Network::MessageQueue::lock_
protected

Map lock.

◆ messageMap_

MessageMap Ocean::Network::MessageQueue::messageMap_
protected

Map holding all messages.

◆ oldMessageAge_

unsigned int Ocean::Network::MessageQueue::oldMessageAge_ = 500u
protected

Definition of an message age of old and therefore unused messages.


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