PacketDelayer.cc
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 
17 #include "PacketDelayer.hh"
18 //#include "PacketDelayer.ih"
19 
20 // Custom includes
21 #include <senf/Utils/membind.hh>
22 
23 #define prefix_
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26  : timer_ ( "PacketDelayer::timer", membind(&PacketDelayer::timeout, this), ClockService::clock_type(0), false),
27  variation_ (senf::ClockService::seconds(0))
28 {
29  init();
31 }
32 
34  : timer_( "PacketDelayer::timer", membind(&PacketDelayer::timeout, this), ClockService::clock_type(0), false),
35  variation_ (senf::ClockService::seconds(0))
36 {
37  init();
38  delay( packetDelay);
39 }
40 
41 prefix_ void senf::emu::PacketDelayer::init()
42 {
43  route( input, output);
44  output.onThrottle( membind(&PacketDelayer::throttleNotification, this) );
45  output.onUnthrottle( membind(&PacketDelayer::unthrottleNotification, this) );
47 }
48 
50 {
51  delay_ = delay;
52  enableDelay(delay != ClockService::seconds(0));
53 }
54 
56  const
57 {
58  return delay_;
59 }
60 
62 {
63  variation_ = variation;
64  enableDelay(delay_ != ClockService::seconds(0) or variation != ClockService::seconds(0));
65 }
66 
68  const
69 {
70  return variation_;
71 }
72 
73 prefix_ void senf::emu::PacketDelayer::enableDelay(bool flag)
74 {
75  if (flag) {
76  input.onRequest( &PacketDelayer::onRequest_delay);
77  rndEngine_.reset( new std::mt19937(std::random_device()()));
78  rndDist_.reset( new std::uniform_int_distribution<ClockService::int64_type>(
80  } else {
81  input.onRequest( &PacketDelayer::onRequest_noDelay);
82  rndEngine_.release();
83  rndDist_.release();
84  }
85 }
86 
87 prefix_ void senf::emu::PacketDelayer::onRequest_delay()
88 {
89  queue_.push( ClockService::now() + delay_ + ClockService::nanoseconds((*rndDist_)(*rndEngine_)) );
90  if (! timer_.enabled())
91  timer_.timeout( queue_.front());
92 }
93 
94 prefix_ void senf::emu::PacketDelayer::timeout()
95 {
96  while (!queue_.empty() && queue_.front() <= ClockService::now()) {
97  output.write( input.read());
98  queue_.pop();
99  }
100  if (! queue_.empty())
101  timer_.timeout( queue_.front());
102 }
103 
104 prefix_ void senf::emu::PacketDelayer::throttleNotification()
105 {
106  //tePullPacket.disable();
107 }
108 
109 prefix_ void senf::emu::PacketDelayer::unthrottleNotification()
110 {
111  //tePullPacket.enable();
112 }
113 
114 //-/////////////////////////////////////////////////////////////////////////////////////////////////
115 #undef prefix_
116 
117 
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // comment-column: 40
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // End:
config::time_type clock_type
Route< connector::InputConnector, connector::OutputConnector > & route(connector::InputConnector &input, connector::OutputConnector &output)
PacketDelayer public header.
static SENF_CLOCKSERVICE_CONSTEXPR clock_type seconds(int64_type const &v)
boost::function< R(Args)> membind(R(T::*fn)(Args), T *ob)
ppi::connector::PassiveInput input
static SENF_CLOCKSERVICE_CONSTEXPR int64_type in_nanoseconds(clock_type const &v)
void onUnthrottle(Handler handler)
void timeout(ClockService::clock_type const &timeout, bool initiallyEnabled=true)
ClockService::clock_type variation() const
static clock_type now()
ClockService::clock_type delay() const
void write(PacketType const &packet)
#define prefix_
ppi::connector::ActiveOutput output
void throttlingDisc(ThrottlingDisc const &disc)
static SENF_CLOCKSERVICE_CONSTEXPR clock_type nanoseconds(int64_type const &v)