d9ca8ae3b426e1abe32735bc113ff7e6a1a14269
[senf.git] / PPI / MonitorModule.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
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 MonitorModule public header */
25
26 #ifndef HH_SENF_PPI_MonitorModule_
27 #define HH_SENF_PPI_MonitorModule_ 1
28
29 // Custom includes
30 #include "Module.hh"
31 #include "Connectors.hh"
32
33 //#include "MonitorModule.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37 namespace ppi {
38 namespace module {
39
40     /** \brief Base class providing simple monitor module support
41
42         A monitor module is a module which needs information about traversing packets but does not
43         really act on the packets. Because of this, it is \e optional to connect the output: If the
44         output is not connected, the packets will be silently dropped.
45
46         This allows to add monitor modules either into an existing chain or add them using an
47         ActiveDuplicator. 
48
49         To write a monitor module, derive from senf::ppi::module::MonitorModule instead of
50         senf::ppi::module and implement v_handlePacket():
51
52         \code
53         class CountPackets
54             : public senf::ppi::module::MonitorModule<>
55         {
56             SENF_PPI_MODULE(CountPackets);
57         public:
58             SomeMonitor() : counter_ (0u) {}
59
60         private:
61             virtual void v_handlePacket(Packet const & p)
62             { ++ counter_; }
63
64             unsigned counter_;
65         };
66         \endcode
67
68         You may of course add events (or even further connectors besides \c input and \c output
69         provided by MonitorModule) to the module.
70
71         \tparam PacketType type of packet expected on input and sent on output. This is also the
72             type of the v_handlePacket() argument.
73      */
74     template <class PacketType=Packet>
75     class MonitorModule : public Module
76     {
77     public:
78         senf::ppi::connector::PassiveInput<PacketType> input;
79         senf::ppi::connector::ActiveOutput<PacketType> output;
80
81     protected:
82         MonitorModule();
83
84         virtual void v_handlePacket(PacketType const & p) = 0; ///< Called for each packet
85
86     private:
87         void request();
88         void throttle();
89         void unthrottle();
90     };
91         
92
93 }}}
94
95 ///////////////////////////////hh.e////////////////////////////////////////
96 //#include "MonitorModule.cci"
97 #include "MonitorModule.ct"
98 #include "MonitorModule.cti"
99 #endif
100
101 \f
102 // Local Variables:
103 // mode: c++
104 // fill-column: 100
105 // comment-column: 40
106 // c-file-style: "senf"
107 // indent-tabs-mode: nil
108 // ispell-local-dictionary: "american"
109 // compile-command: "scons -u test"
110 // End: