PPI: Remove disconnected connectors from MultiConnectorMixin modules
[senf.git] / senf / 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 #include <senf/Utils/Console/Console.hh>
34
35 //#include "Connectors.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 ///////////////////////////////////////////////////////////////////////////
40 // senf::ppi::connector::Connector
41
42 prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
43 {
44     // The connector is not registered -> route() or noroute() statement missing
45     SENF_ASSERT( module_ && 
46                  "senf::ppi::connector::Connector::connect(): (source) "
47                  "Missing route() or noroute()" );
48     // The connector is already connected
49     SENF_ASSERT( ! peer_ &&
50                  "senf::ppi::connector::Connector::connect(): (source) "
51                  "duplicate connection" );
52     // The target connector is not registered -> route() or noroute() statement missing
53     SENF_ASSERT( target.module_ &&
54                  "senf::ppi::connector::Connector::connect(): (target) "
55                  "Missing route() or noroute()" );
56     // The target connector is already connected
57     SENF_ASSERT( ! target.peer_ &&
58                  "senf::ppi::connector::Connector::connect(): (target) "
59                  "duplicate connection" );
60     if (! (packetTypeID() == typeid(void) ||
61            target.packetTypeID() == typeid(void) || 
62            packetTypeID() == target.packetTypeID()) )
63         throw IncompatibleConnectorsException() 
64             << ": " << prettyName(packetTypeID()) 
65             << " [in module " << prettyName(typeid(*module_))  << "] "
66             << ", " << prettyName(target.packetTypeID())
67             << " [in module " << prettyName(typeid(*target.module_)) << "]";
68             
69     peer_ = & target;
70     target.peer_ = this;
71
72     if (! initializationScheduled())
73         enqueueInitializable();
74     if (! peer().initializationScheduled())
75         peer().enqueueInitializable();
76 }
77
78 senf::ppi::connector::Connector::TraceState senf::ppi::connector::Connector::traceState_ (
79     senf::ppi::connector::Connector::NO_TRACING);
80
81 prefix_ void senf::ppi::connector::Connector::trace(Packet const & p, char const * label)
82 {
83     if (traceState_ ==  NO_TRACING)
84         return;
85     SENF_LOG_BLOCK(({
86                 std::string type (prettyName(p.typeId().id()));
87                 log << "PPI packet trace: " << label << " 0x" << std::hex << p.id() << " " 
88                     << type.substr(21, type.size()-22) << " on " << & module() << " " 
89                     << prettyName(typeid(module())) << " connector 0x" << this << "\n";
90                 if (traceState_ == TRACE_CONTENTS)
91                     p.dump(log);
92             }));
93 }
94
95 prefix_ void senf::ppi::connector::Connector::throttleTrace(char const * label,
96                                                             char const * type)
97 {
98     if (traceState_ == NO_TRACING)
99         return;
100     SENF_LOG_BLOCK(({
101                 log << "PPI throttling trace: " << label << " " << type << " on " << & module() 
102                     << " " << prettyName(typeid(module())) << " connector 0x" << this << "\n";
103             }));
104 }
105
106 namespace senf { namespace ppi { namespace connector {
107
108     SENF_CONSOLE_REGISTER_ENUM_MEMBER( 
109         Connector, TraceState, (NO_TRACING)(TRACE_IDS)(TRACE_CONTENTS) );
110
111 }}}
112
113 namespace {
114
115     struct ConsoleRegister
116     {
117         ConsoleRegister();
118     };
119
120     ConsoleRegister::ConsoleRegister()
121     {
122         senf::ppi::ModuleManager::instance().consoleDir()
123             .add("tracing", SENF_FNP(senf::ppi::connector::Connector::TraceState,
124                                      senf::ppi::connector::Connector::tracing, ()))
125             .doc("Log every packet sent or received by any module.\n"
126                  "There are three different tracing levels:\n"
127                  "\n"
128                  "    NO_TRACING      don't output any tracing information\n"
129                  "    TRACE_IDS       trace packet id's but do not show packet contents\n"
130                  "    TRACE_CONTENTS  trace complete packet contents\n"
131                  "\n"
132                  "A log message is generated whenever the packet traverses a connector. The\n"
133                  "TRACE_IDS log message has the following format:\n"
134                  "\n"
135                  "    PPI packet trace: <direction> <packet-id> <packet-type>\n"
136                  "                      on <module-id> <module-type> connector <connector-id>\n"
137                  "    PPI throttling trace: <direction> <throttle-msg>\n"
138                  "                      on <module-id> <module-type> connector <connector-id>\n"
139                  "\n"
140                  "The fields are:\n"
141                  "\n"
142                  "    direction       'IN' for packets/throttle notifications entering the module,\n"
143                  "                    'OUT' for packets/throttle notifications leaving it\n"
144                  "    packet-id       Numeric unique packet id. This value is unique for packets\n"
145                  "                    alive at the same time, packets at different times may (and\n"
146                  "                    will) share id's\n"
147                  "    packet-type     The type of the packet header\n"
148                  "    module-id       Unique module id\n"
149                  "    module-type     Type of the module the packet is sent to/from\n"
150                  "    connector-id    Unique connector id\n"
151                  "    throttle-msg    Type of throttling event\n");
152
153         senf::ppi::ModuleManager::instance().consoleDir()
154             .add("tracing", SENF_FNP(void, senf::ppi::connector::Connector::tracing,
155                                      (senf::ppi::connector::Connector::TraceState)))
156             .arg("state", "new tracing state");
157     }
158
159     ConsoleRegister consoleRegister;
160
161 }
162
163 prefix_ void senf::ppi::connector::Connector::disconnect()
164 {
165     // Cannot disconnected a non-connected connector
166     SENF_ASSERT( peer_ &&
167                  "senf::ppi::connector::Connector::disconnect(): Not connected" );
168
169     Connector & peer (*peer_);
170     peer_ = 0;
171     peer.peer_ = 0;
172
173     if (! initializationScheduled())
174         enqueueInitializable();
175     if (! peer.initializationScheduled())
176         peer.enqueueInitializable();
177
178     v_disconnected();
179     peer.v_disconnected();
180 }
181
182 prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
183 {
184     return typeid(void);
185 }
186
187 prefix_ void senf::ppi::connector::Connector::v_disconnected()
188     const
189 {}
190
191 ///////////////////////////////////////////////////////////////////////////
192 // senf::ppi::connector::PassiveConnector
193
194 ////////////////////////////////////////
195 // private members
196
197 prefix_ void senf::ppi::connector::PassiveConnector::v_init()
198 {
199     Routes::const_iterator i (routes_.begin());
200     Routes::const_iterator const i_end (routes_.end());
201     for (; i != i_end; ++i)
202         if ((*i)->throttled())
203             break;
204     if (i == i_end)
205         remoteThrottled_ = false;
206     if (throttled())
207         emitThrottle();
208     else
209         emitUnthrottle();
210 }
211
212 prefix_ void senf::ppi::connector::PassiveConnector::v_unthrottleEvent()
213 {}
214
215 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
216 {
217     if (std::find_if(routes_.begin(), routes_.end(), 
218                      boost::bind(&ForwardingRoute::throttled, _1)) == routes_.end()) {
219         remoteThrottled_ = false;
220         if (!nativeThrottled_)
221             emitUnthrottle();
222     } else
223         throttleTrace("OUT", "not forwarding unthrottle event");
224 }
225
226 ///////////////////////////////////////////////////////////////////////////
227 // senf::ppi::connector::ActiveConnector
228
229 ////////////////////////////////////////
230 // private members
231
232 prefix_ void senf::ppi::connector::ActiveConnector::v_init()
233 {
234     if (! connected())
235         notifyThrottle();
236 }
237
238 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
239 {
240     throttleTrace("IN ", "throttle");
241     if (! throttled_) {
242         throttled_ = true;
243         if (throttleCallback_)
244             throttleCallback_();
245         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
246         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
247         for (; i != i_end; ++i)
248             (*i)->notifyThrottle();
249     }
250 }
251
252 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
253 {
254     throttleTrace("IN ", "unthrottle");
255     if (throttled_) {
256         throttled_ = false;
257         if (unthrottleCallback_)
258             unthrottleCallback_();
259         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
260         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
261         for (; i != i_end; ++i)
262             (*i)->notifyUnthrottle();
263     }
264 }
265
266 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
267 {
268     notifyRoutes_.push_back(&route);
269 }
270
271 ///////////////////////////////////////////////////////////////////////////
272 // senf::ppi::connector::InputConnector
273
274 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
275 {
276     if (empty())
277         v_requestEvent();
278     Packet p;
279     if (! empty()) {
280         p = peek();
281         queue_.pop_back();
282         v_dequeueEvent();
283     }
284     trace(p, "IN ");
285     return p;
286 }
287
288 ////////////////////////////////////////
289 // private members
290
291 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
292 {}
293
294 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
295 {}
296
297 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
298 {}
299
300 ///////////////////////////////////////////////////////////////////////////
301 // senf::ppi::connector::GenericActiveInput
302
303 ////////////////////////////////////////
304 // private members
305
306 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
307 {
308     request();
309 }
310
311 ///////////////////////////////////////////////////////////////////////////
312 // senf::ppi::connector::GenericPassiveInput
313
314 ////////////////////////////////////////
315 // private members 
316
317 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
318 {
319     emit();
320     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
321 }
322
323 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
324 {
325     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
326 }
327
328 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
329 {
330     size_type n (queueSize());
331     while (n) {
332         emit();
333         size_type nn (queueSize());
334         if (n == nn)
335             break;
336         n = nn;
337     }
338 }
339
340 ///////////////////////////////cc.e////////////////////////////////////////
341 #undef prefix_
342 //#include "Connectors.mpp"
343
344 \f
345 // Local Variables:
346 // mode: c++
347 // fill-column: 100
348 // comment-column: 40
349 // c-file-style: "senf"
350 // indent-tabs-mode: nil
351 // ispell-local-dictionary: "american"
352 // compile-command: "scons -u test"
353 // End: