Skip to content

Instantly share code, notes, and snippets.

@adurpas
Created December 10, 2012 14:29
Show Gist options
  • Save adurpas/4250861 to your computer and use it in GitHub Desktop.
Save adurpas/4250861 to your computer and use it in GitHub Desktop.
#include <MessageDistributionSystem.hpp>
#include <iostream>
using namespace std;
void MessageDistributionSystem::subscribe(const std::string& msgId,
osapi::MsgQueue* mq,
unsigned long id)
{
/* Something missing */
InsertResult ir = sm_.insert(std::make_pair(msgId, SubscriberIdContainer()));
SubscriberIdContainer& sl = ir.first->second;
details::SubscriberId s(mq, id);
SubscriberIdContainer::iterator iter = find(sl.begin(), sl.end(), s);
if(iter == sl.end())
sl.push_back(s);
}
void MessageDistributionSystem::unSubscribe(const std::string& msgId,
osapi::MsgQueue* mq,
unsigned long id)
{
// Find SubscriberIdContainer via via iterator
// Where else do I need to find this container?
// The struct std::pair (the element in std::map<>) contains two vars one
// named first and one named second. (See funktionen std::make_pair() - JFGI)
// If found. How do you know that an element in a container was not found?
// Create a details::SubscriberId and use that to find the desired subscriber.
// When found it must be erased
// New code starts
// Lock the scope
osapi::ScopedLock lock(m_);
//Find via iterator the list for the global messageID
SubscriberIdMap::iterator iterMessage = sm_.find(msgId);
// If found. How do you know that an element in a container was not found?
if(iterMessage != sm_.end())
{
cout << "A msgId was found in the container" << endl;
// Get the subscriberID list
SubscriberIdContainer& sl = iterMessage->second;
// Create a details::SubscriberId and use that to find the desired subscriber.
// A new temp subscriber is made with the MsgQueue and ID for the unsubsubscriber
details::SubscriberId s(mq, id);
//Seach the subscriberId list for the the right subscriber
SubscriberIdContainer::iterator iter = std::find(sl.begin(), sl.end(), s);
if(iter != sl.end())
{
sl.erase(iter); //Delete the subscriber
cout << "Subscriber deleted!" << endl;
}
}
else
{
// Not found
}
// New code ends
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment