model: Refactoring PIT and adding experimental support for a new policy to count container operations
diff --git a/model/pit/ndn-pit-impl.h b/model/pit/ndn-pit-impl.h
index 66ff69b..024054a 100644
--- a/model/pit/ndn-pit-impl.h
+++ b/model/pit/ndn-pit-impl.h
@@ -23,12 +23,18 @@
 
 #include "ndn-pit.h"
 
-#include "../../utils/trie/trie-with-policy.h"
+#include "ns3/log.h"
+#include "ns3/simulator.h"
 
+#include "../../utils/trie/trie-with-policy.h"
 #include "ndn-pit-entry-impl.h"
 
+#include "ns3/ndn-interest.h"
+#include "ns3/ndn-content-object.h"
+#include "ns3/ndn-forwarding-strategy.h"
 #include "ns3/ndn-name.h"
 
+
 namespace ns3 {
 namespace ndn {
 
@@ -104,6 +110,9 @@
   virtual Ptr<Entry>
   Next (Ptr<Entry>);
 
+  const typename super::policy_container &
+  GetPolicy () const { return super::getPolicy (); }
+
 protected:
   void RescheduleCleaning ();
   void CleanExpired ();
@@ -127,6 +136,8 @@
   Ptr<Fib> m_fib; ///< \brief Link to FIB table
   Ptr<ForwardingStrategy> m_forwardingStrategy;
 
+  static LogComponent g_log; ///< @brief Logging variable
+  
   // indexes
   typedef
   boost::intrusive::multiset<entry,
@@ -140,6 +151,311 @@
   friend class EntryImpl< PitImpl >;
 };
 
+//////////////////////////////////////////
+////////// Implementation ////////////////
+//////////////////////////////////////////
+
+template<class Policy>
+LogComponent PitImpl< Policy >::g_log = LogComponent (("ndn.pit." + Policy::GetName ()).c_str ());
+
+
+template<class Policy>
+TypeId
+PitImpl< Policy >::GetTypeId ()
+{
+  static TypeId tid = TypeId (("ns3::ndn::pit::"+Policy::GetName ()).c_str ())
+    .SetGroupName ("Ndn")
+    .SetParent<Pit> ()
+    .AddConstructor< PitImpl< Policy > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum size of PIT in bytes. If 0, limit is not enforced",
+                   UintegerValue (0),
+                   MakeUintegerAccessor (&PitImpl< Policy >::GetMaxSize,
+                                         &PitImpl< Policy >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+
+    .AddAttribute ("CurrentSize", "Get current size of PIT in bytes",
+                   TypeId::ATTR_GET,
+                   UintegerValue (0),
+                   MakeUintegerAccessor (&PitImpl< Policy >::GetCurrentSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<class Policy>
+uint32_t
+PitImpl<Policy>::GetCurrentSize () const
+{
+  return super::getPolicy ().size ();
+}
+
+template<class Policy>
+PitImpl<Policy>::PitImpl ()
+{
+}
+
+template<class Policy>
+PitImpl<Policy>::~PitImpl ()
+{
+}
+
+template<class Policy>
+uint32_t
+PitImpl<Policy>::GetMaxSize () const
+{
+  return super::getPolicy ().get_max_size ();
+}
+
+template<class Policy>
+void
+PitImpl<Policy>::SetMaxSize (uint32_t maxSize)
+{
+  super::getPolicy ().set_max_size (maxSize);
+}
+
+template<class Policy>
+void
+PitImpl<Policy>::NotifyNewAggregate ()
+{
+  if (m_fib == 0)
+    {
+      m_fib = GetObject<Fib> ();
+    }
+  if (m_forwardingStrategy == 0)
+    {
+      m_forwardingStrategy = GetObject<ForwardingStrategy> ();
+    }
+
+  Pit::NotifyNewAggregate ();
+}
+
+template<class Policy>
+void
+PitImpl<Policy>::DoDispose ()
+{
+  super::clear ();
+
+  m_forwardingStrategy = 0;
+  m_fib = 0;
+
+  Pit::DoDispose ();
+}
+
+template<class Policy>
+void
+PitImpl<Policy>::RescheduleCleaning ()
+{
+  // m_cleanEvent.Cancel ();
+  Simulator::Remove (m_cleanEvent); // slower, but better for memory
+  if (i_time.empty ())
+    {
+      // NS_LOG_DEBUG ("No items in PIT");
+      return;
+    }
+
+  Time nextEvent = i_time.begin ()->GetExpireTime () - Simulator::Now ();
+  if (nextEvent <= 0) nextEvent = Seconds (0);
+
+  NS_LOG_DEBUG ("Schedule next cleaning in " <<
+                nextEvent.ToDouble (Time::S) << "s (at " <<
+                i_time.begin ()->GetExpireTime () << "s abs time");
+
+  m_cleanEvent = Simulator::Schedule (nextEvent,
+                                      &PitImpl<Policy>::CleanExpired, this);
+}
+
+template<class Policy>
+void
+PitImpl<Policy>::CleanExpired ()
+{
+  NS_LOG_LOGIC ("Cleaning PIT. Total: " << i_time.size ());
+  Time now = Simulator::Now ();
+
+  // uint32_t count = 0;
+  while (!i_time.empty ())
+    {
+      typename time_index::iterator entry = i_time.begin ();
+      if (entry->GetExpireTime () <= now) // is the record stale?
+        {
+          m_forwardingStrategy->WillEraseTimedOutPendingInterest (entry->to_iterator ()->payload ());
+          super::erase (entry->to_iterator ());
+          // count ++;
+        }
+      else
+        break; // nothing else to do. All later records will not be stale
+    }
+
+  if (super::getPolicy ().size ())
+    {
+      NS_LOG_DEBUG ("Size: " << super::getPolicy ().size ());
+      NS_LOG_DEBUG ("i_time size: " << i_time.size ());
+    }
+  RescheduleCleaning ();
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Lookup (const ContentObject &header)
+{
+  /// @todo use predicate to search with exclude filters
+  typename super::iterator item = super::longest_prefix_match_if (header.GetName (), EntryIsNotEmpty ());
+
+  if (item == super::end ())
+    return 0;
+  else
+    return item->payload (); // which could also be 0
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Lookup (const Interest &header)
+{
+  // NS_LOG_FUNCTION (header.GetName ());
+  NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
+  NS_ASSERT_MSG (m_forwardingStrategy != 0, "Forwarding strategy  should be set");
+
+  typename super::iterator foundItem, lastItem;
+  bool reachLast;
+  boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ());
+
+  if (!reachLast || lastItem == super::end ())
+    return 0;
+  else
+    return lastItem->payload (); // which could also be 0
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Find (const Name &prefix)
+{
+  typename super::iterator item = super::find_exact (prefix);
+
+  if (item == super::end ())
+    return 0;
+  else
+    return item->payload ();
+}
+
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Create (Ptr<const Interest> header)
+{
+  NS_LOG_DEBUG (header->GetName ());
+  Ptr<fib::Entry> fibEntry = m_fib->LongestPrefixMatch (*header);
+  if (fibEntry == 0)
+    return 0;
+
+  // NS_ASSERT_MSG (fibEntry != 0,
+  //                "There should be at least default route set" <<
+  //                " Prefix = "<< header->GetName() << ", NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
+
+  Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry);
+  std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
+  if (result.first != super::end ())
+    {
+      if (result.second)
+        {
+          newEntry->SetTrie (result.first);
+          return newEntry;
+        }
+      else
+        {
+          // should we do anything?
+          // update payload? add new payload?
+          return result.first->payload ();
+        }
+    }
+  else
+    return 0;
+}
+
+
+template<class Policy>
+void
+PitImpl<Policy>::MarkErased (Ptr<Entry> item)
+{
+  if (this->m_PitEntryPruningTimout.IsZero ())
+    {
+      super::erase (StaticCast< entry > (item)->to_iterator ());
+    }
+  else
+    {
+      item->OffsetLifetime (this->m_PitEntryPruningTimout - item->GetExpireTime () + Simulator::Now ());
+    }
+}
+
+
+template<class Policy>
+void
+PitImpl<Policy>::Print (std::ostream& os) const
+{
+  // !!! unordered_set imposes "random" order of item in the same level !!!
+  typename super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0);
+  for (; item != end; item++)
+    {
+      if (item->payload () == 0) continue;
+
+      os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n";
+    }
+}
+
+template<class Policy>
+uint32_t
+PitImpl<Policy>::GetSize () const
+{
+  return super::getPolicy ().size ();
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Begin ()
+{
+  typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
+  for (; item != end; item++)
+    {
+      if (item->payload () == 0) continue;
+      break;
+    }
+
+  if (item == end)
+    return End ();
+  else
+    return item->payload ();
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::End ()
+{
+  return 0;
+}
+
+template<class Policy>
+Ptr<Entry>
+PitImpl<Policy>::Next (Ptr<Entry> from)
+{
+  if (from == 0) return 0;
+
+  typename super::parent_trie::recursive_iterator
+    item (*StaticCast< entry > (from)->to_iterator ()),
+    end (0);
+
+  for (item++; item != end; item++)
+    {
+      if (item->payload () == 0) continue;
+      break;
+    }
+
+  if (item == end)
+    return End ();
+  else
+    return item->payload ();
+}
+
+
 } // namespace pit
 } // namespace ndn
 } // namespace ns3