removed some useless spaces; not very important, I know :)
[senf.git] / Examples / RateStuffer / Mainpage.dox
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 /** \mainpage Example introducing the Packet Processing Infrastructure
24
25     This example application implements a simple PPI application: It will read UDP packets from an
26     input port and will forward them to another port at a fixed packet rate. If the input stream
27     does not provide enough packets, empty UDP packets will be sent instead.
28     
29     \section run Running the example
30
31     Running the example is a little bit more complicated since we need to provide example UDP
32     packets so we can see the application at work. We do this using \c netcat. We open several shell
33     windows and run the following commands, each in it's own window
34
35     The first command listens for incoming UDP packets on port 44345:
36     <pre>
37     # nc -u -l -p 44345
38     </pre>
39     
40     The next command starts the \c ratestuffer
41     <pre>
42     # cd .../Examples/RateStuffer
43     # scons -u
44     # ./ratestuffer
45     </pre>
46
47     We should now see '<idle>' messages arriving in the first window once per second. We now can
48     start another \c netcat to send packets to the input port.
49
50     <pre>
51     # nc -u localhost 44344
52     < type any text here >
53     </pre>
54
55     Whenever we send out a packet with CR in the last window we should see it appear in the first
56     one. If we send out packets faster than 1 packet per second, they will start to be discarded if
57     more than two packets are in flight.
58
59     \image html screenshot.png
60
61     \section setup Module setup
62
63     \image html ratestuffer.png
64
65     Above image depicts the module setup implementing the rate stuffer. A
66     senf::ppi::module::ActiveSocketSource reads the incoming UDP packets and sends them into a
67     senf::ppi::module::PassiveQueue (via a senf::ppi::module::ThrottleBarrier).
68
69     The \a queue feeds the packets into a senf::ppi::module::PriorityJoin. The CloneSource
70     \a generator is fed as second input into the \a join to provide the stuffing packets.
71
72     The RateFilter \a rateFilter reads packets from it's input at a fixed rate and writes them into
73     the senf::ppi::module::PassiveSocketWriter \a udpWriter. The senf::ppi::module::PriorityJoin
74     ensures that read requests of the RateStuffer's input are always serviced, either from the \a
75     queue or, if the \a queue output is throttled, from the \a generator.
76
77     The \a barrier is not strictly necessary. However, it makes the behavior of the RateStuffer
78     predictable in the case where packets need to be dropped. Without the
79     senf::ppi::module::ThrottleBarrier, the packets will be left in the kernel socket queue. Packets
80     will only start to be dropped when that queue fills up. The size of this queue cannot be easily
81     manipulated and it's initial size is immense. So to stop the kernel queue from filling up with
82     increasingly out-of-date packets, we add the \a barrier which will explicitly read and drop
83     excess packets.
84
85     \section code Example code
86
87     \dontinclude ratestuffer.cc
88
89     The code starts out including the necessary header files
90
91     \skip Custom
92     \skip #include
93     \until PPI
94
95     We also define some namespace aliases
96
97     \skip namespace
98     \until senf::ppi;
99
100     The RateStuffer application is based on one additional application module.
101
102     \subsection ratefilter The RateFilter module
103
104     The RateFilter module simply forwards packets at a fixed rate.
105
106     \skip class
107     \until };
108
109     Both connectors of the RateFilter module are active. The module is driven by a
110     senf::ppi::IntervalTimer.
111
112     \until }
113
114     The event is initialized to fire eery \a interval nanoseconds.  The traffic is routed 'across'
115     the timer which controls the traffic. This routing lets the module automatically handle
116     throttling events. The timer is registered to call RateFilter::timeout().
117
118     \until }
119
120     The event handler is quite simple: Every \a interval nanoseconds a packet is read from \a input
121     and forwarded to \a output.
122
123     This is all there is to the RateFilter module. Due to the routing setup, the timer will
124     automatically be disabled should either \a input or \a output become throttled. However, in this
125     specific case this should never happen: The \a input is connected (via the \a join) to the
126     senf::ppi::module::CloneSource, which will never throttle. The \a output is connected to a UDP
127     socket which also never throttles.
128
129     \subsection ratestuffer The RateStuffer subnet
130
131     We decide to implement the RateStuffer as a subnet or collection. This is a simple struct or
132     class which contains all the modules necessary for a specific functionality. The modules are
133     initialized and connected in the class's constructor. External connectors are exported as
134     references to the corresponding module connectors:
135
136     \skip class
137     \until rateFilter
138     
139     First the needed modules are declared. We have 
140     \li the \a barrier to discard incoming packets sent to fast
141     \li the \a queue to receive incoming packets and create throttling notifications
142     \li the \a generator to create the stuffing packets
143     \li the \a join to combine the input stream from the \a queue with the stuffing packet stream
144     \li the \a rateFilter to generate the fixed rate output stream
145
146     \until output
147
148     Here we declare the external connectors. The subnetwork exports a single input and output
149     connector. The external connectors are declared as \e references.
150
151     \until output
152
153     The constructor now initializes all the local objects. We pass the template \a packet to the \a
154     generator and set the timing \a interval of the \a rateFilter.
155     
156     The \a input and \a output connector references are bound to the corresponding connectors we
157     want to expose: \a input to the \a barrier's \a input and \a output to the \a rateFilter's \a
158     output.
159
160     \until };
161
162     The constructor body sets up the connections within the subnetwork. Finally, we set the queueing
163     discipline of the \a queue. This Completes the RateStuffer. This subnetwork can now be used like
164     a module.
165
166     \subsection main Application setup
167
168     The applications main() method starts out by initializing the socket handles
169
170     \skip main
171     \until 44345
172
173     The \a inputSocket is listening on port 44344 while the \a outputSocket will send packets to
174     port 44345 on localhost. The \a outputSocket uses the senf::ConnectedUDPv4SocketProtocol which
175     is compatible with the senf::ppi::module::PassiveSocketWriter module.
176
177     \until udpSink
178
179     Here we allocate the components:
180
181     \li \a udpReader to read the packets from \a inputSocket
182     \li \a stuffer for the real work and
183     \li \a udpWriter to send the packets to \a outputSocket
184
185     \until udpSink
186
187     The \ref senf::ppi::connect() calls setup the necessary connections.
188     
189     The module setup is complete, \ref senf::ppi::run() is called to enter the event loop.
190
191     \until }
192  */
193
194 \f
195 // Local Variables:
196 // mode: c++
197 // fill-column: 100
198 // comment-column: 40
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u doc"
203 // mode: flyspell
204 // mode: auto-fill
205 // End: