cd4b884117d825ec542e8893e1e5936dd92acbe4
[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
42       */
43     class FileDispatcher
44     {
45     public:
46         ///////////////////////////////////////////////////////////////////////////
47         // Types
48
49         typedef boost::function<void (int)> Callback;
50
51         enum Events { 
52             EV_READ = FdManager::EV_READ, EV_WRITE = FdManager::EV_WRITE,
53             EV_HUP = FdManager::EV_HUP, EV_ERR = FdManager::EV_ERR,
54             EV_ALL = FdManager::EV_READ | FdManager::EV_WRITE
55         };
56
57         ///////////////////////////////////////////////////////////////////////////
58         ///\name Structors and default members
59         ///@{
60
61         FileDispatcher(FdManager & manager, FIFORunner & runner);
62         ~FileDispatcher();
63
64         ///@}
65         ///////////////////////////////////////////////////////////////////////////
66         
67         void add(int fd, Callback const & cb, int events = EV_ALL);
68         void remove(int fd, int events = EV_ALL);
69
70         void prepareRun();
71
72         void timeout(int t);
73         int timeout() const;
74
75     protected:
76
77     private:
78         struct FileEvent
79             : public detail::FdTask<0, FileEvent>,
80               public detail::FdTask<1, FileEvent>
81         {
82             typedef detail::FdTask<0, FileEvent> ReadTask;
83             typedef detail::FdTask<1, FileEvent> WriteTask;
84
85             int activeEvents() const;
86             int events;
87         };
88
89         typedef std::map<int, FileEvent> FileMap;
90
91         FileMap files_;
92         FdManager & manager_;
93         FIFORunner & runner_;
94         int managerTimeout_;
95     };
96
97
98 }}
99
100 ///////////////////////////////hh.e////////////////////////////////////////
101 #include "FileDispatcher.cci"
102 //#include "FileDispatcher.ct"
103 //#include "FileDispatcher.cti"
104 #endif
105
106 \f
107 // Local Variables:
108 // mode: c++
109 // fill-column: 100
110 // comment-column: 40
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // End: