6c5c15c39fc6c24cbee362fdd1cf390b4004ea43
[senf.git] / senf / PPI / QueueingSocketSink.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@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 QueueingSocketSink public header */
25
26 #ifndef HH_SENF_PPI_QueueingSocketSink_
27 #define HH_SENF_PPI_QueueingSocketSink_ 1
28
29 // Custom includes
30 #include <queue>
31 #include "SocketSink.hh"
32
33 //#include "QueueingSocketSink.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37 namespace ppi {
38
39     template <typename PacketType=Packet>
40     class QueueingAlgorithm
41     {
42     public:
43         virtual ~QueueingAlgorithm() {};
44         virtual PacketType dequeue() = 0;
45         virtual bool enqueue(PacketType const & packet) = 0;
46         virtual unsigned size() const = 0;
47         virtual void clear() = 0;
48     };
49
50     template <typename PacketType=Packet>
51     class FIFOQueueingAlgorithm : public QueueingAlgorithm<PacketType>
52     {
53     public:
54         FIFOQueueingAlgorithm(unsigned size);
55
56         virtual PacketType dequeue();
57         virtual bool enqueue(PacketType const & packet);
58         virtual unsigned size() const;
59         virtual void clear();
60
61     private:
62         std::queue<PacketType> queue_;
63         unsigned size_;
64     };
65
66
67 namespace module {
68
69     /** \brief QueueingSocketSink
70
71         \ingroup io_modules
72      */
73     template <class Writer=ConnectedDgramWriter>
74     class PassiveQueueingSocketSink : public Module
75     {
76         SENF_PPI_MODULE(PassiveQueueingSocketSink);
77
78     public:
79         typedef typename Writer::Handle Handle; ///< Handle type requested by writer
80         typedef typename Writer::PacketType PacketType;
81
82         connector::PassiveInput<PacketType> input; ///< Input connector from which data is received
83
84         template <class QAlgorithm>
85         explicit PassiveQueueingSocketSink(Handle handle, QAlgorithm const & qAlgorithm);
86
87         Writer & writer();              ///< Access the Writer
88         Handle & handle();              ///< Access handle
89         void handle(Handle handle);     ///< Set handle
90                                         /**< Assigning an empty or in-valid() handle will disable
91                                              the module until a new valid handle is assigned. */
92         QueueingAlgorithm<PacketType> & qAlgorithm();
93
94     private:
95         void write();
96         void writable();
97         void checkThrottle();
98
99         Handle handle_;
100         Writer writer_;
101         boost::scoped_ptr<QueueingAlgorithm<PacketType> > qAlgo_;
102         IOEvent event_;
103     };
104
105 }}}
106
107 ///////////////////////////////hh.e////////////////////////////////////////
108 //#include "QueueingSocketSink.cci"
109 #include "QueueingSocketSink.ct"
110 #include "QueueingSocketSink.cti"
111 #endif
112
113 \f
114 // Local Variables:
115 // mode: c++
116 // fill-column: 100
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // comment-column: 40
122 // End: