model+utils: New implementation (realization) of PIT, implementing limiting based on Interest packet sizes, not the number of entries

Also, small reorganization with policies
diff --git a/model/cs/content-store-with-freshness.h b/model/cs/content-store-with-freshness.h
index 9f394e3..6858f97 100644
--- a/model/cs/content-store-with-freshness.h
+++ b/model/cs/content-store-with-freshness.h
@@ -24,7 +24,7 @@
 #include "content-store-impl.h"
 
 #include "../../utils/trie/multi-policy.h"
-#include "../../utils/trie/freshness-policy.h"
+#include "custom-policies/freshness-policy.h"
 
 namespace ns3 {
 namespace ndn {
@@ -41,7 +41,7 @@
 
   static TypeId
   GetTypeId ();
-  
+
   virtual inline bool
   Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
 
@@ -51,7 +51,7 @@
 
   inline void
   RescheduleCleaning ();
-  
+
 private:
   static LogComponent g_log; ///< @brief Logging variable
 
diff --git a/model/cs/content-store-with-stats.h b/model/cs/content-store-with-stats.h
index 02ffec0..8d4a71d 100644
--- a/model/cs/content-store-with-stats.h
+++ b/model/cs/content-store-with-stats.h
@@ -24,7 +24,7 @@
 #include "content-store-impl.h"
 
 #include "../../utils/trie/multi-policy.h"
-#include "../../utils/trie/lifetime-stats-policy.h"
+#include "custom-policies/lifetime-stats-policy.h"
 
 namespace ns3 {
 namespace ndn {
@@ -42,15 +42,15 @@
     // connect traceback to the policy
     super::getPolicy ().template get<1> ().set_traced_callback (&m_willRemoveEntry);
   }
-  
+
   static TypeId
   GetTypeId ();
-  
+
 private:
   static LogComponent g_log; ///< @brief Logging variable
 
   /// @brief trace of for entry removal: first parameter is pointer to the CS entry, second is how long entry was in the cache
-  TracedCallback< Ptr<const Entry>, Time > m_willRemoveEntry; 
+  TracedCallback< Ptr<const Entry>, Time > m_willRemoveEntry;
 };
 
 //////////////////////////////////////////
diff --git a/utils/trie/freshness-policy.h b/model/cs/custom-policies/freshness-policy.h
similarity index 100%
rename from utils/trie/freshness-policy.h
rename to model/cs/custom-policies/freshness-policy.h
diff --git a/utils/trie/lifetime-stats-policy.h b/model/cs/custom-policies/lifetime-stats-policy.h
similarity index 100%
rename from utils/trie/lifetime-stats-policy.h
rename to model/cs/custom-policies/lifetime-stats-policy.h
diff --git a/model/pit/custom-policies/serialized-size-policy.h b/model/pit/custom-policies/serialized-size-policy.h
new file mode 100644
index 0000000..0050ac6
--- /dev/null
+++ b/model/pit/custom-policies/serialized-size-policy.h
@@ -0,0 +1,174 @@
+/* -*-  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 SERIALIZED_POLICY_H_
+#define SERIALIZED_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/set.hpp>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+
+/**
+ * @brief Traits for Least Recently Used replacement policy
+ */
+struct serialized_size_policy_traits
+{
+  /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
+  static std::string GetName () { return "SerializedSize"; }
+
+  struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t size; };
+
+  template<class Container>
+  struct container_hook
+  {
+    typedef boost::intrusive::member_hook< Container,
+                                           policy_hook_type,
+                                           &Container::policy_hook_ > type;
+  };
+
+  template<class Base,
+           class Container,
+           class Hook>
+  struct policy
+  {
+    static uint32_t& get_size (typename Container::iterator item)
+    {
+      return static_cast<typename policy_container::value_traits::hook_type*>
+        (policy_container::value_traits::to_node_ptr(*item))->size;
+    }
+
+    static const uint32_t& get_size (typename Container::const_iterator item)
+    {
+      return static_cast<const typename policy_container::value_traits::hook_type*>
+        (policy_container::value_traits::to_node_ptr(*item))->size;
+    }
+
+    template<class Key>
+    struct MemberHookLess
+    {
+      bool operator () (const Key &a, const Key &b) const
+      {
+        return get_size (&a) < get_size (&b);
+      }
+    };
+
+    typedef typename boost::intrusive::multiset< Container,
+                                                 boost::intrusive::compare< MemberHookLess< Container > >,
+                                                 Hook > policy_container;
+
+    // could be just typedef
+    class type : public policy_container
+    {
+    public:
+      typedef Container parent_trie;
+
+      type (Base &base)
+        : base_ (base)
+        , max_size_ (10000) // size in bytes. Default ~10 kilobytes
+        , current_space_used_ (0)
+      {
+      }
+
+      inline void
+      update (typename parent_trie::iterator item)
+      {
+        // in case size got changed
+        current_space_used_ -= get_size (item);
+        policy_container::erase (*item);
+
+        get_order (item) = item->payload ()->GetInterest ()->GetSerializedSize ();
+        current_space_used_ += get_size (item); // this operation can violate policy constraint, so in some case
+                                                // it may be necessary to remove some other element
+        policy_container::insert (*item);
+      }
+
+      inline bool
+      insert (typename parent_trie::iterator item)
+      {
+        if (max_size_ != 0 && current_space_used_ >= max_size_)
+          {
+            // the current version just fails to add an element, but it also possible
+            // to remove the largest element (last element in multi_map policy container)
+            return false;
+          }
+
+        get_size (item) = item->payload ()->GetInterest ()->GetSerializedSize ();
+        current_space_used_ += get_size (item);
+
+        policy_container::insert (*item);
+        return true;
+      }
+
+      inline void
+      lookup (typename parent_trie::iterator item)
+      {
+        // do nothing
+      }
+
+      inline void
+      erase (typename parent_trie::iterator item)
+      {
+        current_space_used_ -= get_size (item);
+        policy_container::erase (policy_container::s_iterator_to (*item));
+      }
+
+      inline void
+      clear ()
+      {
+        policy_container::clear ();
+      }
+
+      inline void
+      set_max_size (size_t max_size)
+      {
+        max_size_ = max_size;
+      }
+
+      inline size_t
+      get_max_size () const
+      {
+        return max_size_;
+      }
+
+      inline uint32_t
+      get_current_space_used () const
+      {
+        return current_space_used_;
+      }
+
+    private:
+      type () : base_(*((Base*)0)) { };
+
+    private:
+      Base &base_;
+      uint32_t max_size_;
+      uint32_t current_space_used_;
+    };
+  };
+};
+
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif
diff --git a/model/pit/ndn-pit-impl.cc b/model/pit/ndn-pit-impl.cc
index 41a814b..14b6b2a 100644
--- a/model/pit/ndn-pit-impl.cc
+++ b/model/pit/ndn-pit-impl.cc
@@ -28,6 +28,7 @@
 #include "../../utils/trie/persistent-policy.h"
 #include "../../utils/trie/random-policy.h"
 #include "../../utils/trie/lru-policy.h"
+#include "custom-policies/serialized-size-policy.h"
 
 #include "ns3/log.h"
 #include "ns3/string.h"
@@ -58,61 +59,18 @@
 
 using namespace ndnSIM;
 
-template<>
-TypeId
-PitImpl<persistent_policy_traits>::GetTypeId ()
+template<class Policy>
+uint32_t
+PitImpl<Policy>::GetCurrentSize () const
 {
-  static TypeId tid = TypeId ("ns3::ndn::pit::Persistent")
-    .SetGroupName ("Ndn")
-    .SetParent<Pit> ()
-    .AddConstructor< PitImpl< persistent_policy_traits > > ()
-    .AddAttribute ("MaxSize",
-                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
-                   StringValue ("0"),
-                   MakeUintegerAccessor (&PitImpl< persistent_policy_traits >::GetMaxSize,
-                                         &PitImpl< persistent_policy_traits >::SetMaxSize),
-                   MakeUintegerChecker<uint32_t> ())
-    ;
-
-  return tid;
+  return super::getPolicy ().size ();
 }
 
 template<>
-TypeId
-PitImpl<random_policy_traits>::GetTypeId ()
+uint32_t
+PitImpl<serialized_size_policy_traits>::GetCurrentSize () const
 {
-  static TypeId tid = TypeId ("ns3::ndn::pit::Random")
-    .SetGroupName ("Ndn")
-    .SetParent<Pit> ()
-    .AddConstructor< PitImpl< random_policy_traits > > ()
-    .AddAttribute ("MaxSize",
-                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
-                   StringValue ("0"),
-                   MakeUintegerAccessor (&PitImpl< random_policy_traits >::GetMaxSize,
-                                         &PitImpl< random_policy_traits >::SetMaxSize),
-                   MakeUintegerChecker<uint32_t> ())
-    ;
-
-  return tid;
-}
-
-template<>
-TypeId
-PitImpl<lru_policy_traits>::GetTypeId ()
-{
-  static TypeId tid = TypeId ("ns3::ndn::pit::Lru")
-    .SetGroupName ("Ndn")
-    .SetParent<Pit> ()
-    .AddConstructor< PitImpl< lru_policy_traits > > ()
-    .AddAttribute ("MaxSize",
-                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
-                   StringValue ("0"),
-                   MakeUintegerAccessor (&PitImpl< lru_policy_traits >::GetMaxSize,
-                                         &PitImpl< lru_policy_traits >::SetMaxSize),
-                   MakeUintegerChecker<uint32_t> ())
-    ;
-
-  return tid;
+  return super::getPolicy ().get_current_space_used ();
 }
 
 template<class Policy>
@@ -140,7 +98,7 @@
 }
 
 template<class Policy>
-void 
+void
 PitImpl<Policy>::NotifyNewAggregate ()
 {
   if (m_fib == 0)
@@ -156,7 +114,7 @@
 }
 
 template<class Policy>
-void 
+void
 PitImpl<Policy>::DoDispose ()
 {
   super::clear ();
@@ -170,7 +128,7 @@
 template<class Policy>
 void
 PitImpl<Policy>::RescheduleCleaning ()
-{  
+{
   // m_cleanEvent.Cancel ();
   Simulator::Remove (m_cleanEvent); // slower, but better for memory
   if (i_time.empty ())
@@ -181,11 +139,11 @@
 
   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);
 }
@@ -223,7 +181,7 @@
 Ptr<Entry>
 PitImpl<Policy>::Lookup (const ContentObjectHeader &header)
 {
-  /// @todo use predicate to search with exclude filters  
+  /// @todo use predicate to search with exclude filters
   typename super::iterator item = super::longest_prefix_match (header.GetName ());
 
   if (item == super::end ())
@@ -258,7 +216,7 @@
   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);
@@ -343,11 +301,11 @@
 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;
@@ -360,15 +318,104 @@
     return item->payload ();
 }
 
+//////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////////////////
+
+template<>
+TypeId
+PitImpl<persistent_policy_traits>::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::pit::Persistent")
+    .SetGroupName ("Ndn")
+    .SetParent<Pit> ()
+    .AddConstructor< PitImpl< persistent_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
+                   StringValue ("0"),
+                   MakeUintegerAccessor (&PitImpl< persistent_policy_traits >::GetMaxSize,
+                                         &PitImpl< persistent_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<>
+TypeId
+PitImpl<random_policy_traits>::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::pit::Random")
+    .SetGroupName ("Ndn")
+    .SetParent<Pit> ()
+    .AddConstructor< PitImpl< random_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
+                   StringValue ("0"),
+                   MakeUintegerAccessor (&PitImpl< random_policy_traits >::GetMaxSize,
+                                         &PitImpl< random_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<>
+TypeId
+PitImpl<lru_policy_traits>::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::pit::Lru")
+    .SetGroupName ("Ndn")
+    .SetParent<Pit> ()
+    .AddConstructor< PitImpl< lru_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum number of entries in PIT. If 0, limit is not enforced",
+                   StringValue ("0"),
+                   MakeUintegerAccessor (&PitImpl< lru_policy_traits >::GetMaxSize,
+                                         &PitImpl< lru_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+    ;
+
+  return tid;
+}
+
+template<>
+TypeId
+PitImpl<serialized_size_policy_traits>::GetTypeId ()
+{
+  static TypeId tid = TypeId ("ns3::ndn::pit::SerializedSize")
+    .SetGroupName ("Ndn")
+    .SetParent<Pit> ()
+    .AddConstructor< PitImpl< serialized_size_policy_traits > > ()
+    .AddAttribute ("MaxSize",
+                   "Set maximum size of PIT in bytes. If 0, limit is not enforced",
+                   UintegerValue (0),
+                   MakeUintegerAccessor (&PitImpl< serialized_size_policy_traits >::GetMaxSize,
+                                         &PitImpl< serialized_size_policy_traits >::SetMaxSize),
+                   MakeUintegerChecker<uint32_t> ())
+
+    .AddAttribute ("CurrentSize", "Get current size of PIT in bytes",
+                   TypeId::ATTR_GET,
+                   UintegerValue (0),
+                   MakeUintegerAccessor (&PitImpl< serialized_size_policy_traits >::GetCurrentSize),
+                   MakeUintegerChecker<uint32_t> ())
+
+    ;
+
+  return tid;
+}
+
 
 // explicit instantiation and registering
 template class PitImpl<persistent_policy_traits>;
 template class PitImpl<random_policy_traits>;
 template class PitImpl<lru_policy_traits>;
+template class PitImpl<serialized_size_policy_traits>;
 
 NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, persistent_policy_traits);
 NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, random_policy_traits);
 NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, lru_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, serialized_size_policy_traits);
 
 #ifdef DOXYGEN
 // /**
@@ -386,6 +433,12 @@
  * will be removed when PIT size reached its limit
  */
 class Lru : public PitImpl<lru_policy_traits> { };
+
+/**
+ * @brief A variant of persistent PIT implementation where size of PIT is based on size of interests in bytes (MaxSize parameter)
+ */
+class SerializedSize : public PitImpl<serialized_size_policy_traits> { };
+
 #endif
 
 } // namespace pit
diff --git a/model/pit/ndn-pit-impl.h b/model/pit/ndn-pit-impl.h
index 00cfe3c..7a346d2 100644
--- a/model/pit/ndn-pit-impl.h
+++ b/model/pit/ndn-pit-impl.h
@@ -73,7 +73,7 @@
    */
   virtual ~PitImpl ();
 
-  // inherited from Pit  
+  // inherited from Pit
   virtual Ptr<Entry>
   Lookup (const ContentObjectHeader &header);
 
@@ -82,7 +82,7 @@
 
   virtual Ptr<Entry>
   Create (Ptr<const InterestHeader> header);
-  
+
   virtual void
   MarkErased (Ptr<Entry> entry);
 
@@ -100,12 +100,12 @@
 
   virtual Ptr<Entry>
   Next (Ptr<Entry>);
-  
+
 protected:
   void RescheduleCleaning ();
   void CleanExpired ();
-  
-  // inherited from Object class                                                                                                                                                        
+
+  // inherited from Object class
   virtual void NotifyNewAggregate (); ///< @brief Even when object is aggregated to another Object
   virtual void DoDispose (); ///< @brief Do cleanup
 
@@ -115,7 +115,10 @@
 
   void
   SetMaxSize (uint32_t maxSize);
-  
+
+  uint32_t
+  GetCurrentSize () const;
+
 private:
   EventId m_cleanEvent;
   Ptr<Fib> m_fib; ///< \brief Link to FIB table
@@ -129,8 +132,8 @@
                                                        boost::intrusive::set_member_hook<>,
                                                        &entry::time_hook_>
                         > time_index;
-  time_index i_time; 
-                        
+  time_index i_time;
+
   friend class EntryImpl< PitImpl >;
 };