Scheduler: TimerEvent doku
[senf.git] / Scheduler / FileDispatcher.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 FileDispatcher public header */
25
26 #ifndef HH_FileDispatcher_
27 #define HH_FileDispatcher_ 1
28
29 // Custom includes
30 #include <map>
31 #include "FdManager.hh"
32 #include "FIFORunner.hh"
33 #include "FdDispatcher.hh"
34
35 //#include "FileDispatcher.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace scheduler {
40
41     /** \brief Scheduler dispatcher managing disc files
42
43         This dispatcher manages file descriptors which are connected to disc files. Since disc files
44         do not support select() / poll() / epoll(), they are considered to be always ready (which is
45         especially untrue for remote files e.g. vie NFS).
46
47         The FileDispatcher will change the FdManager's event timeout value to 0 (from -1) whenever
48         there is at least one file registered.
49       */
50     class FileDispatcher
51     {
52     public:
53         ///////////////////////////////////////////////////////////////////////////
54         // Types
55
56         typedef boost::function<void (int)> Callback;
57
58         enum Events { 
59             EV_READ = FdManager::EV_READ, EV_WRITE = FdManager::EV_WRITE,
60             EV_HUP = FdManager::EV_HUP, EV_ERR = FdManager::EV_ERR,
61             EV_ALL = FdManager::EV_READ | FdManager::EV_WRITE
62         };
63
64         ///////////////////////////////////////////////////////////////////////////
65         ///\name Structors and default members
66         ///@{
67
68         FileDispatcher(FdManager & manager, FIFORunner & runner);
69         ~FileDispatcher();
70
71         ///@}
72         ///////////////////////////////////////////////////////////////////////////
73         
74         void add(std::string const & name, int fd, Callback const & cb, int events = EV_ALL);
75                                         ///< Add file descriptor callback
76                                         /**< There is always one active callback for each
77                                              combination of file descriptor and event. Registering a
78                                              new callback will overwrite the old callback.
79                                              \param[in] name descriptive name
80                                              \param[in] fd file descriptor
81                                              \param[in] cb callback
82                                              \param[in] events Events to call \a cb for */
83
84         void remove(int fd, int events = EV_ALL);
85                                         /**< \param[in] fd file descriptor
86                                              \param[in] events Events for which to remove the
87                                                  callback */
88
89         void prepareRun();              ///< Prepare tasks
90                                         /**< This must be called after the FdManager returns before
91                                              running the runnable tasks. */
92
93         void timeout(int t);            ///< Change FdManager timeout
94                                         /**< Since the FileDispatcher must be able to change the
95                                              timeout value, the value must be set here and not
96                                              directly in the FdManager. */
97         int timeout() const;            ///< Retrieve current timeout value
98
99         bool empty() const;             ///< \c true, if no files are registered.
100
101     protected:
102
103     private:
104         /// Internal: Disk file event
105         struct FileEvent
106             : public detail::FdTask<0, FileEvent>,
107               public detail::FdTask<1, FileEvent>
108         {
109             typedef detail::FdTask<0, FileEvent> ReadTask;
110             typedef detail::FdTask<1, FileEvent> WriteTask;
111
112             explicit FileEvent(std::string const & name)
113                 : ReadTask (name), WriteTask (name) {}
114
115             int activeEvents() const;
116             int events;
117         };
118
119         typedef std::map<int, FileEvent> FileMap;
120
121         FileMap files_;
122         FdManager & manager_;
123         FIFORunner & runner_;
124         int managerTimeout_;
125     };
126
127
128 }}
129
130 ///////////////////////////////hh.e////////////////////////////////////////
131 #include "FileDispatcher.cci"
132 //#include "FileDispatcher.ct"
133 //#include "FileDispatcher.cti"
134 #endif
135
136 \f
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // comment-column: 40
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // End: