IBR-DTNSuite
0.8
|
00001 /* 00002 * EventSwitch.h 00003 * 00004 * Created on: 05.03.2009 00005 * Author: morgenro 00006 */ 00007 00008 #ifndef EVENTSWITCH_H_ 00009 #define EVENTSWITCH_H_ 00010 00011 #include "core/Event.h" 00012 #include "core/EventReceiver.h" 00013 #include "Component.h" 00014 #include <ibrcommon/Exceptions.h> 00015 #include <ibrcommon/thread/Mutex.h> 00016 #include <ibrcommon/thread/Conditional.h> 00017 #include "core/EventDebugger.h" 00018 00019 #include <list> 00020 #include <map> 00021 00022 namespace dtn 00023 { 00024 namespace core 00025 { 00026 class EventException : public ibrcommon::Exception 00027 { 00028 00029 }; 00030 00031 class NoSuchEventException : public EventException 00032 { 00033 00034 }; 00035 00036 class NoReceiverFoundException : public ibrcommon::Exception 00037 { 00038 00039 }; 00040 00041 class EventSwitch : public dtn::daemon::IntegratedComponent 00042 { 00043 private: 00044 EventSwitch(); 00045 virtual ~EventSwitch(); 00046 00047 ibrcommon::Mutex _receiverlock; 00048 std::map<std::string, std::list<EventReceiver*> > _list; 00049 00050 bool _running; 00051 00052 // create event debugger 00053 EventDebugger _debugger; 00054 00055 const std::list<EventReceiver*>& getReceivers(std::string eventName) const; 00056 00060 virtual const std::string getName() const; 00061 00062 class Task 00063 { 00064 public: 00065 Task(); 00066 Task(EventReceiver *er, dtn::core::Event *evt); 00067 ~Task(); 00068 00069 EventReceiver *receiver; 00070 dtn::core::Event *event; 00071 }; 00072 00073 class Worker : public ibrcommon::JoinableThread 00074 { 00075 public: 00076 Worker(EventSwitch &sw); 00077 ~Worker(); 00078 00079 protected: 00080 void run(); 00081 virtual void __cancellation(); 00082 00083 private: 00084 EventSwitch &_switch; 00085 bool _running; 00086 }; 00087 00088 ibrcommon::Conditional _queue_cond; 00089 std::list<Task*> _queue; 00090 std::list<Task*> _prio_queue; 00091 std::list<Task*> _low_queue; 00092 00093 ibrcommon::Conditional _active_cond; 00094 size_t _active_worker; 00095 bool _pause; 00096 00097 void process(); 00098 00099 void pause(); 00100 void unpause(); 00101 00102 void unregister(std::string eventName, EventReceiver *receiver); 00103 00104 protected: 00105 virtual void componentUp(); 00106 virtual void componentDown(); 00107 00108 friend class Event; 00109 friend class EventReceiver; 00110 00111 static void registerEventReceiver(string eventName, EventReceiver *receiver); 00112 static void unregisterEventReceiver(string eventName, EventReceiver *receiver); 00113 static void raiseEvent(Event *evt); 00114 00115 public: 00116 static EventSwitch& getInstance(); 00117 void loop(size_t threads = 0); 00118 void clear(); 00119 00120 friend class Worker; 00121 }; 00122 } 00123 } 00124 00125 #endif /* EVENTSWITCH_H_ */