7d0dc8680beeacc4ef7d458fc5e17c0808c3e05d
[senf.git] / senf / PPI / QueueingSocketSink.cc
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
24 /** \file
25     \brief QueueingSocketSink non-inline non-template implementation */
26
27 #include "QueueingSocketSink.hh"
28 //#include "QueueingSocketSink.ih"
29
30 // Custom includes
31 #include <senf/Utils/Console/Variables.hh>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 SENF_PPI_REGISTER_QALGORITHM( "FIFOQueueingAlgorithm", senf::ppi::FIFOQueueingAlgorithm);
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::ppi::QueueingAlgorithm
40
41 prefix_ senf::ppi::QueueingAlgorithm::QueueingAlgorithm()
42     : dir_(this)
43 {
44     namespace fty = console::factory;
45     dir_.add("size", fty::Command( &QueueingAlgorithm::size, this));
46     dir_.add("clear", fty::Command( &QueueingAlgorithm::clear, this));
47 }
48
49 prefix_ senf::console::DirectoryNode & senf::ppi::QueueingAlgorithm::consoleDir()
50 {
51     return dir_;
52 }
53
54 /////////////////////////////////////////////////////////////////////////////
55 // senf::ppi::QueueingAlgorithmRegistry
56
57 prefix_ void senf::ppi::QueueingAlgorithmRegistry::dump(std::ostream & os)
58     const
59 {
60     for (QAlgoMap::const_iterator i = qAlgoMap_.begin(); i != qAlgoMap_.end(); ++i) {
61         os << i->first << std::endl;
62     }
63 }
64
65 prefix_ senf::ppi::QueueingAlgorithm::ptr senf::ppi::QueueingAlgorithmRegistry::createQAlgorithm(std::string const & key)
66     const
67 {
68     QAlgoMap::const_iterator i (qAlgoMap_.find( key));
69     if (i != qAlgoMap_.end())
70         return i->second->create();
71     else
72         throw Exception("QueueingAlgorithm not registered: ") << key;
73 }
74
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::ppi::FIFOQueueingAlgorithm
77
78 prefix_ senf::ppi::FIFOQueueingAlgorithm::FIFOQueueingAlgorithm()
79     : max_size_( 64)
80 {
81     consoleDir().add("max-size", console::factory::Variable(max_size_) );
82 }
83
84 prefix_ senf::Packet senf::ppi::FIFOQueueingAlgorithm::v_dequeue()
85 {
86     if (queue_.size() > 0) {
87         Packet p (queue_.front());
88         queue_.pop();
89         return p;
90     }
91     return Packet();
92 }
93
94 prefix_ bool senf::ppi::FIFOQueueingAlgorithm::v_enqueue(Packet const & packet)
95 {
96     if (queue_.size() >= max_size_)
97         queue_.pop();
98     queue_.push( packet);
99     return true;
100 }
101
102 prefix_ void senf::ppi::FIFOQueueingAlgorithm::v_clear()
103 {
104     while (! queue_.empty())
105         queue_.pop();
106 }
107
108 prefix_ senf::ppi::QueueingAlgorithm::ptr senf::ppi::FIFOQueueingAlgorithm::create()
109 {
110     return new FIFOQueueingAlgorithm();
111 }
112
113 ///////////////////////////////cc.e////////////////////////////////////////
114 #undef prefix_