libassa  3.5.1
Reactor.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Reactor.h
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Created: 05/25/1999
13 //------------------------------------------------------------------------------
14 #ifndef REACTOR_H
15 #define REACTOR_H
16 
17 #include <sys/time.h> // select(2)
18 #include <map>
19 
20 #if !defined(WIN32)
21 # include <sys/resource.h> // getrlimit(2)
22 #endif
23 
24 #include "assa/EventHandler.h"
25 #include "assa/Singleton.h"
26 #include "assa/MaskSet.h"
27 #include "assa/TimerQueue.h"
28 #include "assa/TimerCountdown.h"
29 
30 namespace ASSA {
31 
57 class Reactor
58 {
59 public:
61  Reactor ();
62 
64  ~Reactor();
65 
77  const TimeVal& tv_,
78  const std::string& name_ = "<unknown>");
79 
88  bool registerIOHandler (EventHandler* eh_,
89  handler_t fd_,
90  EventType et_ = RWE_EVENTS);
91 
101  bool removeHandler (EventHandler* eh_, EventType et_ = ALL_EVENTS);
102 
107  bool removeTimerHandler (TimerId id_);
108 
114  bool removeIOHandler (handler_t fd_);
115 
117  void waitForEvents (void);
118 
127  void waitForEvents (TimeVal* tv_);
128 
137  void stopReactor (void);
138 
147  void deactivate (void);
148 
149 private:
150  Reactor (const Reactor&);
151  Reactor& operator= (const Reactor&);
152 
153 private:
154  typedef std::map<u_int, EventHandler*> Fd2Eh_Map_Type;
155  typedef Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter;
156 
157 private:
159  void adjust_maxfdp1 (handler_t fd_);
160 
162  bool handleError (void);
163 
168  bool dispatch (int minimum_);
169 
171  int isAnyReady (void);
172 
177  bool checkFDs (void);
178 
182  void dispatchHandler ( FdSet& mask_,
183  Fd2Eh_Map_Type& fdSet_,
184  EH_IO_Callback callback_);
185 
192  void calculateTimeout (TimeVal*& howlong_, TimeVal* maxwait_);
193 
194 private:
201 
207 
209  bool m_active;
210 
213 
216 
219 
222 
225 
228 };
229 
230 //-----------------------------------------------------------------------------
231 // Inline functions
232 //-----------------------------------------------------------------------------
233 
234 inline void Reactor::deactivate (void) { m_active = false; }
235 
236 } // end namespace ASSA
237 
238 
239 
240 #endif /* REACTOR_H */
ASSA::TimerId
unsigned long TimerId
Definition: EventHandler.h:27
ASSA::TimeVal
Definition: TimeVal.h:30
ASSA::Reactor::m_maxfd_plus1
handler_t m_maxfd_plus1
Max file descriptor number (in all sets) plus 1.
Definition: Reactor.h:206
ASSA::Reactor::m_fd_setsize
int m_fd_setsize
Max number of open files per process.
Definition: Reactor.h:200
ASSA::Reactor::removeIOHandler
bool removeIOHandler(handler_t fd_)
Remove IO Event handler from reactor.
Definition: Reactor.cpp:247
ASSA::Reactor::Fd2Eh_Map_Type
std::map< u_int, EventHandler * > Fd2Eh_Map_Type
no cloning
Definition: Reactor.h:154
TimerQueue.h
ASSA::ALL_EVENTS
@ ALL_EVENTS
Mask that includes all events.
Definition: EventHandler.h:47
ASSA::Reactor::adjust_maxfdp1
void adjust_maxfdp1(handler_t fd_)
Adjust maxfdp1 in a portable way (win32 ignores maxfd alltogether).
Definition: Reactor.cpp:701
ASSA::EventType
EventType
Definition: EventHandler.h:34
ASSA::Reactor::waitForEvents
void waitForEvents(void)
Main waiting loop that blocks indefinitely processing events.
Definition: Reactor.cpp:470
TimerCountdown.h
ASSA::MaskSet
Definition: MaskSet.h:24
ASSA::Reactor::registerTimerHandler
TimerId registerTimerHandler(EventHandler *eh_, const TimeVal &tv_, const std::string &name_="<unknown>")
Register Timer Event handler with Reactor.
Definition: Reactor.cpp:67
ASSA::EH_IO_Callback
int(EventHandler::* EH_IO_Callback)(int)
Definition: EventHandler.h:236
ASSA::Reactor::operator=
Reactor & operator=(const Reactor &)
no cloning
ASSA::Reactor::Reactor
Reactor()
Constructor.
Definition: Reactor.cpp:24
handler_t
int handler_t
Definition: Logger_Impl.h:82
ASSA::Reactor::m_exceptSet
Fd2Eh_Map_Type m_exceptSet
Event handlers awaiting on EXCEPT_EVENT.
Definition: Reactor.h:218
ASSA::Reactor::stopReactor
void stopReactor(void)
Stop Reactor's activity.
Definition: Reactor.cpp:667
ASSA::Reactor
Definition: Reactor.h:57
ASSA::EventHandler
EventHandler class.
Definition: EventHandler.h:102
ASSA::Reactor::dispatch
bool dispatch(int minimum_)
Notify all EventHandlers registered on respecful events occured.
Definition: Reactor.cpp:626
EventHandler.h
ASSA::Reactor::dispatchHandler
void dispatchHandler(FdSet &mask_, Fd2Eh_Map_Type &fdSet_, EH_IO_Callback callback_)
Call handler's callback and, if callback returns negative value, remove it from the Reactor.
Definition: Reactor.cpp:568
ASSA::Reactor::m_readySet
MaskSet m_readySet
Handlers that are ready for processing.
Definition: Reactor.h:224
ASSA::RWE_EVENTS
@ RWE_EVENTS
READ_EVENT | WRITE_EVENT | EXCEPT_EVENT
Definition: EventHandler.h:46
ASSA::Reactor::m_writeSet
Fd2Eh_Map_Type m_writeSet
Event handlers awaiting on WRITE_EVENT.
Definition: Reactor.h:215
ASSA::Reactor::Fd2Eh_Map_Iter
Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter
Definition: Reactor.h:155
ASSA::FdSet
Class FdSet.
Definition: FdSet.h:51
Singleton.h
ASSA::Reactor::m_active
bool m_active
Flag that indicates whether Reactor is active or had been stopped.
Definition: Reactor.h:209
ASSA::Reactor::calculateTimeout
void calculateTimeout(TimeVal *&howlong_, TimeVal *maxwait_)
Calculate closest timeout.
Definition: Reactor.cpp:421
MaskSet.h
ASSA::Reactor::removeHandler
bool removeHandler(EventHandler *eh_, EventType et_=ALL_EVENTS)
Remove Event handler from reactor for either all I/O events or timeout event or both.
Definition: Reactor.cpp:173
ASSA::Reactor::removeTimerHandler
bool removeTimerHandler(TimerId id_)
Remove Timer event from the queue.
Definition: Reactor.cpp:152
ASSA::Reactor::handleError
bool handleError(void)
Handle error in select(2) loop appropriately.
Definition: Reactor.cpp:341
ASSA::Reactor::registerIOHandler
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition: Reactor.cpp:93
ASSA::Reactor::m_waitSet
MaskSet m_waitSet
Handlers to wait for event on.
Definition: Reactor.h:221
ASSA::Reactor::checkFDs
bool checkFDs(void)
Check mask for bad file descriptors.
Definition: Reactor.cpp:317
ASSA::Reactor::m_tqueue
TimerQueue m_tqueue
The queue of Timers.
Definition: Reactor.h:227
ASSA::Reactor::m_readSet
Fd2Eh_Map_Type m_readSet
Event handlers awaiting on READ_EVENT.
Definition: Reactor.h:212
ASSA::Reactor::isAnyReady
int isAnyReady(void)
Return number of file descriptors ready accross all sets.
Definition: Reactor.cpp:404
ASSA
Definition: Acceptor.h:40
ASSA::Reactor::~Reactor
~Reactor()
Destructor.
Definition: Reactor.cpp:55
ASSA::TimerQueue
Definition: TimerQueue.h:35
ASSA::Reactor::deactivate
void deactivate(void)
Deactivate Reactor.
Definition: Reactor.h:234