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