46c762ef3ee9418c7ff812887107203c66b6b87a
[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 #include "../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 trace: 0x" << std::hex << p.id() << " " 
88                     << type.substr(21, type.size()-22) << " " << label
89                     << " on " << & module() << " " << prettyName(typeid(module()))
90                     << " connector 0x" << this << "\n";
91                 if (traceState_ == TRACE_CONTENTS)
92                     p.dump(log);
93             }));
94 }
95
96 namespace senf { namespace ppi { namespace connector {
97
98     SENF_CONSOLE_REGISTER_ENUM_MEMBER( 
99         Connector, TraceState, (NO_TRACING)(TRACE_IDS)(TRACE_CONTENTS) );
100
101 }}}
102
103 namespace {
104
105     struct ConsoleRegister
106     {
107         ConsoleRegister();
108     };
109
110     ConsoleRegister::ConsoleRegister()
111     {
112         senf::console::sysdir()
113             .add("ppiTracing", SENF_FNP(senf::ppi::connector::Connector::TraceState,
114                                          senf::ppi::connector::Connector::tracing, ()))
115             .doc("Log every packet sent or received by any module.\n"
116                  "There are three different tracing levels:\n"
117                  "\n"
118                  "    NO_TRACING      don't output any tracing information\n"
119                  "    TRACE_IDS       trace packet id's but do not show packet contents\n"
120                  "    TRACE_CONTENTS  trace complete packet contents\n"
121                  "\n"
122                  "A log message is generated whenever the packet traverses a connector. The\n"
123                  "TRACE_IDS log message has the following format:\n"
124                  "\n"
125                  "    PPI trace: <packet-id> <packet-type> <direction>\n"
126                  "                      on <module-id> <module-type> connector <connector-id>\n"
127                  "\n"
128                  "The fields are:\n"
129                  "\n"
130                  "    packet-id       Numeric unique packet id. This value is unique for packets\n"
131                  "                    alive at the same time, packets at different times may (and\n"
132                  "                    will) share id's\n"
133                  "    packet-type     The type of the packet header\n"
134                  "    direction       'INCOMING' for packets entering the module, 'OUTGOING' for\n"
135                  "                    packets leaving it\n"
136                  "    module-id       Unique module id\n"
137                  "    module-type     Type of the module the packet is sent to/from\n"
138                  "    connector-id    Unique connector id\n");
139
140         senf::console::sysdir()
141             .add("ppiTracing", SENF_FNP(void, senf::ppi::connector::Connector::tracing,
142                                          (senf::ppi::connector::Connector::TraceState)))
143             .arg("state", "new tracing state");
144     }
145
146     ConsoleRegister consoleRegister;
147
148 }
149
150 prefix_ void senf::ppi::connector::Connector::disconnect()
151 {
152     // Cannot disconnected a non-connected connector
153     SENF_ASSERT( peer_ &&
154                  "senf::ppi::connector::Connector::disconnect(): Not connected" );
155     Connector & peer (*peer_);
156     peer_ = 0;
157     peer.peer_ = 0;
158
159     if (! initializationScheduled())
160         enqueueInitializable();
161     if (! peer.initializationScheduled())
162         peer.enqueueInitializable();
163 }
164
165 prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
166 {
167     return typeid(void);
168 }
169
170 ///////////////////////////////////////////////////////////////////////////
171 // senf::ppi::connector::PassiveConnector
172
173 ////////////////////////////////////////
174 // private members
175
176 prefix_ void senf::ppi::connector::PassiveConnector::v_init()
177 {
178     Routes::const_iterator i (routes_.begin());
179     Routes::const_iterator const i_end (routes_.end());
180     for (; i != i_end; ++i)
181         if ((*i)->throttled())
182             break;
183     if (i == i_end)
184         remoteThrottled_ = false;
185     if (throttled())
186         emitThrottle();
187     else
188         emitUnthrottle();
189 }
190
191 prefix_ void senf::ppi::connector::PassiveConnector::v_unthrottleEvent()
192 {}
193
194 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
195 {
196     if (std::find_if(routes_.begin(), routes_.end(), 
197                      boost::bind(&ForwardingRoute::throttled, _1)) == routes_.end()) {
198         remoteThrottled_ = false;
199         if (!nativeThrottled_)
200             emitUnthrottle();
201     }
202 }
203
204 ///////////////////////////////////////////////////////////////////////////
205 // senf::ppi::connector::ActiveConnector
206
207 ////////////////////////////////////////
208 // private members
209
210 prefix_ void senf::ppi::connector::ActiveConnector::v_init()
211 {
212     if (! connected())
213         notifyThrottle();
214 }
215
216 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
217 {
218     if (! throttled_) {
219         throttled_ = true;
220         if (throttleCallback_)
221             throttleCallback_();
222         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
223         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
224         for (; i != i_end; ++i)
225             (*i)->notifyThrottle();
226     }
227 }
228
229 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
230 {
231     if (throttled_) {
232         throttled_ = false;
233         if (unthrottleCallback_)
234             unthrottleCallback_();
235         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
236         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
237         for (; i != i_end; ++i)
238             (*i)->notifyUnthrottle();
239     }
240 }
241
242 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
243 {
244     notifyRoutes_.push_back(&route);
245 }
246
247 ///////////////////////////////////////////////////////////////////////////
248 // senf::ppi::connector::InputConnector
249
250 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
251 {
252     if (empty())
253         v_requestEvent();
254     Packet p;
255     if (! empty()) {
256         p = peek();
257         queue_.pop_back();
258         v_dequeueEvent();
259     }
260     trace(p, "INCOMING");
261     return p;
262 }
263
264 ////////////////////////////////////////
265 // private members
266
267 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
268 {}
269
270 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
271 {}
272
273 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
274 {}
275
276 ///////////////////////////////////////////////////////////////////////////
277 // senf::ppi::connector::GenericActiveInput
278
279 ////////////////////////////////////////
280 // private members
281
282 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
283 {
284     request();
285 }
286
287 ///////////////////////////////////////////////////////////////////////////
288 // senf::ppi::connector::GenericPassiveInput
289
290 ////////////////////////////////////////
291 // private members 
292
293 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
294 {
295     emit();
296     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
297 }
298
299 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
300 {
301     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
302 }
303
304 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
305 {
306     size_type n (queueSize());
307     while (n) {
308         emit();
309         size_type nn (queueSize());
310         if (n == nn)
311             break;
312         n = nn;
313     }
314 }
315
316 ///////////////////////////////cc.e////////////////////////////////////////
317 #undef prefix_
318 //#include "Connectors.mpp"
319
320 \f
321 // Local Variables:
322 // mode: c++
323 // fill-column: 100
324 // comment-column: 40
325 // c-file-style: "senf"
326 // indent-tabs-mode: nil
327 // ispell-local-dictionary: "american"
328 // compile-command: "scons -u test"
329 // End: