QueueingSocketSink.ct
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief QueueingSocketSink non-inline template implementation */
16 
17 //#include "QueueingSocketSink.ih"
18 
19 // Custom includes
20 #include <senf/Utils/Console/ParsedCommand.hh>
21 
22 #define prefix_
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::ppi::module::PassiveQueueingSocketSink<Writer>
27 
28 template <class Writer>
29 prefix_ senf::ppi::module::PassiveQueueingSocketSink<Writer>::PassiveQueueingSocketSink(Handle const & handle, QueueingAlgorithm::ptr qAlgorithm)
30  : handle_(handle), writer_(),
31  qAlgo_(qAlgorithm.release()),
32  event_(handle_, IOEvent::Write)
33 {
34  namespace fty = console::factory;
35  dir.add( "active", qAlgo_->consoleDir());
36  dir.add( "set", fty::Command(
37  &PassiveQueueingSocketSink<Writer>::setQAlgorithm, this) );
38  dir.add( "list", fty::Command(
39  &QueueingAlgorithmRegistry::dump, &QueueingAlgorithmRegistry::instance()));
40  registerEvent( event_, &PassiveQueueingSocketSink::writable );
41  event_.enabled( false);
42  noroute(input);
43  input.onRequest( &PassiveQueueingSocketSink::write);
44  input.throttlingDisc( ThrottlingDiscipline::NONE);
45  checkThrottle();
46 }
47 
48 template <class Writer>
49 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::handle(Handle const & handle)
50 {
51  handle_ = handle;
52  event_.set( handle_, IOEvent::Write);
53  qAlgo_->clear();
54  checkThrottle();
55 }
56 
57 template <class Writer>
58 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::write()
59 {
60  PacketType const & p ( input());
61  if (qAlgo_->size() > 0) {
62  qAlgo_->enqueue( p);
63  return;
64  }
65  if (! writer_( handle_, p)) {
66  if (qAlgo_->enqueue( p) && !event_.enabled()) {
67  event_.enabled( true);
68  }
69  }
70 }
71 
72 template <class Writer>
73 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::writable()
74 {
75  if (qAlgo_->peek() > 0) {
76  writer_( handle_, static_cast<PacketType const &>(qAlgo_->front()));
77  qAlgo_->pop();
78  }
79  if (qAlgo_->size() == 0) {
80  event_.enabled( false);
81  }
82 }
83 
84 template <class Writer>
85 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::checkThrottle()
86 {
87  if (handle_.valid())
88  input.unthrottle();
89  else
90  input.throttle();
91 }
92 
93 template <class Writer>
94 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::qAlgorithm(QueueingAlgorithm::ptr qAlgorithm)
95 {
96 // dir.remove( "active");
97  qAlgo_.reset( qAlgorithm.release());
98  dir.add( "active", qAlgo_->consoleDir());
99  if (event_.enabled())
100  event_.enabled( false);
101 }
102 
103 template <class Writer>
104 prefix_ void senf::ppi::module::PassiveQueueingSocketSink<Writer>::setQAlgorithm(std::string const & key)
105 {
106  qAlgorithm( QueueingAlgorithmRegistry::instance().createQAlgorithm( key));
107 }
108 
109 //-/////////////////////////////////////////////////////////////////////////////////////////////////
110 #undef prefix_
111 
112 
113 // Local Variables:
114 // mode: c++
115 // fill-column: 100
116 // comment-column: 40
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // End::