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