switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / FdEvent.ih
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief FdDispatcher internal header */
30
31 #ifndef IH_SENF_Scheduler_FdEvent_
32 #define IH_SENF_Scheduler_FdEvent_ 1
33
34 // Custom includes
35 #include <senf/boost_intrusive/iset.hpp>
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace senf {
40 namespace scheduler {
41
42     void restart();
43
44 namespace detail {
45
46     struct FdSetCompare {
47         bool operator()(FdEvent const & a, FdEvent const & b) const
48         { return a.fd_ < b.fd_; }
49     };
50
51     struct FindFd {
52         bool operator()(FdEvent const & a, int b) const
53         { return a.fd_ < b; }
54         bool operator()(int a, FdEvent const & b) const
55         { return a < b.fd_; }
56     };
57
58     class FdDispatcher
59         : public senf::singleton<FdDispatcher>
60     {
61     public:
62         using senf::singleton<FdDispatcher>::instance;
63         using senf::singleton<FdDispatcher>::alive;
64
65         bool add(FdEvent & event);
66         void remove(FdEvent & event);
67
68         bool empty() const;
69
70     protected:
71
72     private:
73         FdDispatcher();
74         ~FdDispatcher();
75
76         typedef boost::intrusive::imultiset< FdSetBase::value_traits<FdEvent>,
77                                              FdSetCompare,
78                                              false > FdSet;
79
80         FdSet fds_;
81
82         friend void senf::scheduler::restart();
83         friend class singleton<FdDispatcher>;
84         friend class senf::scheduler::FdEvent;
85     };
86
87     class FileDispatcher
88         : public senf::singleton<FileDispatcher>
89     {
90     public:
91         using senf::singleton<FileDispatcher>::instance;
92         using senf::singleton<FileDispatcher>::alive;
93
94         void add(FdEvent & event);
95         void remove(FdEvent & event);
96
97         void prepareRun();
98
99         // Called by IdleEventDispatcher
100         void timeout(int t);
101         int timeout() const;
102
103         bool empty() const;
104
105     protected:
106
107     private:
108         FileDispatcher();
109         ~FileDispatcher();
110
111         // We really only need a list here but we need to use the same event structure used by
112         // the FdEvent.
113         typedef boost::intrusive::imultiset< FdSetBase::value_traits<FdEvent>,
114                                              FdSetCompare,
115                                              false > FdSet;
116
117         FdSet fds_;
118         int managerTimeout_;
119
120         friend void senf::scheduler::restart();
121         friend class singleton<FileDispatcher>;
122     };
123
124     template <class Handle>
125     int get_descriptor(Handle const & handle);
126
127     int retrieve_filehandle(int fd);
128
129 }}}
130
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 #endif
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // fill-column: 100
138 // comment-column: 40
139 // c-file-style: "senf"
140 // indent-tabs-mode: nil
141 // ispell-local-dictionary: "american"
142 // compile-command: "scons -u test"
143 // End: