00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef REACTOR_H
00015 #define REACTOR_H
00016
00017 #include <sys/time.h>
00018 #include <map>
00019
00020 #if !defined(WIN32)
00021 # include <sys/resource.h>
00022 #endif
00023
00024 #include "assa/EventHandler.h"
00025 #include "assa/Singleton.h"
00026 #include "assa/MaskSet.h"
00027 #include "assa/TimerQueue.h"
00028 #include "assa/TimerCountdown.h"
00029
00030 namespace ASSA {
00031
00057 class Reactor
00058 {
00059 public:
00061 Reactor ();
00062
00064 ~Reactor();
00065
00076 TimerId registerTimerHandler (EventHandler* eh_,
00077 const TimeVal& tv_,
00078 const std::string& name_ = "<unknown>");
00079
00088 bool registerIOHandler (EventHandler* eh_,
00089 handler_t fd_,
00090 EventType et_ = RWE_EVENTS);
00091
00101 bool removeHandler (EventHandler* eh_, EventType et_ = ALL_EVENTS);
00102
00107 bool removeTimerHandler (TimerId id_);
00108
00114 bool removeIOHandler (handler_t fd_);
00115
00117 void waitForEvents (void);
00118
00127 void waitForEvents (TimeVal* tv_);
00128
00137 void stopReactor (void);
00138
00147 void deactivate (void);
00148
00149 private:
00150 Reactor (const Reactor&);
00151 Reactor& operator= (const Reactor&);
00152
00153 private:
00154 typedef std::map<u_int, EventHandler*> Fd2Eh_Map_Type;
00155 typedef Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter;
00156
00157 private:
00159 void adjust_maxfdp1 (handler_t fd_,
00160 handler_t rmax_,
00161 handler_t wmax_,
00162 handler_t emax_);
00163
00165 bool handleError (void);
00166
00171 bool dispatch (int minimum_);
00172
00174 int isAnyReady (void);
00175
00180 bool checkFDs (void);
00181
00185 void dispatchHandler ( FdSet& mask_,
00186 Fd2Eh_Map_Type& fdSet_,
00187 EH_IO_Callback callback_);
00188
00195 void calculateTimeout (TimeVal*& howlong_, TimeVal* maxwait_);
00196
00197 private:
00203 int m_fd_setsize;
00204
00209 handler_t m_maxfd_plus1;
00210
00212 bool m_active;
00213
00215 Fd2Eh_Map_Type m_readSet;
00216
00218 Fd2Eh_Map_Type m_writeSet;
00219
00221 Fd2Eh_Map_Type m_exceptSet;
00222
00224 MaskSet m_waitSet;
00225
00227 MaskSet m_readySet;
00228
00230 TimerQueue m_tqueue;
00231 };
00232
00233
00234
00235
00236
00237 inline void Reactor::deactivate (void) { m_active = false; }
00238
00239 }
00240
00241
00242
00243 #endif