1989c90f969f2604b0ac6b52c893a9a299a94d8e
[senf.git] / Scheduler / FileDispatcher.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 FileDispatcher non-inline non-template implementation */
25
26 #include "FileDispatcher.hh"
27 //#include "FileDispatcher.ih"
28
29 // Custom includes
30
31 //#include "FileDispatcher.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 prefix_ senf::scheduler::FileDispatcher::FileDispatcher(FdManager & manager,
36                                                         FIFORunner & runner)
37     : manager_ (manager), runner_ (runner), managerTimeout_ (manager.timeout())
38 {}
39
40 prefix_ senf::scheduler::FileDispatcher::~FileDispatcher()
41 {
42     manager_.timeout(-1);
43
44     for (FileMap::iterator i (files_.begin()); i != files_.end(); ++i) {
45         runner_.dequeue(static_cast<FileEvent::ReadTask*>(&i->second));
46         runner_.dequeue(static_cast<FileEvent::WriteTask*>(&i->second));
47     }
48 }
49
50 prefix_ void senf::scheduler::FileDispatcher::add(int fd, Callback const & cb, int events)
51 {
52     if (events == 0)
53         return;
54     
55     FileMap::iterator i (files_.find(fd));
56     if (i == files_.end()) {
57         i = files_.insert(std::make_pair(fd, FileEvent())).first;
58         runner_.enqueue(static_cast<FileEvent::ReadTask*>(&i->second));
59         runner_.enqueue(static_cast<FileEvent::WriteTask*>(&i->second));
60     }
61     FileEvent & event (i->second);
62
63     if (events & EV_READ) event.FileEvent::ReadTask::cb = cb;
64     if (events & EV_WRITE) event.FileEvent::WriteTask::cb = cb;
65     
66     manager_.timeout(0);
67 }
68
69 prefix_ void senf::scheduler::FileDispatcher::remove(int fd, int events)
70 {
71     if (events == 0)
72         return;
73
74     FileMap::iterator i (files_.find(fd));
75     if (i == files_.end()) 
76         return;
77     FileEvent & event (i->second);
78
79     if (events & EV_READ) event.FileEvent::ReadTask::cb = 0;
80     if (events & EV_WRITE) event.FileEvent::WriteTask::cb = 0;
81
82     int activeEvents (event.activeEvents());
83     if (! activeEvents) {
84         runner_.dequeue(static_cast<FileEvent::ReadTask*>(&i->second));
85         runner_.dequeue(static_cast<FileEvent::WriteTask*>(&i->second));
86         files_.erase(fd);
87     }
88     
89     if (files_.empty())
90         manager_.timeout(managerTimeout_);
91 }
92
93 prefix_ void senf::scheduler::FileDispatcher::prepareRun()
94 {
95     for (FileMap::iterator i (files_.begin()); i != files_.end(); ++i) {
96         i->second.events = i->second.activeEvents();
97         if (i->second.FileEvent::ReadTask::cb)
98             i->second.FileEvent::ReadTask::runnable = true;
99         if (i->second.FileEvent::WriteTask::cb)
100             i->second.FileEvent::WriteTask::runnable = true;
101     }
102 }
103
104 ///////////////////////////////cc.e////////////////////////////////////////
105 #undef prefix_
106 //#include "FileDispatcher.mpp"
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // comment-column: 40
113 // c-file-style: "senf"
114 // indent-tabs-mode: nil
115 // ispell-local-dictionary: "american"
116 // compile-command: "scons -u test"
117 // End: