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&);
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 */
An abstract interface for handling I/O events, timers, and such.
int handler_t
Definition: Logger_Impl.h:82
Bundles file descriptor mask sets to be used with select().
Singleton template class allows to turn any new or existing class T into Singleton Pattern.
TimerCountdown class keep the track of the elapsed time.
TimerQueue class represents the queue of Timers that application can install for Reactor to dispatch.
EventHandler class.
Definition: EventHandler.h:103
Class FdSet.
Definition: FdSet.h:52
void calculateTimeout(TimeVal *&howlong_, TimeVal *maxwait_)
Calculate closest timeout.
Definition: Reactor.cpp:421
bool registerIOHandler(EventHandler *eh_, handler_t fd_, EventType et_=RWE_EVENTS)
Register I/O Event handler with Reactor.
Definition: Reactor.cpp:93
Reactor(const Reactor &)
TimerQueue m_tqueue
The queue of Timers.
Definition: Reactor.h:227
MaskSet m_waitSet
Handlers to wait for event on.
Definition: Reactor.h:221
Fd2Eh_Map_Type m_writeSet
Event handlers awaiting on WRITE_EVENT.
Definition: Reactor.h:215
Reactor()
Constructor.
Definition: Reactor.cpp:24
Fd2Eh_Map_Type::iterator Fd2Eh_Map_Iter
Definition: Reactor.h:155
MaskSet m_readySet
Handlers that are ready for processing.
Definition: Reactor.h:224
void waitForEvents(void)
Main waiting loop that blocks indefinitely processing events.
Definition: Reactor.cpp:470
handler_t m_maxfd_plus1
Max file descriptor number (in all sets) plus 1.
Definition: Reactor.h:206
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
int m_fd_setsize
Max number of open files per process.
Definition: Reactor.h:200
bool handleError(void)
Handle error in select(2) loop appropriately.
Definition: Reactor.cpp:341
std::map< u_int, EventHandler * > Fd2Eh_Map_Type
no cloning
Definition: Reactor.h:154
void deactivate(void)
Deactivate Reactor.
Definition: Reactor.h:234
bool dispatch(int minimum_)
Notify all EventHandlers registered on respecful events occured.
Definition: Reactor.cpp:626
Fd2Eh_Map_Type m_readSet
Event handlers awaiting on READ_EVENT.
Definition: Reactor.h:212
bool m_active
Flag that indicates whether Reactor is active or had been stopped.
Definition: Reactor.h:209
Reactor & operator=(const Reactor &)
no cloning
void stopReactor(void)
Stop Reactor's activity.
Definition: Reactor.cpp:667
TimerId registerTimerHandler(EventHandler *eh_, const TimeVal &tv_, const std::string &name_="<unknown>")
Register Timer Event handler with Reactor.
Definition: Reactor.cpp:67
bool checkFDs(void)
Check mask for bad file descriptors.
Definition: Reactor.cpp:317
int isAnyReady(void)
Return number of file descriptors ready accross all sets.
Definition: Reactor.cpp:404
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
~Reactor()
Destructor.
Definition: Reactor.cpp:55
bool removeTimerHandler(TimerId id_)
Remove Timer event from the queue.
Definition: Reactor.cpp:152
void adjust_maxfdp1(handler_t fd_)
Adjust maxfdp1 in a portable way (win32 ignores maxfd alltogether).
Definition: Reactor.cpp:701
bool removeIOHandler(handler_t fd_)
Remove IO Event handler from reactor.
Definition: Reactor.cpp:247
Fd2Eh_Map_Type m_exceptSet
Event handlers awaiting on EXCEPT_EVENT.
Definition: Reactor.h:218
Definition: Acceptor.h:40
EventType
EventType defines events types that Reactor understands.
Definition: EventHandler.h:35
@ ALL_EVENTS
Mask that includes all events.
Definition: EventHandler.h:47
@ RWE_EVENTS
READ_EVENT | WRITE_EVENT | EXCEPT_EVENT
Definition: EventHandler.h:46
unsigned long TimerId
Timer Id is used in handle_timeout() calls.
Definition: EventHandler.h:27
int(EventHandler::* EH_IO_Callback)(int)
A type for the pointer to I/O-related callback member function of class EventHandler.
Definition: EventHandler.h:236