PPI: Add optional template arg for packet type to connectors
[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::GenericPassiveInput & senf::ppi::module::PassiveJoin::newInput()
46 {
47     inputs_.push_back(new connector::GenericPassiveInput());
48     connector::GenericPassiveInput & 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::GenericActiveInput & senf::ppi::module::PriorityJoin::newInput()
84 {
85     inputs_.push_back(new connector::GenericActiveInput());
86     connector::GenericActiveInput & input (inputs_.back());
87
88     noroute(input);
89     input.onThrottle(&PriorityJoin::onThrottle);
90     input.onUnthrottle(&PriorityJoin::onUnthrottle);
91
92     return input;
93 }
94
95 prefix_ void senf::ppi::module::PriorityJoin::request()
96 {
97     using boost::lambda::_1;
98     using boost::lambda::bind;
99     Inputs::iterator i (std::find_if(inputs_.begin(), inputs_.end(),
100                                      ! bind(&connector::GenericActiveInput::throttled, _1)));
101     if (i != inputs_.end())
102         output((*i)());
103 }
104
105 prefix_ void senf::ppi::module::PriorityJoin::onThrottle()
106 {
107     if (std::find_if(inputs_.begin(), inputs_.end(),
108                      ! bind(&connector::GenericActiveInput::throttled, _1)) == inputs_.end())
109         output.throttle();
110 }
111
112 prefix_ void senf::ppi::module::PriorityJoin::onUnthrottle()
113 {
114     output.unthrottle();
115 }
116
117 ///////////////////////////////cc.e////////////////////////////////////////
118 #undef prefix_
119 //#include "Joins.mpp"
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: