Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[senf.git] / Scheduler / FdDispatcher.cc
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 FdDispatcher non-inline non-template implementation */
25
26 #include "FdDispatcher.hh"
27 #include "FdDispatcher.ih"
28
29 // Custom includes
30 #include "../Utils/senfassert.hh"
31
32 //#include "FdDispatcher.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::scheduler::FdDispatcher
38
39 prefix_ senf::scheduler::FdDispatcher::~FdDispatcher()
40 {
41     for (FdMap::iterator i (fds_.begin()); i != fds_.end(); ++i) {
42         manager_.remove(i->first);
43         runner_.dequeue(static_cast<FdEvent::ReadTask*>(&i->second));
44         runner_.dequeue(static_cast<FdEvent::PrioTask*>(&i->second));
45         runner_.dequeue(static_cast<FdEvent::WriteTask*>(&i->second));
46     }
47 }
48
49 prefix_ bool senf::scheduler::FdDispatcher::add(std::string const & name, int fd,
50                                                 Callback const & cb, int events)
51 {
52     if (events == 0)
53         return true;
54     
55     FdMap::iterator i (fds_.find(fd));
56     if (i == fds_.end()) {
57         i = fds_.insert(std::make_pair(fd, FdEvent())).first;
58         runner_.enqueue(static_cast<FdEvent::ReadTask*>(&i->second));
59         runner_.enqueue(static_cast<FdEvent::PrioTask*>(&i->second));
60         runner_.enqueue(static_cast<FdEvent::WriteTask*>(&i->second));
61     }
62     FdEvent & event (i->second);
63
64     if (events & EV_READ) {
65         event.FdEvent::ReadTask::cb = cb;
66         event.FdEvent::ReadTask::name = name;
67     }
68     if (events & EV_PRIO) {
69         event.FdEvent::PrioTask::cb = cb;
70         event.FdEvent::PrioTask::name = name;
71     }
72     if (events & EV_WRITE) {
73         event.FdEvent::WriteTask::cb = cb;
74         event.FdEvent::WriteTask::name = name;
75     }
76
77     if (! manager_.set(fd, event.activeEvents(), &event)) {
78         runner_.dequeue(static_cast<FdEvent::ReadTask*>(&i->second));
79         runner_.dequeue(static_cast<FdEvent::PrioTask*>(&i->second));
80         runner_.dequeue(static_cast<FdEvent::WriteTask*>(&i->second));
81         fds_.erase(i);
82         return false;
83     }
84     else
85         return true;
86 }
87
88 prefix_ void senf::scheduler::FdDispatcher::remove(int fd, int events)
89 {
90     if (events == 0)
91         return;
92
93     FdMap::iterator i (fds_.find(fd));
94     if (i == fds_.end())
95         return;
96     FdEvent & event (i->second);
97
98     if (events & EV_READ) {
99         event.FdEvent::ReadTask::cb = 0;
100         event.FdEvent::ReadTask::name.clear();
101     }
102     if (events & EV_PRIO) {
103         event.FdEvent::PrioTask::cb = 0;
104         event.FdEvent::PrioTask::name.clear();
105     }
106     if (events & EV_WRITE) {
107         event.FdEvent::WriteTask::cb = 0;
108         event.FdEvent::WriteTask::name.clear();
109     }
110
111     int activeEvents (event.activeEvents());
112     if (! activeEvents) {
113         manager_.remove(fd);
114         runner_.dequeue(static_cast<FdEvent::ReadTask*>(&i->second));
115         runner_.dequeue(static_cast<FdEvent::PrioTask*>(&i->second));
116         runner_.dequeue(static_cast<FdEvent::WriteTask*>(&i->second));
117         fds_.erase(fd);
118     } else
119         manager_.set(fd, activeEvents, &event);
120 }
121
122 ///////////////////////////////////////////////////////////////////////////
123 // senf::scheduler::FdDispatcher::FdEvent
124
125 prefix_ void senf::scheduler::FdDispatcher::FdEvent::signal(int e)
126 {
127     events = e;
128
129     if (events & EV_READ) 
130         ReadTask::runnable = true;
131     if (events & EV_PRIO) 
132         PrioTask::runnable = true;
133     if (events & EV_WRITE)
134         WriteTask::runnable = true;
135
136     if ((events & (EV_ERR | EV_HUP)) && ! (events & (EV_READ | EV_PRIO | EV_WRITE))) {
137         if (ReadTask::cb) ReadTask::runnable = true;
138         if (PrioTask::cb) PrioTask::runnable = true;
139         if (WriteTask::cb) WriteTask::runnable = true;
140     }
141 }
142
143 ///////////////////////////////cc.e////////////////////////////////////////
144 #undef prefix_
145 //#include "FdDispatcher.mpp"
146
147 \f
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"
156 // End: