6a1e831cad5b111b279c9d9618ca6b2924e77e5c
[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::QueueingAlgorithmRegistry
35
36 template <class QAlgorithm>
37 prefix_ void senf::ppi::QueueingAlgorithmRegistry::registerQAlgorithm(std::string key)
38 {
39     if (qAlgoMap_.find( key) == qAlgoMap_.end() )
40         qAlgoMap_.insert(key, new detail::QueueingAlgorithmRegistry_Entry<QAlgorithm>() );
41     else
42         throw Exception("Duplicated QAlgorithm Registration ") << key;
43 }
44
45 ///////////////////////////////////////////////////////////////////////////
46 // senf::ppi::detail::QueueingAlgorithmRegistry_Entry<QAlgorithm>
47
48 template <class QAlgorithm>
49 prefix_ senf::ppi::QueueingAlgorithm::ptr senf::ppi::detail::QueueingAlgorithmRegistry_Entry<QAlgorithm>::create()
50     const
51 {
52     return QAlgorithm::create();
53 }
54
55 ///////////////////////////////////////////////////////////////////////////
56 // senf::ppi::module::PassiveQueueingSocketSink<Writer>
57
58 template <class Writer>
59 prefix_ senf::ppi::module::PassiveQueueingSocketSink<Writer>::PassiveQueueingSocketSink(Handle const & handle, QueueingAlgorithm::ptr qAlgorithm)
60     : dir( this),
61       handle_( handle), writer_( ),
62       qAlgo_( qAlgorithm),
63       event_( handle_, IOEvent::Write)
64 {
65     namespace fty = console::factory;
66     dir.add( "active", qAlgo_->consoleDir());
67     dir.add( "set", fty::Command(
68             &PassiveQueueingSocketSink<Writer>::setQAlgorithm, this) );
69     dir.add( "list", fty::Command(
70             &QueueingAlgorithmRegistry::dump, &QueueingAlgorithmRegistry::instance()));
71     registerEvent( event_, &PassiveQueueingSocketSink::writable );
72     event_.enabled( false);
73     noroute(input);
74     input.onRequest( &PassiveQueueingSocketSink::write);
75     input.qdisc( QueueingDiscipline::NONE);
76     checkThrottle();
77 }
78
79 template <class Writer>
80 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::write()
81 {
82     PacketType p ( input());
83     if (qAlgo_->size() > 0) {
84         qAlgo_->enqueue( p);
85         return;
86     }
87     if (! writer_( handle_, p)) {
88         if (qAlgo_->enqueue( p) && !event_.enabled()) {
89           event_.enabled( true);
90         }
91     }
92 }
93
94 template <class Writer>
95 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::writable()
96 {
97     PacketType p (qAlgo_->dequeue());
98     if (p)
99         writer_( handle_, p);
100     if (qAlgo_->size() == 0) {
101         event_.enabled( false);
102     }
103 }
104
105 template <class Writer>
106 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::checkThrottle()
107 {
108     if (handle_.valid())
109         input.unthrottle();
110     else
111         input.throttle();
112 }
113
114 template <class Writer>
115 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::qAlgorithm(QueueingAlgorithm::ptr qAlgorithm)
116 {
117 //    dir.remove( "active");
118     qAlgo_.reset( qAlgorithm);
119     dir.add( "active", qAlgo_->consoleDir());
120     if (event_.enabled())
121         event_.enabled( false);
122 }
123
124 template <class Writer>
125 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::setQAlgorithm(std::string const & key)
126 {
127     qAlgorithm( QueueingAlgorithmRegistry::instance().createQAlgorithm( key));
128 }
129
130 ///////////////////////////////ct.e////////////////////////////////////////
131 #undef prefix_
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // End::