Implementing base support for TCP-style window-based limiting on per-FIB-prefix and per-face granularity

Currently, limits are supported only by ndn::fw::Limits forwarding
strategy and only at the very basic (not fully tested) level.
diff --git a/model/fw/fw-stats.cc b/model/fw/fw-stats.cc
index de30473..959d25f 100644
--- a/model/fw/fw-stats.cc
+++ b/model/fw/fw-stats.cc
@@ -167,9 +167,9 @@
 
 
 void
-FwStats::WillErasePendingInterest (Ptr<pit::Entry> pitEntry)
+FwStats::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
 {
-  super::WillErasePendingInterest (pitEntry);
+  super::WillEraseTimedOutPendingInterest (pitEntry);
 
   m_stats.Timeout (pitEntry->GetPrefix ().cut (1));
   
diff --git a/model/fw/fw-stats.h b/model/fw/fw-stats.h
index 2b8cae9..1a516d8 100644
--- a/model/fw/fw-stats.h
+++ b/model/fw/fw-stats.h
@@ -94,7 +94,7 @@
                   const Ptr<const Packet> &packet);
 
   virtual void
-  WillErasePendingInterest (Ptr<pit::Entry> pitEntry);
+  WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry);
 
   // from Object
   void
diff --git a/model/fw/nacks.h b/model/fw/nacks.h
index e9226ad..4e7da4e 100644
--- a/model/fw/nacks.h
+++ b/model/fw/nacks.h
@@ -51,8 +51,6 @@
               const Ptr<const Packet> &p);
 
 protected:
-  // using NdnForwardingStrategy::PropagateInterest; // some strange c++ cheating
-
   virtual void
   DidReceiveDuplicateInterest (const Ptr<Face> &face,
                                Ptr<InterestHeader> &header,
diff --git a/model/fw/ndn-forwarding-strategy.cc b/model/fw/ndn-forwarding-strategy.cc
index 1cbcd09..cf09878 100644
--- a/model/fw/ndn-forwarding-strategy.cc
+++ b/model/fw/ndn-forwarding-strategy.cc
@@ -427,8 +427,8 @@
 
 bool
 ForwardingStrategy::WillSendOutInterest (const Ptr<Face> &outgoingFace,
-                                             Ptr<InterestHeader> header,
-                                             Ptr<pit::Entry> pitEntry)
+                                         Ptr<InterestHeader> header,
+                                         Ptr<pit::Entry> pitEntry)
 {
   pit::Entry::out_iterator outgoing =
     pitEntry->GetOutgoing ().find (outgoingFace);
@@ -462,7 +462,7 @@
 }
 
 void
-ForwardingStrategy::WillErasePendingInterest (Ptr<pit::Entry> pitEntry)
+ForwardingStrategy::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
 {
   // do nothing for now. may be need to do some logging
 }
diff --git a/model/fw/ndn-forwarding-strategy.h b/model/fw/ndn-forwarding-strategy.h
index 1d4111e..75f6647 100644
--- a/model/fw/ndn-forwarding-strategy.h
+++ b/model/fw/ndn-forwarding-strategy.h
@@ -82,9 +82,19 @@
           Ptr<Packet> &payload,
           const Ptr<const Packet> &packet);
 
+  /**
+   * @brief Event fired just before PIT entry is removed by timeout
+   * @param pitEntry PIT entry to be removed
+   */
   virtual void
-  WillErasePendingInterest (Ptr<pit::Entry> pitEntry);
+  WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry);
 
+  /**
+   * @brief Event fired every time face is removed from NDN stack
+   * @param face face to be removed
+   *
+   * For example, when an application terminates, AppFace is removed and this method called by NDN stack.
+   */
   virtual void
   RemoveFace (Ptr<Face> face);
   
diff --git a/model/fw/per-fib-limits.cc b/model/fw/per-fib-limits.cc
new file mode 100644
index 0000000..77851bc
--- /dev/null
+++ b/model/fw/per-fib-limits.cc
@@ -0,0 +1,168 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ *         Ilya Moiseenko <iliamo@cs.ucla.edu>
+ */
+
+#include "per-fib-limits.h"
+
+#include "ns3/ndn-interest-header.h"
+#include "ns3/ndn-content-object-header.h"
+#include "ns3/ndn-pit.h"
+#include "ns3/ndn-pit-entry.h"
+
+#include "ns3/assert.h"
+#include "ns3/log.h"
+#include "ns3/simulator.h"
+
+#include <boost/foreach.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/lambda/bind.hpp>
+namespace ll = boost::lambda;
+
+NS_LOG_COMPONENT_DEFINE ("ndn.fw.PerFibLimits");
+
+namespace ns3 {
+namespace ndn {
+namespace fw {
+
+NS_OBJECT_ENSURE_REGISTERED (PerFibLimits);
+  
+TypeId
+PerFibLimits::GetTypeId (void)
+{
+  static TypeId tid = TypeId ("ns3::ndn::fw::PerFibLimits")
+    .SetGroupName ("Ndn")
+    .SetParent <FwStats> ()
+    .AddConstructor <PerFibLimits> ()
+    ;
+  return tid;
+}
+    
+PerFibLimits::PerFibLimits ()
+{
+}
+
+void
+PerFibLimits::DoDispose ()
+{
+  BestRoute::DoDispose ();
+  m_decayLimitsEvent.Cancel ();
+}
+
+bool
+PerFibLimits::WillSendOutInterest (const Ptr<Face> &outgoingFace,
+                                   Ptr<InterestHeader> header,
+                                   Ptr<pit::Entry> pitEntry)
+{
+  NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
+  // override all (if any) parent processing
+  
+  pit::Entry::out_iterator outgoing =
+    pitEntry->GetOutgoing ().find (outgoingFace);
+
+  if (outgoing != pitEntry->GetOutgoing ().end ())
+    {
+      return false;
+    }
+
+  if (pitEntry->GetFibEntry ()->GetLimits ().IsBelowLimit ())
+    {
+      if (outgoingFace->GetLimits ()->IsBelowLimit ())
+        {
+          pitEntry->AddOutgoing (outgoingFace);
+          return true;
+        }
+      else
+        {
+          pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
+        }
+    }
+  
+  return false;
+}
+
+void
+PerFibLimits::WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry)
+{
+  NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
+
+  for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
+       face != pitEntry->GetOutgoing ().end ();
+       face ++)
+    {
+      face->m_face->GetLimits ()->RemoveOutstanding ();
+      // face->m_face->GetLimits ()->DecreaseLimit (); !!! do not decrease per-face limit. it doesn't make sense !!!
+    }
+  
+  pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
+  pitEntry->GetFibEntry ()->GetLimits ().DecreaseLimit (); // multiplicative decrease
+
+  if (!m_decayLimitsEvent.IsRunning ())
+    m_decayLimitsEvent = Simulator::Schedule (Seconds (1.0), &PerFibLimits::DecayLimits, this);
+}
+
+
+void
+PerFibLimits::WillSatisfyPendingInterest (const Ptr<Face> &incomingFace,
+                                          Ptr<pit::Entry> pitEntry)
+{
+  NS_LOG_FUNCTION (this << pitEntry->GetPrefix ());
+
+  super::WillSatisfyPendingInterest (incomingFace, pitEntry);
+
+  for (pit::Entry::out_container::iterator face = pitEntry->GetOutgoing ().begin ();
+       face != pitEntry->GetOutgoing ().end ();
+       face ++)
+    {
+      face->m_face->GetLimits ()->RemoveOutstanding ();
+      // face->m_face->GetLimits ()->IncreaseLimit (); !!! do not increase (as do not decrease) per-face limit. again, it doesn't make sense
+    }
+  
+  pitEntry->GetFibEntry ()->GetLimits ().RemoveOutstanding ();
+  pitEntry->GetFibEntry ()->GetLimits ().IncreaseLimit (); // additive increase
+}
+
+
+// void
+// PerFibLimits::DidReceiveValidNack (const Ptr<Face> &incomingFace,
+//                                    uint32_t nackCode,
+//                                    Ptr<pit::Entry> pitEntry)
+// {
+//   // super::DidReceiveValidNack (incomingFace, nackCode, pitEntry);
+
+//   // ??
+// }
+
+void
+PerFibLimits::DecayLimits ()
+{
+  for (Ptr<fib::Entry> entry = m_fib->Begin ();
+       entry != m_fib->End ();
+       entry = m_fib->Next (entry))
+    {
+      entry->GetLimits ().DecayCurrentLimit ();
+    }
+
+  m_decayLimitsEvent = Simulator::Schedule (Seconds (1.0), &PerFibLimits::DecayLimits, this);
+}
+
+
+} // namespace fw
+} // namespace ndn
+} // namespace ns3
diff --git a/model/fw/per-fib-limits.h b/model/fw/per-fib-limits.h
new file mode 100644
index 0000000..2ee5141
--- /dev/null
+++ b/model/fw/per-fib-limits.h
@@ -0,0 +1,81 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+
+#ifndef NDNSIM_PER_FIB_LIMITS_H
+#define NDNSIM_PER_FIB_LIMITS_H
+
+#include "ns3/event-id.h"
+
+#include "fw-stats.h"
+
+namespace ns3 {
+namespace ndn {
+namespace fw {
+
+/**
+ * \ingroup ndn
+ * \brief Strategy implementing per-FIB entry limits
+ */
+class PerFibLimits :
+    public FwStats
+{
+public:
+  static TypeId
+  GetTypeId ();
+
+  /**
+   * @brief Default constructor
+   */
+  PerFibLimits ();
+
+  virtual void
+  WillEraseTimedOutPendingInterest (Ptr<pit::Entry> pitEntry);
+
+protected:
+  virtual bool
+  WillSendOutInterest (const Ptr<Face> &outgoingFace,
+                       Ptr<InterestHeader> header,
+                       Ptr<pit::Entry> pitEntry);
+  
+  virtual void
+  WillSatisfyPendingInterest (const Ptr<Face> &incomingFace,
+                              Ptr<pit::Entry> pitEntry);
+
+  // from Object
+  void
+  DoDispose ();
+  
+private:
+  void
+  DecayLimits ();
+  
+private:
+  EventId m_decayLimitsEvent;
+
+  typedef FwStats super;
+};
+
+
+} // namespace fw
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDNSIM_PER_FIB_LIMITS_H