IBR-DTNSuite  0.8
daemon/src/Notifier.cpp
Go to the documentation of this file.
00001 /*
00002  * Notifier.cpp
00003  *
00004  *  Created on: 11.12.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "Notifier.h"
00009 #include "core/NodeEvent.h"
00010 #include <stdlib.h>
00011 
00012 #include <sstream>
00013 
00014 namespace dtn
00015 {
00016         namespace daemon
00017         {
00018                 using namespace dtn::core;
00019 
00020                 Notifier::Notifier(std::string cmd) : _cmd(cmd)
00021                 {
00022                 }
00023 
00024                 Notifier::~Notifier()
00025                 {
00026                 }
00027 
00028                 void Notifier::componentUp()
00029                 {
00030                         bindEvent(NodeEvent::className);
00031                 }
00032 
00033                 void Notifier::componentDown()
00034                 {
00035                         unbindEvent(NodeEvent::className);
00036                 }
00037 
00038                 void Notifier::raiseEvent(const dtn::core::Event *evt)
00039                 {
00040                         const NodeEvent *node = dynamic_cast<const NodeEvent*>(evt);
00041 
00042                         if (node != NULL)
00043                         {
00044                                 std::stringstream msg;
00045 
00046                                 switch (node->getAction())
00047                                 {
00048                                 case NODE_AVAILABLE:
00049                                         msg << "Node is available: " << node->getNode().toString();
00050                                         notify("IBR-DTN", msg.str());
00051                                         break;
00052 
00053                                 case NODE_UNAVAILABLE:
00054                                         msg << "Node is unavailable: " << node->getNode().toString();
00055                                         notify("IBR-DTN", msg.str());
00056                                         break;
00057                                 default:
00058                                         break;
00059                                 }
00060                         }
00061                 }
00062 
00063                 void Notifier::notify(std::string title, std::string msg)
00064                 {
00065                         std::stringstream notifycmd;
00066                         notifycmd << _cmd;
00067                         notifycmd << " \"" << title << "\" \"" << msg << "\"";
00068 
00069                         system(notifycmd.str().c_str());
00070                 }
00071 
00072                 const std::string Notifier::getName() const
00073                 {
00074                         return "Notifier";
00075                 }
00076         }
00077 }