geventhandler.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // geventhandler.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "geventhandler.h"
24 #include "geventloop.h"
25 #include "gexception.h"
26 #include "gdebug.h"
27 #include "gassert.h"
28 #include "gdescriptor.h"
29 #include "glog.h"
30 
32 {
33 }
34 
36 {
37  G_DEBUG( "GNet::EventHandler::readEvent: no override" ) ;
38 }
39 
41 {
42  G_DEBUG( "GNet::EventHandler::writeEvent: no override" ) ;
43 }
44 
46 {
47  throw G::Exception( "exception event" ) ;
48 }
49 
50 // ===
51 
52 GNet::EventHandlerList::EventHandlerList( const std::string & type ) :
53  m_type(type) ,
54  m_lock(0U) ,
55  m_has_garbage(false)
56 {
57 }
58 
60 {
61  return m_map.begin() ;
62 }
63 
65 {
66  return m_map.end() ;
67 }
68 
70 {
71  return m_map.find(fd) != m_map.end() ;
72 }
73 
75 {
76  Map::iterator p = m_map.find( fd ) ;
77  return p != m_map.end() ? (*p).second : NULL ;
78 }
79 
81 {
82  G_ASSERT( handler != NULL ) ;
83  G_DEBUG( "GNet::EventHandlerList::add: " << m_type << "-list: " << "adding " << fd ) ;
84 
85  m_map[fd] = handler ;
86 }
87 
89 {
90  Map::iterator p = m_map.find( fd ) ;
91  if( p != m_map.end() )
92  {
93  G_DEBUG( "GNet::EventHandlerList::remove: " << m_type << "-list: " << "removing " << fd ) ;
94  if( m_lock )
95  {
96  (*p).second = NULL ;
97  m_has_garbage = true ;
98  }
99  else
100  {
101  m_map.erase( p ) ;
102  }
103  }
104 }
105 
107 {
108  m_lock++ ;
109 }
110 
112 {
113  G_ASSERT( m_lock != 0U ) ;
114  m_lock-- ;
115  if( m_lock == 0U && m_has_garbage )
116  collectGarbage() ;
117 }
118 
119 void GNet::EventHandlerList::collectGarbage()
120 {
121  const Map::iterator end = m_map.end() ;
122  for( Map::iterator p = m_map.begin() ; p != end ; )
123  {
124  Map::iterator test = p++ ;
125  if( (*test).second == NULL )
126  m_map.erase( test ) ;
127  }
128  m_has_garbage = false ;
129 }
130 
Iterator begin() const
Returns a forward iterator.
virtual void readEvent()
Called for a read event.
A network file descriptor.
Definition: gdescriptor.h:37
void lock()
Called at the start of an iteration which might change the list.
#define G_ASSERT(test)
Definition: gassert.h:30
virtual void writeEvent()
Called for a write event.
Map::const_iterator Iterator
bool contains(Descriptor fd) const
Returns true if the list contains the given file-descriptor.
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
void add(Descriptor fd, EventHandler *handler)
Adds a file-descriptor/handler pair to the list.
#define G_DEBUG(expr)
Definition: glog.h:95
void unlock()
Called at the end of an iteration.
void remove(Descriptor fd)
Removes a file-descriptor from the list.
Iterator end() const
Returns an end iterator.
A general-purpose exception class derived from std::exception and containing a std::string.
Definition: gexception.h:44
EventHandlerList(const std::string &type)
Constructor.
EventHandler * find(Descriptor fd)
Finds the handler associated with the given file descriptor.
virtual ~EventHandler()
Destructor.
virtual void exceptionEvent()
Called for an exception event.