switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / QueueingSocketSink.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 //
29 /** \file
30     \brief QueueingSocketSink non-inline non-template implementation */
31
32 #include "QueueingSocketSink.hh"
33 //#include "QueueingSocketSink.ih"
34
35 // Custom includes
36 #include <senf/Utils/Console/Variables.hh>
37
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 SENF_PPI_REGISTER_QALGORITHM( "FIFOQueueingAlgorithm", senf::ppi::FIFOQueueingAlgorithm);
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::ppi::QueueingAlgorithm
45
46 prefix_ senf::ppi::QueueingAlgorithm::QueueingAlgorithm()
47     : dir_(this)
48 {
49     namespace fty = console::factory;
50     dir_.add("size", fty::Command( &QueueingAlgorithm::size, this));
51     dir_.add("clear", fty::Command( &QueueingAlgorithm::clear, this));
52 }
53
54 prefix_ senf::console::DirectoryNode & senf::ppi::QueueingAlgorithm::consoleDir()
55 {
56     return dir_;
57 }
58
59 //-/////////////////////////////////////////////////////////////////////////////////////////////////
60 // senf::ppi::QueueingAlgorithmRegistry
61
62 prefix_ void senf::ppi::QueueingAlgorithmRegistry::dump(std::ostream & os)
63     const
64 {
65     for (QAlgoMap::const_iterator i = qAlgoMap_.begin(); i != qAlgoMap_.end(); ++i) {
66         os << i->first << std::endl;
67     }
68 }
69
70 prefix_ senf::ppi::QueueingAlgorithm::ptr senf::ppi::QueueingAlgorithmRegistry::createQAlgorithm(std::string const & key)
71     const
72 {
73     QAlgoMap::const_iterator i (qAlgoMap_.find( key));
74     if (i != qAlgoMap_.end())
75         return i->second->create();
76     else
77         throw Exception("QueueingAlgorithm not registered: ") << key;
78 }
79
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::ppi::FIFOQueueingAlgorithm
82
83 prefix_ senf::ppi::FIFOQueueingAlgorithm::FIFOQueueingAlgorithm()
84     : max_size_( 64)
85 {
86     consoleDir().add("max-size", console::factory::Variable(max_size_) );
87 }
88
89 prefix_ senf::Packet senf::ppi::FIFOQueueingAlgorithm::v_dequeue()
90 {
91     if (queue_.size() > 0) {
92         Packet p (queue_.front());
93         queue_.pop();
94         return p;
95     }
96     return Packet();
97 }
98
99 prefix_ bool senf::ppi::FIFOQueueingAlgorithm::v_enqueue(Packet const & packet)
100 {
101     if (queue_.size() >= max_size_)
102         queue_.pop();
103     queue_.push( packet);
104     return true;
105 }
106
107 prefix_ void senf::ppi::FIFOQueueingAlgorithm::v_clear()
108 {
109     while (! queue_.empty())
110         queue_.pop();
111 }
112
113 prefix_ senf::ppi::QueueingAlgorithm::ptr senf::ppi::FIFOQueueingAlgorithm::create()
114 {
115     return new FIFOQueueingAlgorithm();
116 }
117
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 #undef prefix_