123 lines
4.0 KiB
C++
123 lines
4.0 KiB
C++
/*
|
|
* File: MulticastMessageQueue.cc
|
|
* Author: jgaebler
|
|
*
|
|
* Created on April 13, 2010, 6:14 PM
|
|
*/
|
|
|
|
#include "MulticastMessageQueue.h"
|
|
#include "mt/msg/MulticastMessage.h"
|
|
#include <algorithm>
|
|
|
|
namespace ubeeme {
|
|
namespace moversight {
|
|
|
|
/**
|
|
* @brief Clears the queue.
|
|
*/
|
|
void
|
|
MulticastMessageQueue::clear() {
|
|
|
|
for (auto m = queue.begin(); m != queue.end();) {
|
|
delete *m;
|
|
m = queue.erase(m);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Removes a node from the queue, if it exists
|
|
* @param mref Identifies the node to remove
|
|
*/
|
|
void
|
|
MulticastMessageQueue::remove(const MessageReference & mref) {
|
|
|
|
auto it = std::find_if(queue.begin(), queue.end(), MulticastMessageFunctor(mref));
|
|
|
|
if (it != queue.end()) {
|
|
delete *it;
|
|
queue.erase(it);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Sorts the message queue according the logicalDelvieryTime, stored within each message.
|
|
* @see MulticastMessageLogicalDeliveryTimeComparatorLessThan
|
|
*/
|
|
void
|
|
MulticastMessageQueue::sortByLogicalDeliveryTime() {
|
|
std::sort(queue.begin(), queue.end(), MulticastMessageLogicalDeliveryTimeComparatorLessThan);
|
|
}
|
|
|
|
/**
|
|
* @brief Returns the stored multicast message, if it stored within the queue, else otherwise.
|
|
* @param mRef the message reference identifing the search message
|
|
* @return the stored message, if it stored within the queue, NULL otherwise
|
|
*/
|
|
MulticastMessage*
|
|
MulticastMessageQueue::find(const MessageReference & mref) {
|
|
auto it = std::find_if(queue.begin(), queue.end(), MulticastMessageFunctor(mref));
|
|
|
|
if (it == queue.end()) {
|
|
return NULL;
|
|
}
|
|
|
|
return *it;
|
|
}
|
|
|
|
/**
|
|
* @brief Adds a copy of the given multicast message to the queue.
|
|
* @param pdu the message to add
|
|
*/
|
|
void
|
|
MulticastMessageQueue::add(const MulticastMessage * pdu) {
|
|
MulticastMessage* elem = find(pdu->getMessageReference());
|
|
|
|
if (elem == NULL) {
|
|
MessageQueue::add(pdu->dup());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Prints the content of the queue to the standard output. Only used for debug purpose.
|
|
*/
|
|
void
|
|
MulticastMessageQueue::printQueue() {
|
|
|
|
auto it = queue.begin();
|
|
std::stringstream stream;
|
|
|
|
while (it != queue.end()) {
|
|
stream << " message_reference: " << (*it)->getMessageReference()
|
|
<< " source: " << (*it)->getSourceID()
|
|
<< " last_hop: " << (*it)->getLastHop()
|
|
<< " sequ: " << (*it)->getSequ() << " lt: " << (*it)->getLT() << " | ";
|
|
|
|
it++;
|
|
}
|
|
|
|
stream << "\n";
|
|
|
|
#if OMNETPP
|
|
std::cout << stream.str();
|
|
#else
|
|
MOV_DEBUG << stream.str().c_str();
|
|
#endif
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief Compares the logicalDeliveryTime of the given messages and returns true, if the delivery time of a less than the one of b.
|
|
* @param a The first message.
|
|
* @param b The second message.
|
|
* @return True, if the delivery time of the message a less than the corresponding time of b, false otherwise.
|
|
* @throws NullPointerException If one of the given attributes NULL.
|
|
*/
|
|
bool
|
|
MulticastMessageLogicalDeliveryTimeComparatorLessThan(const MulticastMessage * a, const MulticastMessage * b) {
|
|
if (a == NULL) throw NullPointerException("MulticastMessageLogicalDeliveryTimeComparatorLessThan - the first argument given is NULL");
|
|
if (b == NULL) throw NullPointerException("MulticastMessageLogicalDeliveryTimeComparatorLessThan - the second argument given is NULL");
|
|
return (a->getLogicalDeliveryTime() < b->getLogicalDeliveryTime());
|
|
}
|
|
}
|
|
}
|