e11e5e23307733c50042d1524458d40c59dfab36
[senf.git] / PPI / Connectors.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 Connectors non-inline non-template implementation */
25
26 #include "Connectors.hh"
27 #include "Connectors.ih"
28
29 // Custom includes
30 #include "Route.hh"
31 #include "Module.hh"
32
33 //#include "Connectors.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::ppi::connector::Connector
39
40 prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
41 {
42     SENF_ASSERT( module_ && ! peer_ && target.module_ && ! target.peer_ );
43     if (! (packetTypeID() == typeid(void) ||
44            target.packetTypeID() == typeid(void) || 
45            packetTypeID() == target.packetTypeID()) )
46         throw IncompatibleConnectorsException() 
47             << ": " << prettyName(packetTypeID()) 
48             << " [in module " << prettyName(typeid(*module_))  << "] "
49             << ", " << prettyName(target.packetTypeID())
50             << " [in module " << prettyName(typeid(*target.module_)) << "]";
51             
52     peer_ = & target;
53     target.peer_ = this;
54 }
55
56 prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
57 {
58     return typeid(void);
59 }
60
61 ///////////////////////////////////////////////////////////////////////////
62 // senf::ppi::connector::PassiveConnector
63
64 ////////////////////////////////////////
65 // private members
66
67 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
68 {
69     if (throttled() && !nativeThrottled_) {
70         Routes::const_iterator i (routes_.begin());
71         Routes::const_iterator const i_end (routes_.end());
72         for (; i != i_end; ++i)
73             if ((*i)->throttled())
74                 break;
75         if (i == i_end) {
76             remoteThrottled_ = false;
77             emitUnthrottle();
78         }
79     } 
80     else
81         remoteThrottled_ = false;
82 }
83
84 ///////////////////////////////////////////////////////////////////////////
85 // senf::ppi::connector::ActiveConnector
86
87 ////////////////////////////////////////
88 // private members
89
90 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
91 {
92     if (throttleCallback_)
93         throttleCallback_();
94     NotifyRoutes::const_iterator i (notifyRoutes_.begin());
95     NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
96     for (; i != i_end; ++i)
97         (*i)->notifyThrottle();
98 }
99
100 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
101 {
102     if (unthrottleCallback_)
103         unthrottleCallback_();
104     NotifyRoutes::const_iterator i (notifyRoutes_.begin());
105     NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
106     for (; i != i_end; ++i)
107         (*i)->notifyUnthrottle();
108 }
109
110 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
111 {
112     notifyRoutes_.push_back(&route);
113 }
114
115 ///////////////////////////////////////////////////////////////////////////
116 // senf::ppi::connector::InputConnector
117
118 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
119 {
120     if (empty())
121         v_requestEvent();
122     Packet p;
123     if (! empty()) {
124         p = peek();
125         queue_.pop_back();
126         v_dequeueEvent();
127     }
128     return p;
129 }
130
131 ////////////////////////////////////////
132 // private members
133
134 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
135 {}
136
137 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
138 {}
139
140 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
141 {}
142
143 ///////////////////////////////////////////////////////////////////////////
144 // senf::ppi::connector::GenericActiveInput
145
146 ////////////////////////////////////////
147 // private members
148
149 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
150 {
151     request();
152 }
153
154 ///////////////////////////////////////////////////////////////////////////
155 // senf::ppi::connector::GenericPassiveInput
156
157 ////////////////////////////////////////
158 // private members 
159
160 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
161 {
162     emit();
163     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
164 }
165
166 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
167 {
168     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
169 }
170
171 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
172 {
173     size_type n (queueSize());
174     while (n) {
175         emit();
176         size_type nn (queueSize());
177         if (n == nn)
178             break;
179         n = nn;
180     }
181 }
182
183 ///////////////////////////////cc.e////////////////////////////////////////
184 #undef prefix_
185 //#include "Connectors.mpp"
186
187 \f
188 // Local Variables:
189 // mode: c++
190 // fill-column: 100
191 // comment-column: 40
192 // c-file-style: "senf"
193 // indent-tabs-mode: nil
194 // ispell-local-dictionary: "american"
195 // compile-command: "scons -u test"
196 // End: