Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[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
33 //#include "FdManager.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37 namespace scheduler {
38
39     /** \brief Manage file descriptor event processing
40
41         The FdManager is the internal class which manages all events (all events need to somehow be
42         made accessible via a file descriptor). File descriptors are added or removed from the
43         FdManager which then allows waiting until an event occurs on one of the descriptors.
44
45         Registered events must be derived from FdManager::Event. The FdManager does \e not manage
46         the event classes, it just manages pointers to externally owned events (the events are owned
47         by the respective dispatchers). 
48
49         When an event is posted, it's \c signal() member is called. However, this call will \e not
50         execute the user callback registered for the event, it will just mark the relevant tasks as
51         runnable.
52
53         \implementation
54       */
55     class FdManager
56     {
57     public:
58         ///////////////////////////////////////////////////////////////////////////
59         // Types
60
61         ///< Event baseclass
62         struct Event {
63             virtual ~Event();
64             virtual void signal(int events) = 0; ///< Called when the given event is posted
65         };
66
67         enum Events { 
68             EV_READ = Poller<Event>::EV_READ, EV_PRIO = Poller<Event>::EV_PRIO, EV_WRITE = Poller<Event>::EV_WRITE,
69             EV_HUP = Poller<Event>::EV_HUP, EV_ERR = Poller<Event>::EV_ERR 
70         };
71
72         ///////////////////////////////////////////////////////////////////////////
73         ///\name Structors and default members
74         ///@{
75
76         FdManager();
77
78         ///@}
79         ///////////////////////////////////////////////////////////////////////////
80
81         bool set(int fd, int events, Event * entry); ///< Set file descriptor event mask
82                                         /**< This sets the event mask for \a fd to \a events which
83                                              is a combination of values from the \c Events enum. If
84                                              \a fd is already registered, the registration is
85                                              changed to conform to the parameters passed, otherwise
86                                              a new registration is added.
87                                              \param[in] fd file descriptor
88                                              \param[in] events events to register for
89                                              \param[in] entry event to signal 
90                                              \returns \c true, if \a fd supports polling, \c false
91                                                  otherwise */
92         void remove(int fd);            ///< Remove \a fd from the manager
93
94         void timeout(int t);            ///< Set event timeout
95                                         /**< proceseOnce() will wait for max \a t milliseconds for
96                                              an event to occur. If set to -1, processOnce() will
97                                              wait forever. */
98         int timeout() const;            ///< Get  timeout in milliseconds
99
100         void processOnce();             ///< Wait for events
101                                         /**< This call waits until at least one event is posted but
102                                              no longer than the current timeout(). */
103
104         ClockService::clock_type eventTime() const; ///< Time of last event
105
106     protected:
107
108     private:
109         Poller<Event> poller_;
110         senf::ClockService::clock_type eventTime_;
111     };
112
113 }}
114
115 ///////////////////////////////hh.e////////////////////////////////////////
116 #include "FdManager.cci"
117 //#include "FdManager.ct"
118 //#include "FdManager.cti"
119 #endif
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: