Move include files in debian packge into 'senf' subdirectory
[senf.git] / Examples / RateStuffer / ratestuffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 ppitest non-inline non-template implementation */
25
26 //#include "ppitest.hh"
27 //#include "ppitest.ih"
28
29 // Custom includes
30 #include "../../Socket/Protocols/INet/UDPSocketHandle.hh"
31 #include "../../Socket/Protocols/INet/ConnectedUDPSocketHandle.hh"
32 #include "../../Socket/Protocols/INet/INetAddressing.hh"
33 #include "../../PPI/SocketReader.hh"
34 #include "../../PPI/SocketWriter.hh"
35 #include "../../PPI/Module.hh"
36 #include "../../PPI/IntervalTimer.hh"
37 #include "../../PPI/Joins.hh"
38 #include "../../PPI/ThrottleBarrier.hh"
39 #include "../../PPI/PassiveQueue.hh"
40 #include "../../PPI/Queueing.hh"
41 #include "../../PPI/CloneSource.hh"
42 #include "../../PPI/Setup.hh"
43
44 //#include "ppitest.mpp"
45 #define prefix_
46 ///////////////////////////////cc.p////////////////////////////////////////
47
48 namespace module = senf::ppi::module;
49 namespace connector = senf::ppi::connector;
50 namespace ppi = senf::ppi;
51
52 // ////////////////////////////////////////////////////////////////////////
53 // RateFilter
54
55 class RateFilter
56     : public module::Module
57 {
58     SENF_PPI_MODULE(RateFilter);
59 public:
60
61     connector::ActiveInput input;
62     connector::ActiveOutput output;
63
64     RateFilter(senf::ClockService::clock_type interval);
65
66 private:
67     void timeout();
68
69     ppi::IntervalTimer timer;
70 };
71
72 RateFilter::RateFilter(senf::ClockService::clock_type interval)
73     : timer(interval) 
74 {
75     route(input,timer);
76     route(timer,output);
77     registerEvent(&RateFilter::timeout, timer);
78 }
79
80 void RateFilter::timeout()
81 {
82     output(input());
83 }
84
85 // ////////////////////////////////////////////////////////////////////////
86
87 class RateStuffer
88 {
89     module::ThrottleBarrier barrier;
90     module::PassiveQueue    queue;
91     module::CloneSource     generator;
92     module::PriorityJoin    join;
93     RateFilter              rateFilter;
94
95 public:
96     connector::PassiveInput & input;
97     connector::ActiveOutput & output;
98
99     RateStuffer(senf::ClockService::clock_type interval, 
100                 senf::Packet packet,
101                 unsigned high = 1,
102                 unsigned low  = 0)
103     :   barrier    (),
104         queue      (),
105         generator  ( packet ),
106         join       (),
107         rateFilter ( interval ),
108         input      ( barrier.input ),
109         output     ( rateFilter.output )
110     {
111         ppi::connect( barrier,    queue      );
112         ppi::connect( queue,      join       );
113         ppi::connect( generator,  join       );
114         ppi::connect( join,       rateFilter );
115
116         queue.qdisc(ppi::ThresholdQueueing(high,low));
117     }
118 };
119         
120 // ////////////////////////////////////////////////////////////////////////
121 // ////////////////////////////////////////////////////////////////////////
122
123 // Module setup:
124 //
125 // 'O'        = active connector
126 // '>' or '<' = input connector
127 //
128 //                   +----------------------------------------------------+
129 //                   | stuffer                                            |
130 //                   |                                                    |
131 // [ udpReader ] O-->:---> [ queue ] -->O [      ]                        |
132 //                   |                    [ join ] -->O [ rateFilter] O-->:O--> [ udpWriter ]
133 //                   | [ generator ] -->O [      ]                        |
134 //                   |                                                    |
135 //                   +----------------------------------------------------+
136
137 int main(int argc, char * argv[])
138 {
139     senf::UDPv4ClientSocketHandle inputSocket;
140     inputSocket.bind(senf::INet4SocketAddress("0.0.0.0:44344"));
141
142     senf::ConnectedUDPv4ClientSocketHandle outputSocket(
143         senf::INet4SocketAddress("localhost:44345"));
144
145     module::ActiveSocketReader<>  udpReader  ( inputSocket );
146     RateStuffer                   stuffer    ( 1000000000ul, 
147                                                senf::DataPacket::create(std::string("<idle>\n")),
148                                                2u, 1u );
149     module::PassiveSocketWriter<> udpWriter  ( outputSocket );
150
151     ppi::connect( udpReader, stuffer   );
152     ppi::connect( stuffer,   udpWriter );
153
154     ppi::run();
155
156     return 0;
157 }
158
159 ///////////////////////////////cc.e////////////////////////////////////////
160 #undef prefix_
161 //#include "ppitest.mpp"
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u"
172 // End: