libassa  3.5.1
EventHandler.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // EventHandler.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 #ifndef _EventHandler_h
13 #define _EventHandler_h
14 
15 #include "assa/Assure.h"
16 
17 namespace ASSA {
18 
27 typedef unsigned long TimerId;
28 
35 {
36  READ_EVENT = 0x01,
39  WRITE_EVENT = 0x02,
42  EXCEPT_EVENT = 0x04,
44  TIMEOUT_EVENT = 0x10,
45  SIGNAL_EVENT = 0x20,
46  RWE_EVENTS = 0x07,
47  ALL_EVENTS = 0x37
48 };
49 
50 inline
52 {
53  return (e_ & READ_EVENT) == READ_EVENT;
54 }
55 
56 inline
58 {
59  return (e_ & WRITE_EVENT) == WRITE_EVENT;
60 }
61 
62 inline
64 {
65  return (e_ & EXCEPT_EVENT) == EXCEPT_EVENT;
66 }
67 
68 inline
70 {
71  return (e_ & TIMEOUT_EVENT) == TIMEOUT_EVENT;
72 }
73 
74 inline
76 {
77  return (e_ & SIGNAL_EVENT) == SIGNAL_EVENT;
78 }
79 
80 inline
82 {
83  return isReadEvent (e_) && isWriteEvent (e_) && isExceptEvent (e_);
84 }
85 
86 inline
88 {
89  return isReadEvent (e_) && isWriteEvent (e_) && isExceptEvent (e_) &&
90  isSignalEvent (e_) && isTimeoutEvent (e_) ;
91 }
92 
103 {
104 public:
106  EventHandler();
107 
109  virtual ~EventHandler () { /* no-op */ }
110 
115  virtual int handle_read (int fd);
116 
120  virtual int handle_write (int fd);
121 
125  virtual int handle_except (int fd);
126 
130  virtual int handle_timeout (TimerId tid);
131 
135  virtual int handle_signal (int signum_);
136 
143  virtual int handle_close (int fd);
144 
148  virtual void resetState (void);
149 
153  void set_id (const std::string& id_) { m_id = id_; }
154 
157  std::string get_id () const { return m_id; }
158 
159 protected:
160  std::string m_id;
161 };
162 
163 inline
165 EventHandler() : m_id ("EventHandler")
166 {
167  trace_with_mask("EventHandler::EventHandler",REACTTRACE);
168 }
169 
170 inline int
172 handle_read (int /* fd */)
173 {
174  trace_with_mask("EventHandler::handle_read",REACTTRACE);
175  return -1;
176 }
177 
178 inline int
180 handle_write (int /* fd */)
181 {
182  trace_with_mask("EventHandler::handle_write",REACTTRACE);
183  return -1;
184 }
185 
186 inline int
188 handle_except (int /* fd */)
189 {
190  trace_with_mask("EventHandler::handle_except",REACTTRACE);
191  return -1;
192 }
193 
194 inline int
196 handle_timeout (TimerId /* timer_id */)
197 {
198  trace_with_mask("EventHandler::handle_timeout",REACTTRACE);
199  return -1;
200 }
201 
202 inline int
204 handle_signal (int /* signum_ */)
205 {
206  trace_with_mask("EventHandler::handle_signal",REACTTRACE);
207  return -1;
208 }
209 
210 inline int
212 handle_close (int /* fd */)
213 {
214  trace_with_mask("EventHandler::handle_close",REACTTRACE);
215  return -1;
216 }
217 
218 inline void
220 resetState (void)
221 {
222  trace_with_mask("EventHandler::reset",REACTTRACE);
223 }
224 
236 typedef int (EventHandler::*EH_IO_Callback) (int);
237 
238 } // end namespace ASSA
239 
240 #endif // _EventHandler_h
A collection of assert function wrappers.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
EventHandler class.
Definition: EventHandler.h:103
virtual int handle_close(int fd)
EOF on peer socket handler callback.
Definition: EventHandler.h:212
std::string m_id
Definition: EventHandler.h:160
virtual int handle_signal(int signum_)
Signal handler callback.
Definition: EventHandler.h:204
virtual int handle_write(int fd)
Write handler callback.
Definition: EventHandler.h:180
virtual int handle_except(int fd)
Exception handler callback.
Definition: EventHandler.h:188
void set_id(const std::string &id_)
Set EventHandler ID.
Definition: EventHandler.h:153
virtual void resetState(void)
A hook for derived class to reset internal state as needed.
Definition: EventHandler.h:220
std::string get_id() const
Retrieve EventHandler ID.
Definition: EventHandler.h:157
virtual int handle_timeout(TimerId tid)
Timeout handler callback.
Definition: EventHandler.h:196
virtual ~EventHandler()
Virtual destructor.
Definition: EventHandler.h:109
EventHandler()
Constructor.
Definition: EventHandler.h:165
virtual int handle_read(int fd)
Read event callback.
Definition: EventHandler.h:172
Definition: Acceptor.h:40
EventType
EventType defines events types that Reactor understands.
Definition: EventHandler.h:35
@ WRITE_EVENT
Notify when there will be room for at least 1 byte to be written to IO channel without blocking.
Definition: EventHandler.h:39
@ ALL_EVENTS
Mask that includes all events.
Definition: EventHandler.h:47
@ SIGNAL_EVENT
Notify when UNIX signal is delivered by OS.
Definition: EventHandler.h:45
@ READ_EVENT
Notify when there will be at least 1 byte available for reading from IO channel without blocking .
Definition: EventHandler.h:36
@ RWE_EVENTS
READ_EVENT | WRITE_EVENT | EXCEPT_EVENT
Definition: EventHandler.h:46
@ EXCEPT_EVENT
Notify when there is an exception condition detected in TCP layer.
Definition: EventHandler.h:42
@ TIMEOUT_EVENT
Notify about expired timer.
Definition: EventHandler.h:44
unsigned long TimerId
Timer Id is used in handle_timeout() calls.
Definition: EventHandler.h:27
bool isReadEvent(EventType e_)
Definition: EventHandler.h:51
bool isExceptEvent(EventType e_)
Definition: EventHandler.h:63
@ REACTTRACE
Extended Reactor/PrioriyQueue messages
Definition: LogMask.h:40
bool isSignalEvent(EventType e_)
Definition: EventHandler.h:75
bool isTimeoutEvent(EventType e_)
Definition: EventHandler.h:69
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
bool isRWEEvents(EventType e_)
Definition: EventHandler.h:81
bool isAllEvents(EventType e_)
Definition: EventHandler.h:87
bool isWriteEvent(EventType e_)
Definition: EventHandler.h:57