limits: finalizing rate-based limits
diff --git a/utils/ndn-limits-rate.cc b/utils/ndn-limits-rate.cc
index 215250e..1bf0af4 100644
--- a/utils/ndn-limits-rate.cc
+++ b/utils/ndn-limits-rate.cc
@@ -23,6 +23,8 @@
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/random-variable.h"
+#include "ns3/ndn-face.h"
+#include "ns3/node.h"
NS_LOG_COMPONENT_DEFINE ("ndn.Limits.Rate");
@@ -44,6 +46,26 @@
}
void
+LimitsRate::NotifyNewAggregate ()
+{
+ super::NotifyNewAggregate ();
+
+ if (!m_isLeakScheduled)
+ {
+ if (GetObject<Face> () != 0)
+ {
+ NS_ASSERT_MSG (GetObject<Face> ()->GetNode () != 0, "Node object should exist on the face");
+
+ m_isLeakScheduled = true;
+ UniformVariable r (0,1);
+ Simulator::ScheduleWithContext (GetObject<Face> ()->GetNode ()->GetId (),
+ Seconds (r.GetValue ()), &LimitsRate::LeakBucket, this, 0.0);
+ }
+ }
+}
+
+
+void
LimitsRate::UpdateCurrentLimit (double limit)
{
NS_ASSERT_MSG (limit >= 0.0, "Limit should be greater or equal to zero");
@@ -80,7 +102,23 @@
{
const double leak = m_bucketLeak * interval;
+#ifdef NS3_LOG_ENABLE
+ if (m_bucket>1)
+ {
+ NS_LOG_DEBUG ("Leak from " << m_bucket << " to " << std::max (0.0, m_bucket - leak));
+ }
+#endif
+
m_bucket = std::max (0.0, m_bucket - leak);
+
+ // calculate interval so next time we will leak by 1.001, unless such interval would be more than 1 second
+ double newInterval = 1.0;
+ if (m_bucketLeak > 1.0)
+ {
+ newInterval = 1.001 / m_bucketLeak;
+ }
+
+ Simulator::Schedule (Seconds (newInterval), &LimitsRate::LeakBucket, this, newInterval);
}
} // namespace ndn
diff --git a/utils/ndn-limits-rate.h b/utils/ndn-limits-rate.h
index 4050bb2..15344c6 100644
--- a/utils/ndn-limits-rate.h
+++ b/utils/ndn-limits-rate.h
@@ -44,8 +44,9 @@
* \param prefix smart pointer to the prefix for the FIB entry
*/
LimitsRate ()
- : m_bucketMax (0)
- , m_bucketLeak (0)
+ : m_isLeakScheduled (false)
+ , m_bucketMax (0)
+ , m_bucketLeak (1)
, m_bucket (0)
{ }
@@ -96,6 +97,11 @@
return m_bucketLeak;
}
+protected:
+ // from Node
+ void
+ NotifyNewAggregate ();
+
private:
/**
* @brief Leak bucket, assuming `interval' seconds between leakages
@@ -106,6 +112,8 @@
LeakBucket (double interval);
private:
+ bool m_isLeakScheduled;
+
double m_bucketMax; ///< \brief Maximum Interest allowance for this face (maximum tokens that can be issued at the same time)
double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second (token bucket leak rate)
double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face (current size of token bucket)