PPI: Assert documentation
[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 #include "ModuleManager.hh"
33
34 //#include "Connectors.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::ppi::connector::Connector
40
41 prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
42 {
43     // The connector is not registered -> route() or noroute() statement missing
44     SENF_ASSERT( module_ );
45     // The connector is already connected
46     SENF_ASSERT( ! peer_ );
47     // The target connector is not registered -> route() or noroute() statement missing
48     SENF_ASSERT( target.module_ );
49     // The target connector is already connected
50     SENF_ASSERT( ! target.peer_ );
51     if (! (packetTypeID() == typeid(void) ||
52            target.packetTypeID() == typeid(void) || 
53            packetTypeID() == target.packetTypeID()) )
54         throw IncompatibleConnectorsException() 
55             << ": " << prettyName(packetTypeID()) 
56             << " [in module " << prettyName(typeid(*module_))  << "] "
57             << ", " << prettyName(target.packetTypeID())
58             << " [in module " << prettyName(typeid(*target.module_)) << "]";
59             
60     peer_ = & target;
61     target.peer_ = this;
62
63     if (! initializationScheduled())
64         enqueueInitializable();
65     if (! peer().initializationScheduled())
66         peer().enqueueInitializable();
67 }
68
69 prefix_ void senf::ppi::connector::Connector::disconnect()
70 {
71     // Cannot disconnected a non-connected connector
72     SENF_ASSERT( peer_ );
73     Connector & peer (*peer_);
74     peer_ = 0;
75     peer.peer_ = 0;
76
77     if (! initializationScheduled())
78         enqueueInitializable();
79     if (! peer.initializationScheduled())
80         peer.enqueueInitializable();
81 }
82
83 prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
84 {
85     return typeid(void);
86 }
87
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::ppi::connector::PassiveConnector
90
91 ////////////////////////////////////////
92 // private members
93
94 prefix_ void senf::ppi::connector::PassiveConnector::v_init()
95 {
96     Routes::const_iterator i (routes_.begin());
97     Routes::const_iterator const i_end (routes_.end());
98     for (; i != i_end; ++i)
99         if ((*i)->throttled())
100             break;
101     if (i == i_end)
102         remoteThrottled_ = false;
103     if (throttled())
104         emitThrottle();
105     else
106         emitUnthrottle();
107 }
108
109 prefix_ void senf::ppi::connector::PassiveConnector::v_unthrottleEvent()
110 {}
111
112 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
113 {
114     if (throttled() && !nativeThrottled_) {
115         Routes::const_iterator i (routes_.begin());
116         Routes::const_iterator const i_end (routes_.end());
117         for (; i != i_end; ++i)
118             if ((*i)->throttled())
119                 break;
120         if (i == i_end) {
121             remoteThrottled_ = false;
122             emitUnthrottle();
123         }
124     } 
125     else
126         remoteThrottled_ = false;
127 }
128
129 ///////////////////////////////////////////////////////////////////////////
130 // senf::ppi::connector::ActiveConnector
131
132 ////////////////////////////////////////
133 // private members
134
135 prefix_ void senf::ppi::connector::ActiveConnector::v_init()
136 {
137     if (! connected())
138         notifyThrottle();
139 }
140
141 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
142 {
143     if (! throttled_) {
144         throttled_ = true;
145         if (throttleCallback_)
146             throttleCallback_();
147         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
148         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
149         for (; i != i_end; ++i)
150             (*i)->notifyThrottle();
151     }
152 }
153
154 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
155 {
156     if (throttled_) {
157         throttled_ = false;
158         if (unthrottleCallback_)
159             unthrottleCallback_();
160         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
161         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
162         for (; i != i_end; ++i)
163             (*i)->notifyUnthrottle();
164     }
165 }
166
167 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
168 {
169     notifyRoutes_.push_back(&route);
170 }
171
172 ///////////////////////////////////////////////////////////////////////////
173 // senf::ppi::connector::InputConnector
174
175 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
176 {
177     if (empty())
178         v_requestEvent();
179     Packet p;
180     if (! empty()) {
181         p = peek();
182         queue_.pop_back();
183         v_dequeueEvent();
184     }
185     return p;
186 }
187
188 ////////////////////////////////////////
189 // private members
190
191 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
192 {}
193
194 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
195 {}
196
197 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
198 {}
199
200 ///////////////////////////////////////////////////////////////////////////
201 // senf::ppi::connector::GenericActiveInput
202
203 ////////////////////////////////////////
204 // private members
205
206 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
207 {
208     request();
209 }
210
211 ///////////////////////////////////////////////////////////////////////////
212 // senf::ppi::connector::GenericPassiveInput
213
214 ////////////////////////////////////////
215 // private members 
216
217 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
218 {
219     emit();
220     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
221 }
222
223 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
224 {
225     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
226 }
227
228 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
229 {
230     size_type n (queueSize());
231     while (n) {
232         emit();
233         size_type nn (queueSize());
234         if (n == nn)
235             break;
236         n = nn;
237     }
238 }
239
240 ///////////////////////////////cc.e////////////////////////////////////////
241 #undef prefix_
242 //#include "Connectors.mpp"
243
244 \f
245 // Local Variables:
246 // mode: c++
247 // fill-column: 100
248 // comment-column: 40
249 // c-file-style: "senf"
250 // indent-tabs-mode: nil
251 // ispell-local-dictionary: "american"
252 // compile-command: "scons -u test"
253 // End: