limits: Adding support for callback that fired every time a new slot becomes available
diff --git a/utils/ndn-limits-rate.cc b/utils/ndn-limits-rate.cc
index 27e2f0d..f323cdb 100644
--- a/utils/ndn-limits-rate.cc
+++ b/utils/ndn-limits-rate.cc
@@ -57,7 +57,7 @@
NS_ASSERT_MSG (GetObject<Face> ()->GetNode () != 0, "Node object should exist on the face");
m_isLeakScheduled = true;
- UniformVariable r (0,1);
+ UniformVariable r (0.0, 1.0);
Simulator::ScheduleWithContext (GetObject<Face> ()->GetNode ()->GetId (),
Seconds (r.GetValue ()), &LimitsRate::LeakBucket, this, 0.0);
}
@@ -120,6 +120,8 @@
NS_LOG_DEBUG ("Leak from " << m_bucket << " to " << std::max (0.0, m_bucket - leak));
}
#endif
+
+ double bucketOld = m_bucket;
m_bucket = std::max (0.0, m_bucket - leak);
@@ -129,6 +131,12 @@
{
newInterval = 1.001 / m_bucketLeak;
}
+
+ if (m_bucketMax - bucketOld < 1.0 &&
+ m_bucketMax - m_bucket >= 1.0) // limit number of times this stuff is called
+ {
+ this->FireAvailableSlotCallback ();
+ }
Simulator::Schedule (Seconds (newInterval), &LimitsRate::LeakBucket, this, newInterval);
}
diff --git a/utils/ndn-limits-window.cc b/utils/ndn-limits-window.cc
index 95c221b..a859be5 100644
--- a/utils/ndn-limits-window.cc
+++ b/utils/ndn-limits-window.cc
@@ -80,6 +80,8 @@
NS_ASSERT_MSG (m_outstanding >= (uint32_t)1, "Should not be possible, unless we decreasing this number twice somewhere");
m_outstanding -= 1;
+
+ FireAvailableSlotCallback ();
}
} // namespace ndn
diff --git a/utils/ndn-limits.cc b/utils/ndn-limits.cc
index 338d195..ee4160e 100644
--- a/utils/ndn-limits.cc
+++ b/utils/ndn-limits.cc
@@ -40,5 +40,27 @@
return tid;
}
+Limits::Limits ()
+ : m_maxRate (-1)
+ , m_maxDelay (1.0)
+ , m_handler (MakeNullCallback<void> ())
+{
+}
+
+
+void
+Limits::RegisterAvailableSlotCallback (CallbackHandler handler)
+{
+ m_handler = handler;
+}
+
+void
+Limits::FireAvailableSlotCallback ()
+{
+ if (!m_handler.IsNull ())
+ m_handler ();
+}
+
+
} // namespace ndn
} // namespace ns3
diff --git a/utils/ndn-limits.h b/utils/ndn-limits.h
index cf5c039..5fdad53 100644
--- a/utils/ndn-limits.h
+++ b/utils/ndn-limits.h
@@ -36,16 +36,15 @@
public Object
{
public:
+ typedef Callback<void> CallbackHandler;
+
static TypeId
GetTypeId ();
/**
* @brief Default constructor
*/
- Limits ()
- : m_maxRate (-1)
- , m_maxDelay (1.0)
- { }
+ Limits ();
/**
* @brief Virtual destructor
@@ -135,10 +134,26 @@
*/
virtual void
ReturnLimit () = 0;
+
+ ////////////////////////////////////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * @brief Set callback which will be called when exhausted limit gets a new slot
+ */
+ void
+ RegisterAvailableSlotCallback (CallbackHandler handler);
+
+protected:
+ void
+ FireAvailableSlotCallback ();
private:
double m_maxRate;
double m_maxDelay;
+
+ CallbackHandler m_handler;
};