model: Refactoring PIT and adding experimental support for a new policy to count container operations
diff --git a/model/pit/ndn-pit-impl.cc b/model/pit/ndn-pit-impl.cc
index 777ab39..a0d0c3e 100644
--- a/model/pit/ndn-pit-impl.cc
+++ b/model/pit/ndn-pit-impl.cc
@@ -20,14 +20,12 @@
#include "ndn-pit-impl.h"
-#include "ns3/ndn-interest.h"
-#include "ns3/ndn-content-object.h"
-#include "ns3/ndn-forwarding-strategy.h"
-
#include "../../utils/trie/empty-policy.h"
#include "../../utils/trie/persistent-policy.h"
#include "../../utils/trie/random-policy.h"
#include "../../utils/trie/lru-policy.h"
+#include "../../utils/trie/multi-policy.h"
+#include "../../utils/trie/aggregate-stats-policy.h"
#include "ns3/log.h"
@@ -62,13 +60,6 @@
using namespace ndnSIM;
-template<class Policy>
-uint32_t
-PitImpl<Policy>::GetCurrentSize () const
-{
- return super::getPolicy ().size ();
-}
-
template<>
uint32_t
PitImpl<serialized_size_policy_traits>::GetCurrentSize () const
@@ -76,358 +67,10 @@
return super::getPolicy ().get_current_space_used ();
}
-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 ();
-}
-
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
-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>;
@@ -439,6 +82,28 @@
NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, lru_policy_traits);
NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, serialized_size_policy_traits);
+
+typedef multi_policy_traits< boost::mpl::vector2< persistent_policy_traits,
+ aggregate_stats_policy_traits > > PersistentWithCountsTraits;
+typedef multi_policy_traits< boost::mpl::vector2< random_policy_traits,
+ aggregate_stats_policy_traits > > RandomWithCountsTraits;
+typedef multi_policy_traits< boost::mpl::vector2< lru_policy_traits,
+ aggregate_stats_policy_traits > > LruWithCountsTraits;
+typedef multi_policy_traits< boost::mpl::vector2< serialized_size_policy_traits,
+ aggregate_stats_policy_traits > > SerializedSizeWithCountsTraits;
+
+template class PitImpl<PersistentWithCountsTraits>;
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, PersistentWithCountsTraits);
+
+template class PitImpl<RandomWithCountsTraits>;
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, RandomWithCountsTraits);
+
+template class PitImpl<LruWithCountsTraits>;
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, LruWithCountsTraits);
+
+template class PitImpl<SerializedSizeWithCountsTraits>;
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, SerializedSizeWithCountsTraits);
+
#ifdef DOXYGEN
// /**
// * \brief PIT in which new entries will be rejected if PIT size reached its limit