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