IBR-DTNSuite  0.8
daemon/src/api/EventConnection.cpp
Go to the documentation of this file.
00001 /*
00002  * EventConnection.cpp
00003  *
00004  *  Created on: 13.10.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "EventConnection.h"
00009 
00010 #include "core/NodeEvent.h"
00011 #include "core/GlobalEvent.h"
00012 #include "core/CustodyEvent.h"
00013 #include "routing/QueueBundleEvent.h"
00014 #include "net/TransferAbortedEvent.h"
00015 #include "net/TransferCompletedEvent.h"
00016 #include "net/ConnectionEvent.h"
00017 
00018 #include <ibrdtn/utils/Utils.h>
00019 
00020 namespace dtn
00021 {
00022         namespace api
00023         {
00024                 EventConnection::EventConnection(ClientHandler &client, ibrcommon::tcpstream &stream)
00025                  : ProtocolHandler(client, stream), _running(true)
00026                 {
00027                 }
00028 
00029                 EventConnection::~EventConnection()
00030                 {
00031                 }
00032 
00033                 void EventConnection::raiseEvent(const dtn::core::Event *evt)
00034                 {
00035                         ibrcommon::MutexLock l(_mutex);
00036                         if (!_running) return;
00037 
00038                         try {
00039                                 const dtn::core::NodeEvent &node = dynamic_cast<const dtn::core::NodeEvent&>(*evt);
00040 
00041                                 // ignore NODE_INFO_UPDATED
00042                                 if (node.getAction() == dtn::core::NODE_INFO_UPDATED) return;
00043 
00044                                 // start with the event tag
00045                                 _stream << "Event: " << node.getName() << std::endl;
00046                                 _stream << "Action: ";
00047 
00048                                 switch (node.getAction())
00049                                 {
00050                                 case dtn::core::NODE_AVAILABLE:
00051                                         _stream << "available";
00052                                         break;
00053                                 case dtn::core::NODE_UNAVAILABLE:
00054                                         _stream << "unavailable";
00055                                         break;
00056                                 default:
00057                                         break;
00058                                 }
00059 
00060                                 _stream << std::endl;
00061 
00062                                 // write the node eid
00063                                 _stream << "EID: " << node.getNode().getEID().getString() << std::endl;
00064 
00065                                 // close the event
00066                                 _stream << std::endl;
00067                         } catch (const std::bad_cast&) { };
00068 
00069                         try {
00070                                 const dtn::core::GlobalEvent &global = dynamic_cast<const dtn::core::GlobalEvent&>(*evt);
00071 
00072                                 // start with the event tag
00073                                 _stream << "Event: " << global.getName() << std::endl;
00074                                 _stream << "Action: ";
00075 
00076                                 switch (global.getAction())
00077                                 {
00078                                 case dtn::core::GlobalEvent::GLOBAL_BUSY:
00079                                         _stream << "busy";
00080                                         break;
00081                                 case dtn::core::GlobalEvent::GLOBAL_IDLE:
00082                                         _stream << "idle";
00083                                         break;
00084                                 case dtn::core::GlobalEvent::GLOBAL_POWERSAVE:
00085                                         _stream << "powersave";
00086                                         break;
00087                                 case dtn::core::GlobalEvent::GLOBAL_RELOAD:
00088                                         _stream << "reload";
00089                                         break;
00090                                 case dtn::core::GlobalEvent::GLOBAL_SHUTDOWN:
00091                                         _stream << "shutdown";
00092                                         break;
00093                                 case dtn::core::GlobalEvent::GLOBAL_SUSPEND:
00094                                         _stream << "suspend";
00095                                         break;
00096                                 default:
00097                                         break;
00098                                 }
00099                                 _stream << std::endl;
00100 
00101                                 // close the event
00102                                 _stream << std::endl;
00103                         } catch (const std::bad_cast&) { };
00104 
00105                         try {
00106                                 const dtn::core::CustodyEvent &custody = dynamic_cast<const dtn::core::CustodyEvent&>(*evt);
00107 
00108                         } catch (const std::bad_cast&) { };
00109 
00110                         try {
00111                                 const dtn::net::TransferAbortedEvent &aborted = dynamic_cast<const dtn::net::TransferAbortedEvent&>(*evt);
00112 
00113                         } catch (const std::bad_cast&) { };
00114 
00115                         try {
00116                                 const dtn::net::TransferCompletedEvent &completed = dynamic_cast<const dtn::net::TransferCompletedEvent&>(*evt);
00117 
00118                         } catch (const std::bad_cast&) { };
00119 
00120                         try {
00121                                 const dtn::net::ConnectionEvent &connection = dynamic_cast<const dtn::net::ConnectionEvent&>(*evt);
00122 
00123                                 // start with the event tag
00124                                 _stream << "Event: " << connection.getName() << std::endl;
00125                                 _stream << "Action: ";
00126 
00127                                 switch (connection.state)
00128                                 {
00129                                 case dtn::net::ConnectionEvent::CONNECTION_UP:
00130                                         _stream << "up";
00131                                         break;
00132                                 case dtn::net::ConnectionEvent::CONNECTION_DOWN:
00133                                         _stream << "down";
00134                                         break;
00135                                 case dtn::net::ConnectionEvent::CONNECTION_SETUP:
00136                                         _stream << "setup";
00137                                         break;
00138                                 case dtn::net::ConnectionEvent::CONNECTION_TIMEOUT:
00139                                         _stream << "timeout";
00140                                         break;
00141                                 default:
00142                                         break;
00143                                 }
00144                                 _stream << std::endl;
00145 
00146                                 // write the peer eid
00147                                 _stream << "Peer: " << connection.peer.getString() << std::endl;
00148 
00149                                 // close the event
00150                                 _stream << std::endl;
00151                         } catch (const std::bad_cast&) { };
00152 
00153                         try {
00154                                 const dtn::routing::QueueBundleEvent &queued = dynamic_cast<const dtn::routing::QueueBundleEvent&>(*evt);
00155 
00156                                 // start with the event tag
00157                                 _stream << "Event: " << queued.getName() << std::endl;
00158 
00159                                 // write the bundle data
00160                                 _stream << "Source: " << queued.bundle.source.getString() << std::endl;
00161                                 _stream << "Timestamp: " << queued.bundle.timestamp << std::endl;
00162                                 _stream << "Sequencenumber: " << queued.bundle.sequencenumber << std::endl;
00163                                 _stream << "Lifetime: " << queued.bundle.lifetime << std::endl;
00164                                 _stream << "Procflags: " << queued.bundle.procflags << std::endl;
00165 
00166                                 // write the destination eid
00167                                 _stream << "Destination: " << queued.bundle.destination.getString() << std::endl;
00168 
00169                                 if (queued.bundle.fragment)
00170                                 {
00171                                         // write fragmentation values
00172                                         _stream << "Appdatalength: " << queued.bundle.appdatalength << std::endl;
00173                                         _stream << "Fragmentoffset: " << queued.bundle.offset << std::endl;
00174                                 }
00175 
00176                                 // close the event
00177                                 _stream << std::endl;
00178                         } catch (const std::bad_cast&) { };
00179                 }
00180 
00181                 void EventConnection::run()
00182                 {
00183                         std::string buffer;
00184 
00185                         // announce protocol change
00186                         _stream << ClientHandler::API_STATUS_OK << " SWITCHED TO EVENT" << std::endl;
00187 
00188                         // run as long the stream is ok
00189                         while (_stream.good())
00190                         {
00191                                 getline(_stream, buffer);
00192 
00193                                 // search for '\r\n' and remove the '\r'
00194                                 std::string::reverse_iterator iter = buffer.rbegin();
00195                                 if ( (*iter) == '\r' ) buffer = buffer.substr(0, buffer.length() - 1);
00196 
00197                                 std::vector<std::string> cmd = dtn::utils::Utils::tokenize(" ", buffer);
00198                                 if (cmd.size() == 0) continue;
00199 
00200                                 // return to previous level
00201                                 if (cmd[0] == "exit") break;
00202                         }
00203 
00204                         ibrcommon::MutexLock l(_mutex);
00205                         _running = false;
00206                 }
00207 
00208                 void EventConnection::setup()
00209                 {
00210                         // bind to several events
00211                         bindEvent(dtn::core::NodeEvent::className);
00212                         bindEvent(dtn::core::GlobalEvent::className);
00213                         bindEvent(dtn::core::CustodyEvent::className);
00214                         bindEvent(dtn::net::TransferAbortedEvent::className);
00215                         bindEvent(dtn::net::TransferCompletedEvent::className);
00216                         bindEvent(dtn::net::ConnectionEvent::className);
00217                         bindEvent(dtn::routing::QueueBundleEvent::className);
00218                 }
00219 
00220                 void EventConnection::finally()
00221                 {
00222                         // unbind to events
00223                         unbindEvent(dtn::core::NodeEvent::className);
00224                         unbindEvent(dtn::core::GlobalEvent::className);
00225                         unbindEvent(dtn::core::CustodyEvent::className);
00226                         unbindEvent(dtn::net::TransferAbortedEvent::className);
00227                         unbindEvent(dtn::net::TransferCompletedEvent::className);
00228                         unbindEvent(dtn::net::ConnectionEvent::className);
00229                         unbindEvent(dtn::routing::QueueBundleEvent::className);
00230                 }
00231 
00232                 void EventConnection::__cancellation()
00233                 {
00234                 }
00235         } /* namespace api */
00236 } /* namespace dtn */