Skip to content

Instantly share code, notes, and snippets.

@adurpas
Created December 10, 2012 14:31
Show Gist options
  • Save adurpas/4250873 to your computer and use it in GitHub Desktop.
Save adurpas/4250873 to your computer and use it in GitHub Desktop.
#include <osapi/Log.hpp>
#include <Subscriber.hpp>
#include <MessageDistributionSystem.hpp>
static const int MAX_QUEUE_SIZE=10;
Subscriber::Subscriber(unsigned int subId)
: mq_(MAX_QUEUE_SIZE), subId_(subId)
{
/*****************************************************/
/*****************************************************/
/* Write the necessary code to subscribe to an event */
/*****************************************************/
/*****************************************************/
// New code starts
// Subscribe to a message
MessageDistributionSystem::getInstance().subscribe("RSS_Feed", &mq_, ID_HELLO);
// New code ends
}
Subscriber::~Subscriber()
{
mq_.send(ID_TERMINATE);
}
void Subscriber::handleMsgIdHello(HelloMsg* hm)
{
OSAPI_LOG_DBG("S(" << subId_ << ") The hello message contained: '" << hm->data_ << "'");
/*******************************************************/
/*******************************************************/
/* Write the necessary code to unsubscribe to an event */
/*******************************************************/
/*******************************************************/
// New code starts
//Unsubscribe to a message
MessageDistributionSystem::getInstance().unSubscribe("RSS_Feed", &mq_, ID_TERMINATE);
// New code ends
}
void Subscriber::handleMsg(unsigned long id, osapi::Message* msg)
{
switch(id)
{
case ID_HELLO:
handleMsgIdHello(static_cast<HelloMsg*>(msg));
break;
case ID_TERMINATE:
OSAPI_LOG_DBG("S(" << subId_ << ") Got terminate signal");
break;
}
}
void Subscriber::run()
{
OSAPI_LOG_DBG("Preparing for loop...");
while(running_)
{
unsigned long id;
osapi::Message* m = mq_.receive(id);
handleMsg(id, m);
delete m;
}
OSAPI_LOG_DBG("Thread terminating...");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment