Skip to content

Instantly share code, notes, and snippets.

@adurpas
Created December 10, 2012 14:32
Show Gist options
  • Save adurpas/4250877 to your computer and use it in GitHub Desktop.
Save adurpas/4250877 to your computer and use it in GitHub Desktop.
#include <Publisher.hpp>
#include <MessageDistributionSystem.hpp>
#include <osapi/Log.hpp>
static const int MAX_QUEUE_SIZE=10;
Publisher::Publisher()
: running_(true),
mq_(MAX_QUEUE_SIZE),
timer_(osapi::createNewTimer(&mq_, ID_TIME_OUT))
{
OSAPI_LOG_DBG("Creating publisher with associated timer...");
}
Publisher::~Publisher()
{
delete timer_;
mq_.send(ID_TERMINATE);
}
void Publisher::handleMsgIdTimeOut()
{
OSAPI_LOG_DBG("Got timeout, publishing message and rearming...");
/************************************************/
/************************************************/
/* Write the necessary code to publish an event */
/************************************************/
/************************************************/
// New code starts
//Create a new message
HelloMsg* msg = new HelloMsg;
//Write a text in the message
msg->data_ = "Hello World!";
//Send the message
MessageDistributionSystem::getInstance().notify("RSS_Feed", msg);
// New code ends
timer_->reArm(); // Timeout in TIMEOUT msec
}
void Publisher::handleMsg(unsigned long id, osapi::Message* msg)
{
switch(id)
{
case ID_TIME_OUT:
handleMsgIdTimeOut();
break;
case ID_TERMINATE:
running_ = false;
break;
default:
OSAPI_LOG_DBG("Arg, got unknown event...");
break;
}
}
void Publisher::run()
{
OSAPI_LOG_DBG("Preparing for loop, arming timer...");
timer_->arm(TIMEOUT);
while(running_)
{
unsigned long id;
osapi::Message* m = mq_.receive(id);
handleMsg(id, m);
delete m;
}
OSAPI_LOG_DBG("Ensuring timers are stopped...");
timer_->disArm();
OSAPI_LOG_DBG("Thread terminating...");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment