48452330483988de8faf15507913cdafd6913963
[senf.git] / Scheduler / FdManager.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief FdManager public header */
25
26 #ifndef HH_FdManager_
27 #define HH_FdManager_ 1
28
29 // Custom includes
30 #include "Poller.hh"
31 #include "ClockService.hh"
32 #include "../Utils/singleton.hh"
33
34 //#include "FdManager.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     class Scheduler;
40
41 namespace scheduler {
42
43     /** \brief Manage file descriptor event processing
44
45         The FdManager is the internal class which manages all events (all events need to somehow be
46         made accessible via a file descriptor). File descriptors are added or removed from the
47         FdManager which then allows waiting until an event occurs on one of the descriptors.
48
49         Registered events must be derived from FdManager::Event. The FdManager does \e not manage
50         the event classes, it just manages pointers to externally owned events (the events are owned
51         by the respective dispatchers). 
52
53         When an event is posted, it's \c signal() member is called. However, this call will \e not
54         execute the user callback registered for the event, it will just mark the relevant tasks as
55         runnable.
56
57         \implementation
58       */
59     class FdManager
60         : public singleton<FdManager>
61     {
62     public:
63         ///////////////////////////////////////////////////////////////////////////
64         // Types
65
66         ///< Event baseclass
67         struct Event {
68             virtual ~Event();
69             virtual void signal(int events) = 0; ///< Called when the given event is posted
70         };
71
72         enum Events { 
73             EV_READ = Poller<Event>::EV_READ, EV_PRIO = Poller<Event>::EV_PRIO, EV_WRITE = Poller<Event>::EV_WRITE,
74             EV_HUP = Poller<Event>::EV_HUP, EV_ERR = Poller<Event>::EV_ERR 
75         };
76
77         ///////////////////////////////////////////////////////////////////////////
78         ///\name Structors and default members
79         ///@{
80
81         using singleton<FdManager>::instance;
82         using singleton<FdManager>::alive;
83
84         ///@}
85         ///////////////////////////////////////////////////////////////////////////
86
87         bool set(int fd, int events, Event * entry); ///< Set file descriptor event mask
88                                         /**< This sets the event mask for \a fd to \a events which
89                                              is a combination of values from the \c Events enum. If
90                                              \a fd is already registered, the registration is
91                                              changed to conform to the parameters passed, otherwise
92                                              a new registration is added.
93                                              \param[in] fd file descriptor
94                                              \param[in] events events to register for
95                                              \param[in] entry event to signal 
96                                              \returns \c true, if \a fd supports polling, \c false
97                                                  otherwise */
98         void remove(int fd);            ///< Remove \a fd from the manager
99
100         void timeout(int t);            ///< Set event timeout
101                                         /**< proceseOnce() will wait for max \a t milliseconds for
102                                              an event to occur. If set to -1, processOnce() will
103                                              wait forever. */
104         int timeout() const;            ///< Get  timeout in milliseconds
105
106         void processOnce();             ///< Wait for events
107                                         /**< This call waits until at least one event is posted but
108                                              no longer than the current timeout(). */
109
110         ClockService::clock_type eventTime() const; ///< Time of last event
111
112     protected:
113
114     private:
115         FdManager();
116
117         Poller<Event> poller_;
118         senf::ClockService::clock_type eventTime_;
119
120         friend class singleton<FdManager>;
121         friend class senf::Scheduler;
122     };
123
124 }}
125
126 ///////////////////////////////hh.e////////////////////////////////////////
127 #include "FdManager.cci"
128 //#include "FdManager.ct"
129 //#include "FdManager.cti"
130 #endif
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End: