7d033a3126092a15c82188fc89f9a5d618968e42
[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_ void senf::scheduler::FdDispatcher::add(int fd, Callback const & cb, int events)
50 {
51     if (events == 0)
52         return;
53     
54     FdMap::iterator i (fds_.find(fd));
55     if (i == fds_.end()) {
56         i = fds_.insert(std::make_pair(fd, FdEvent())).first;
57         runner_.enqueue(static_cast<FdEvent::ReadTask*>(&i->second));
58         runner_.enqueue(static_cast<FdEvent::PrioTask*>(&i->second));
59         runner_.enqueue(static_cast<FdEvent::WriteTask*>(&i->second));
60     }
61     FdEvent & event (i->second);
62
63     if (events & EV_READ) event.FdEvent::ReadTask::cb = cb;
64     if (events & EV_PRIO) event.FdEvent::PrioTask::cb = cb;
65     if (events & EV_WRITE) event.FdEvent::WriteTask::cb = cb;
66
67     manager_.set(fd, event.activeEvents(), &event);
68 }
69
70 prefix_ void senf::scheduler::FdDispatcher::remove(int fd, int events)
71 {
72     if (events == 0)
73         return;
74
75     FdMap::iterator i (fds_.find(fd));
76     if (i == fds_.end())
77         return;
78     FdEvent & event (i->second);
79
80     if (events & EV_READ) event.FdEvent::ReadTask::cb = 0;
81     if (events & EV_PRIO) event.FdEvent::PrioTask::cb = 0;
82     if (events & EV_WRITE) event.FdEvent::WriteTask::cb = 0;
83
84     int activeEvents (event.activeEvents());
85     if (! activeEvents) {
86         manager_.remove(fd);
87         runner_.dequeue(static_cast<FdEvent::ReadTask*>(&i->second));
88         runner_.dequeue(static_cast<FdEvent::PrioTask*>(&i->second));
89         runner_.dequeue(static_cast<FdEvent::WriteTask*>(&i->second));
90         fds_.erase(fd);
91     } else
92         manager_.set(fd, activeEvents, &event);
93 }
94
95 ///////////////////////////////////////////////////////////////////////////
96 // senf::scheduler::FdDispatcher::FdEvent
97
98 prefix_ void senf::scheduler::FdDispatcher::FdEvent::signal(int e)
99 {
100     events = e;
101
102     if (events & EV_READ) 
103         ReadTask::runnable = true;
104     if (events & EV_PRIO) 
105         PrioTask::runnable = true;
106     if (events & EV_WRITE)
107         WriteTask::runnable = true;
108
109     if (events & (EV_ERR | EV_HUP) && ! events & (EV_READ | EV_PRIO | EV_WRITE)) {
110         if (ReadTask::cb) ReadTask::runnable = true;
111         if (PrioTask::cb) PrioTask::runnable = true;
112         if (WriteTask::cb) WriteTask::runnable = true;
113     }
114 }
115
116 ///////////////////////////////cc.e////////////////////////////////////////
117 #undef prefix_
118 //#include "FdDispatcher.mpp"
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // comment-column: 40
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // End: