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