switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Joins.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Joins non-inline non-template implementation */
30
31 #include "Joins.hh"
32 //#include "Joins.ih"
33
34 // Custom includes
35 #include <algorithm>
36 #include <boost/bind.hpp>
37 #include <boost/lambda/lambda.hpp>
38 #include <boost/lambda/bind.hpp>
39
40 //#include "Joins.mpp"
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 // senf::ppi::module::PassiveJoin
46
47 prefix_ senf::ppi::module::PassiveJoin::PassiveJoin()
48 {
49     noroute(output);
50     output.onThrottle(&PassiveJoin::onThrottle);
51     output.onUnthrottle(&PassiveJoin::onUnthrottle);
52 }
53
54 //-/////////////////////////////////////////////////////////////////////////////////////////////////
55 // private members
56
57 prefix_ void senf::ppi::module::PassiveJoin::connectorSetup(connector::PassiveInput<> & conn)
58 {
59     noroute(conn);
60     conn.onRequest(boost::bind(&PassiveJoin::request,this,boost::ref(conn)));
61     conn.qdisc( QueueingDiscipline::NONE);
62 }
63
64 prefix_ void senf::ppi::module::PassiveJoin::onThrottle()
65 {
66     using boost::lambda::_1;
67     using boost::lambda::bind;
68     std::for_each(connectors().begin(), connectors().end(),
69                   bind(&connector::GenericPassiveInput::throttle, _1));
70 }
71
72 prefix_ void senf::ppi::module::PassiveJoin::onUnthrottle()
73 {
74     using boost::lambda::_1;
75     using boost::lambda::bind;
76     std::for_each(connectors().begin(), connectors().end(),
77                   bind(&connector::GenericPassiveInput::unthrottle, _1));
78 }
79
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::ppi::module::PriorityJoin
82
83 prefix_ senf::ppi::module::PriorityJoin::PriorityJoin()
84 {
85     noroute(output);
86     output.onRequest(&PriorityJoin::request);
87 }
88
89 //-/////////////////////////////////////////////////////////////////////////////////////////////////
90 // private members
91
92 prefix_ void
93 senf::ppi::module::PriorityJoin::connectorSetup(PriorityJoin::ConnectorType & conn,
94                                                 int priority)
95 {
96     noroute(conn);
97     conn.onThrottle(&PriorityJoin::onThrottle);
98     conn.onUnthrottle(&PriorityJoin::onUnthrottle);
99
100     if (priority < 0) {
101         priority = connectors().size() + priority;
102         if (priority < 0)
103             priority = 0;
104     }
105     if (priority >= int(connectors().size())-1)
106         return;
107
108     connectors().insert(connectors().begin()+priority, connectors().pop_back().release());
109 }
110
111 prefix_ void senf::ppi::module::PriorityJoin::request()
112 {
113     using boost::lambda::_1;
114     using boost::lambda::bind;
115     PriorityJoin::ContainerType::iterator i (
116         std::find_if(connectors().begin(), connectors().end(),
117                      ! bind(&connector::GenericActiveInput::throttled, _1)));
118     if (i != connectors().end())
119         output((*i)());
120 }
121
122 prefix_ void senf::ppi::module::PriorityJoin::onThrottle()
123 {
124     if (std::find_if(connectors().begin(), connectors().end(),
125                      ! bind(&connector::GenericActiveInput::throttled, _1)) == connectors().end())
126         output.throttle();
127 }
128
129 prefix_ void senf::ppi::module::PriorityJoin::onUnthrottle()
130 {
131     output.unthrottle();
132 }
133
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 #undef prefix_
136 //#include "Joins.mpp"
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // End: