594c5019e2aa0b1a64057b020cf7844b5767c223
[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     checkThrottle();
76 }
77
78 template <class Writer>
79 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::write()
80 {
81     PacketType p ( input.read());
82     if (qAlgo_->size() > 0) {
83         qAlgo_->enqueue( p);
84         return;
85     }
86     if (! writer_( handle_, p)) {
87         if (qAlgo_->enqueue( p) && !event_.enabled()) {
88           event_.enabled( true);
89         }
90     }
91 }
92
93 template <class Writer>
94 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::writable()
95 {
96     PacketType p (qAlgo_->dequeue());
97     if (p)
98         writer_( handle_, p);
99     if (qAlgo_->size() == 0) {
100         event_.enabled( false);
101     }
102 }
103
104 template <class Writer>
105 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::checkThrottle()
106 {
107     if (handle_.valid())
108         input.unthrottle();
109     else
110         input.throttle();
111 }
112
113 template <class Writer>
114 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::qAlgorithm(QueueingAlgorithm::ptr qAlgorithm)
115 {
116 //    dir.remove( "active");
117     qAlgo_.reset( qAlgorithm);
118     dir.add( "active", qAlgo_->consoleDir());
119     if (event_.enabled())
120         event_.enabled( false);
121 }
122
123 template <class Writer>
124 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::setQAlgorithm(std::string const & key)
125 {
126     qAlgorithm( QueueingAlgorithmRegistry::instance().createQAlgorithm( key));
127 }
128
129 ///////////////////////////////ct.e////////////////////////////////////////
130 #undef prefix_
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End::