Reorganize examples
[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/PassiveQueue.hh"
39 #include "PPI/Setup.hh"
40
41 //#include "ppitest.mpp"
42 #define prefix_
43 ///////////////////////////////cc.p////////////////////////////////////////
44
45 namespace module = senf::ppi::module;
46 namespace connector = senf::ppi::connector;
47 namespace ppi = senf::ppi;
48
49 // ////////////////////////////////////////////////////////////////////////
50 // RateFilter
51
52 class RateFilter
53     : public module::Module
54 {
55     SENF_PPI_MODULE(RateFilter);
56 public:
57
58     connector::ActiveInput input;
59     connector::ActiveOutput output;
60
61     RateFilter(senf::ClockService::clock_type interval);
62
63 private:
64     void timeout();
65
66     ppi::IntervalTimer timer;
67 };
68
69 RateFilter::RateFilter(senf::ClockService::clock_type interval)
70     : timer(interval) 
71 {
72     route(input,timer);
73     route(timer,output);
74     registerEvent(&RateFilter::timeout, timer);
75 }
76
77 void RateFilter::timeout()
78 {
79     output(input());
80 }
81
82 // ////////////////////////////////////////////////////////////////////////
83 // CopyPacketGenerator
84
85 class CopyPacketGenerator
86     : public module::Module
87 {
88     SENF_PPI_MODULE(CopyPacketGenerator);
89 public:
90
91     connector::PassiveOutput output;
92
93     CopyPacketGenerator(senf::Packet p);
94
95 private:
96     void request();
97
98     senf::Packet packet;
99 };
100
101 CopyPacketGenerator::CopyPacketGenerator(senf::Packet p)
102     : packet(p) 
103 {
104     noroute(output);
105     output.onRequest(&CopyPacketGenerator::request);
106 }
107
108 void CopyPacketGenerator::request()
109 {
110     output(packet);
111 }
112
113 // ////////////////////////////////////////////////////////////////////////
114 // ////////////////////////////////////////////////////////////////////////
115
116 // Module setup:
117 //
118 // 'O'        = active connector
119 // '>' or '<' = input connector
120 //
121 // [ udpReader ] O--> [ queue ] -->O [      ]
122 //                                   [ join ] -->O [ rateFilter] O--> [ udpWriter ]
123 //                [ generator ] -->O [      ]
124
125 int main(int argc, char * argv[])
126 {
127     senf::UDPv4ClientSocketHandle inputSocket;
128     inputSocket.bind(senf::INet4SocketAddress("0.0.0.0:44344"));
129
130     senf::ConnectedUDPv4ClientSocketHandle outputSocket(
131         senf::INet4SocketAddress("localhost:44345"));
132
133     module::ActiveSocketReader<>  udpReader  (inputSocket);
134     module::PassiveQueue          queue;
135     CopyPacketGenerator           generator  (senf::DataPacket::create(std::string("<idle>\n")));
136     module::PriorityJoin          join;
137     RateFilter                    rateFilter (1000000000ul);
138     module::PassiveSocketWriter<> udpWriter  (outputSocket);
139
140     ppi::connect( udpReader,  queue      );
141     ppi::connect( queue,      join       );
142     ppi::connect( generator,  join       );
143     ppi::connect( join,       rateFilter );
144     ppi::connect( rateFilter, udpWriter  );
145
146     ppi::run();
147
148     return 0;
149 }
150
151 ///////////////////////////////cc.e////////////////////////////////////////
152 #undef prefix_
153 //#include "ppitest.mpp"
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // fill-column: 100
159 // comment-column: 40
160 // c-file-style: "senf"
161 // indent-tabs-mode: nil
162 // ispell-local-dictionary: "american"
163 // compile-command: "scons -u"
164 // End: