027673d784b041f83d53f782b2c44d71ebde65cd
[senf.git] / senf / PPI / QueueingSocketSink.ct
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 non-inline template implementation  */
25
26 //#include "QueueingSocketSink.ih"
27
28 // Custom includes
29
30 #define prefix_
31 ///////////////////////////////ct.p////////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::ppi::FIFOQueueingAlgorithm<PacketType>
35
36 template <typename PacketType>
37 prefix_ senf::ppi::FIFOQueueingAlgorithm<PacketType>::FIFOQueueingAlgorithm(unsigned size)
38     : size_( size)
39 { }
40
41 template <typename PacketType>
42 prefix_ PacketType senf::ppi::FIFOQueueingAlgorithm<PacketType>::dequeue()
43 {
44     if (queue_.size() > 0) {
45         PacketType const & p = queue_.front();
46         queue_.pop();
47         return p;
48     }
49     return PacketType();
50 }
51
52 template <typename PacketType>
53 prefix_ bool senf::ppi::FIFOQueueingAlgorithm<PacketType>::enqueue(PacketType const & packet)
54 {
55     if (queue_.size() < size_) {
56         queue_.push( packet);
57         return true;
58     }
59     return false;
60 }
61
62 template <typename PacketType>
63 prefix_ void senf::ppi::FIFOQueueingAlgorithm<PacketType>::clear()
64 {
65     while (! queue_.empty())
66         queue_.pop();
67 }
68
69 ///////////////////////////////////////////////////////////////////////////
70 // senf::ppi::module::PassiveQueueingSocketSink<Writer>
71
72 template <class Writer>
73 template <class QAlgorithm>
74 prefix_ senf::ppi::module::PassiveQueueingSocketSink<Writer>::PassiveQueueingSocketSink(Handle handle, QAlgorithm const & qAlgorithm)
75     : handle_( handle), writer_( ),
76       qAlgo_( new QAlgorithm(qAlgorithm)),
77       event_( handle_, IOEvent::Write)
78 {
79     registerEvent( event_, &PassiveQueueingSocketSink::writable );
80     event_.enabled( false);
81     noroute(input);
82     input.onRequest( &PassiveQueueingSocketSink::write);
83     checkThrottle();
84 }
85
86 template <class Writer>
87 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::write()
88 {
89     PacketType p ( input.read());
90     if (qAlgo_->size() > 0) {
91         qAlgo_->enqueue( p);
92         return;
93     }
94     if (! writer_( handle_, p)) {
95         if (qAlgo_->enqueue( p) && !event_.enabled()) {
96             event_.enabled( true);
97         }
98     }
99 }
100
101 template <class Writer>
102 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::writable()
103 {
104     PacketType p (qAlgo_->dequeue());
105     if (p)
106         writer_( handle_, p);
107     if (qAlgo_->size() == 0) {
108         event_.enabled( false);
109     }
110 }
111
112 template <class Writer>
113 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::checkThrottle()
114 {
115     if (handle_.valid())
116         input.unthrottle();
117     else
118         input.throttle();
119 }
120
121 ///////////////////////////////ct.e////////////////////////////////////////
122 #undef prefix_
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: