bad31fab00d7b79131c35517a4d95d16ffaf0427
[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 }
57
58 prefix_ void senf::ppi::module::PassiveJoin::onThrottle()
59 {
60     using boost::lambda::_1;
61     using boost::lambda::bind;
62     std::for_each(connectors().begin(), connectors().end(),
63                   bind(&connector::GenericPassiveInput::throttle, _1));
64 }
65
66 prefix_ void senf::ppi::module::PassiveJoin::onUnthrottle()
67 {
68     using boost::lambda::_1;
69     using boost::lambda::bind;
70     std::for_each(connectors().begin(), connectors().end(),
71                   bind(&connector::GenericPassiveInput::unthrottle, _1));
72 }
73
74 ///////////////////////////////////////////////////////////////////////////
75 // senf::ppi::module::PriorityJoin
76
77 prefix_ senf::ppi::module::PriorityJoin::PriorityJoin()
78 {
79     noroute(output);
80     output.onRequest(&PriorityJoin::request);
81 }
82
83 ////////////////////////////////////////
84 // private members
85
86 prefix_ void
87 senf::ppi::module::PriorityJoin::connectorSetup(PriorityJoin::ConnectorType & conn,
88                                                 int priority)
89 {
90     noroute(conn);
91     conn.onThrottle(&PriorityJoin::onThrottle);
92     conn.onUnthrottle(&PriorityJoin::onUnthrottle);
93
94     if (priority < 0) {
95         priority = connectors().size() + priority;
96         if (priority < 0)
97             priority = 0;
98     }
99     if (priority >= int(connectors().size())-1)
100         return;
101
102     connectors().insert(connectors().begin()+priority, connectors().pop_back().release());
103 }
104
105 prefix_ void senf::ppi::module::PriorityJoin::request()
106 {
107     using boost::lambda::_1;
108     using boost::lambda::bind;
109     PriorityJoin::ContainerType::iterator i (
110         std::find_if(connectors().begin(), connectors().end(),
111                      ! bind(&connector::GenericActiveInput::throttled, _1)));
112     if (i != connectors().end())
113         output((*i)());
114 }
115
116 prefix_ void senf::ppi::module::PriorityJoin::onThrottle()
117 {
118     if (std::find_if(connectors().begin(), connectors().end(),
119                      ! bind(&connector::GenericActiveInput::throttled, _1)) == connectors().end())
120         output.throttle();
121 }
122
123 prefix_ void senf::ppi::module::PriorityJoin::onUnthrottle()
124 {
125     output.unthrottle();
126 }
127
128 ///////////////////////////////cc.e////////////////////////////////////////
129 #undef prefix_
130 //#include "Joins.mpp"
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End: