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