Examples: fixed RateStuffer for new clock_type
[senf.git] / Examples / RateStuffer / ratestuffer.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ppitest non-inline non-template implementation */
30
31 //#include "ppitest.hh"
32 //#include "ppitest.ih"
33
34 // Custom includes
35 #include <senf/Socket/Protocols/INet.hh>
36 #include <senf/PPI.hh>
37
38 //#include "ppitest.mpp"
39 #define prefix_
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace module = senf::ppi::module;
43 namespace connector = senf::ppi::connector;
44 namespace ppi = senf::ppi;
45
46 //-////////////////////////////////////////////////////////////////////////
47 // RateFilter
48
49 class RateFilter
50     : public module::Module
51 {
52     SENF_PPI_MODULE(RateFilter);
53 public:
54
55     connector::ActiveInput<> input;
56     connector::ActiveOutput<> output;
57
58     RateFilter(senf::ClockService::clock_type interval);
59
60 private:
61     void timeout();
62
63     ppi::IntervalTimer timer;
64 };
65
66 RateFilter::RateFilter(senf::ClockService::clock_type interval)
67     : timer(interval)
68 {
69     route(input,timer);
70     route(timer,output);
71     registerEvent(timer, &RateFilter::timeout);
72 }
73
74 void RateFilter::timeout()
75 {
76     output(input());
77 }
78
79 //-////////////////////////////////////////////////////////////////////////
80
81 class RateStuffer
82 {
83     module::ThrottleBarrier barrier;
84     module::PassiveQueue    queue;
85     module::CloneSource     generator;
86     module::PriorityJoin    join;
87     RateFilter              rateFilter;
88
89 public:
90     connector::PassiveInput<> & input;
91     connector::ActiveOutput<> & output;
92
93     RateStuffer(senf::ClockService::clock_type interval,
94                 senf::Packet packet,
95                 unsigned high = 1,
96                 unsigned low  = 0)
97     :   barrier    (),
98         queue      (),
99         generator  ( packet ),
100         join       (),
101         rateFilter ( interval ),
102         input      ( barrier.input ),
103         output     ( rateFilter.output )
104     {
105         ppi::connect( barrier,    queue      );
106         ppi::connect( queue,      join       );
107         ppi::connect( generator,  join       );
108         ppi::connect( join,       rateFilter );
109
110         queue.qdisc(ppi::ThresholdQueueing(high,low));
111     }
112 };
113
114 //-////////////////////////////////////////////////////////////////////////
115 //-////////////////////////////////////////////////////////////////////////
116
117 // Module setup:
118 //
119 // 'O'        = active connector
120 // '>' or '<' = input connector
121 //
122 //                   +----------------------------------------------------+
123 //                   | stuffer                                            |
124 //                   |                                                    |
125 // [ udpSource ] O-->:---> [ queue ] -->O [      ]                        |
126 //                   |                    [ join ] -->O [ rateFilter] O-->:O--> [ udpSink ]
127 //                   | [ generator ] -->O [      ]                        |
128 //                   |                                                    |
129 //                   +----------------------------------------------------+
130
131 int main(int argc, char * argv[])
132 {
133     senf::UDPv4ClientSocketHandle inputSocket(
134         senf::INet4SocketAddress("0.0.0.0:44344"));
135
136     senf::ConnectedUDPv4ClientSocketHandle outputSocket(
137         senf::INet4SocketAddress("localhost:44345"));
138
139     module::ActiveSocketSource<>  udpSource ( inputSocket );
140     RateStuffer                   stuffer   ( senf::ClockService::seconds(1),
141                                               senf::DataPacket::create(std::string("<idle>\n")),
142                                               2u, 1u );
143     module::PassiveSocketSink<>   udpSink   ( outputSocket );
144
145     ppi::connect( udpSource, stuffer   );
146     ppi::connect( stuffer,   udpSink );
147
148     ppi::run();
149
150     return 0;
151 }
152
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 #undef prefix_
155 //#include "ppitest.mpp"
156
157 \f
158 // Local Variables:
159 // mode: c++
160 // fill-column: 100
161 // comment-column: 40
162 // c-file-style: "senf"
163 // indent-tabs-mode: nil
164 // ispell-local-dictionary: "american"
165 // compile-command: "scons -u"
166 // End: