00001 /* 00002 * EventSwitch.cpp 00003 * 00004 * Created on: 05.03.2009 00005 * Author: morgenro 00006 */ 00007 00008 #include "core/EventSwitch.h" 00009 00010 #include <ibrcommon/thread/MutexLock.h> 00011 #include "core/GlobalEvent.h" 00012 #include <stdexcept> 00013 #include <iostream> 00014 using namespace std; 00015 00016 namespace dtn 00017 { 00018 namespace core 00019 { 00020 EventSwitch::EventSwitch() 00021 : _running(false) 00022 { 00023 } 00024 00025 EventSwitch::~EventSwitch() 00026 { 00027 componentDown(); 00028 } 00029 00030 void EventSwitch::componentUp() 00031 { 00032 } 00033 00034 void EventSwitch::componentDown() 00035 { 00036 try { 00037 while (!_queue.empty()) 00038 { 00039 delete _queue.frontpop(); 00040 } 00041 } catch (ibrcommon::Exception) { 00042 00043 } 00044 00045 _running = false; 00046 _queue.unblock(); 00047 } 00048 00049 void EventSwitch::loop() 00050 { 00051 _running = true; 00052 00053 try { 00054 while (_running) 00055 { 00056 Event *evt = _queue.blockingpop(); 00057 dtn::core::GlobalEvent *global = dynamic_cast<dtn::core::GlobalEvent*>(evt); 00058 00059 { 00060 ibrcommon::MutexLock reglock(_receiverlock); 00061 00062 // forward to debugger 00063 _debugger.raiseEvent(evt); 00064 00065 try { 00066 // get the list for this event 00067 const list<EventReceiver*> receivers = getReceivers(evt->getName()); 00068 00069 for (list<EventReceiver*>::const_iterator iter = receivers.begin(); iter != receivers.end(); iter++) 00070 { 00071 try { 00072 (*iter)->raiseEvent(evt); 00073 } catch (...) { 00074 // An error occurred during event raising 00075 } 00076 } 00077 } catch (NoReceiverFoundException ex) { 00078 // No receiver available! 00079 } 00080 } 00081 00082 if (global != NULL) 00083 { 00084 if (global->getAction() == dtn::core::GlobalEvent::GLOBAL_SHUTDOWN) 00085 { 00086 _running = false; 00087 } 00088 } 00089 00090 delete evt; 00091 } 00092 } catch (ibrcommon::Exception) { 00093 _running = false; 00094 } 00095 } 00096 00097 const list<EventReceiver*>& EventSwitch::getReceivers(string eventName) const 00098 { 00099 map<string,list<EventReceiver*> >::const_iterator iter = _list.find(eventName); 00100 00101 if (iter == _list.end()) 00102 { 00103 throw NoReceiverFoundException(); 00104 } 00105 00106 return iter->second; 00107 } 00108 00109 void EventSwitch::registerEventReceiver(string eventName, EventReceiver *receiver) 00110 { 00111 // get the list for this event 00112 EventSwitch &s = EventSwitch::getInstance(); 00113 ibrcommon::MutexLock l(s._receiverlock); 00114 s._list[eventName].push_back(receiver); 00115 } 00116 00117 void EventSwitch::unregisterEventReceiver(string eventName, EventReceiver *receiver) 00118 { 00119 // get the list for this event 00120 EventSwitch &s = EventSwitch::getInstance(); 00121 ibrcommon::MutexLock l(s._receiverlock); 00122 s._list[eventName].remove( receiver ); 00123 } 00124 00125 void EventSwitch::raiseEvent(Event *evt) 00126 { 00127 EventSwitch &s = EventSwitch::getInstance(); 00128 s._queue.push(evt); 00129 } 00130 00131 EventSwitch& EventSwitch::getInstance() 00132 { 00133 static EventSwitch instance; 00134 return instance; 00135 } 00136 } 00137 }
1.6.3