8f4926dc4be845be126796226883b4741e6ec039
[senf.git] / senf / PPI / Joins.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 Joins non-inline non-template implementation */
25
26 #include "Joins.hh"
27 //#include "Joins.ih"
28
29 // Custom includes
30 #include <algorithm>
31 #include <boost/bind.hpp>
32 #include <boost/lambda/lambda.hpp>
33 #include <boost/lambda/bind.hpp>
34
35 //#include "Joins.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::ppi::module::PassiveJoin
41
42 prefix_ senf::ppi::module::PassiveJoin::PassiveJoin()
43 {
44     noroute(output);
45     output.onThrottle(&PassiveJoin::onThrottle);
46     output.onUnthrottle(&PassiveJoin::onUnthrottle);
47 }
48
49 ////////////////////////////////////////
50 // private members
51
52 prefix_ void senf::ppi::module::PassiveJoin::connectorSetup(connector::PassiveInput<> & conn)
53 {
54     noroute(conn);
55     conn.onRequest(boost::bind(&PassiveJoin::request,this,boost::ref(conn)));
56     conn.qdisc( QueueingDiscipline::NONE);
57 }
58
59 prefix_ void senf::ppi::module::PassiveJoin::onThrottle()
60 {
61     using boost::lambda::_1;
62     using boost::lambda::bind;
63     std::for_each(connectors().begin(), connectors().end(),
64                   bind(&connector::GenericPassiveInput::throttle, _1));
65 }
66
67 prefix_ void senf::ppi::module::PassiveJoin::onUnthrottle()
68 {
69     using boost::lambda::_1;
70     using boost::lambda::bind;
71     std::for_each(connectors().begin(), connectors().end(),
72                   bind(&connector::GenericPassiveInput::unthrottle, _1));
73 }
74
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::ppi::module::PriorityJoin
77
78 prefix_ senf::ppi::module::PriorityJoin::PriorityJoin()
79 {
80     noroute(output);
81     output.onRequest(&PriorityJoin::request);
82 }
83
84 ////////////////////////////////////////
85 // private members
86
87 prefix_ void
88 senf::ppi::module::PriorityJoin::connectorSetup(PriorityJoin::ConnectorType & conn,
89                                                 int priority)
90 {
91     noroute(conn);
92     conn.onThrottle(&PriorityJoin::onThrottle);
93     conn.onUnthrottle(&PriorityJoin::onUnthrottle);
94
95     if (priority < 0) {
96         priority = connectors().size() + priority;
97         if (priority < 0)
98             priority = 0;
99     }
100     if (priority >= int(connectors().size())-1)
101         return;
102
103     connectors().insert(connectors().begin()+priority, connectors().pop_back().release());
104 }
105
106 prefix_ void senf::ppi::module::PriorityJoin::request()
107 {
108     using boost::lambda::_1;
109     using boost::lambda::bind;
110     PriorityJoin::ContainerType::iterator i (
111         std::find_if(connectors().begin(), connectors().end(),
112                      ! bind(&connector::GenericActiveInput::throttled, _1)));
113     if (i != connectors().end())
114         output((*i)());
115 }
116
117 prefix_ void senf::ppi::module::PriorityJoin::onThrottle()
118 {
119     if (std::find_if(connectors().begin(), connectors().end(),
120                      ! bind(&connector::GenericActiveInput::throttled, _1)) == connectors().end())
121         output.throttle();
122 }
123
124 prefix_ void senf::ppi::module::PriorityJoin::onUnthrottle()
125 {
126     output.unthrottle();
127 }
128
129 ///////////////////////////////cc.e////////////////////////////////////////
130 #undef prefix_
131 //#include "Joins.mpp"
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // End: