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