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