model: New content store variations: support for content freshness

The following content store realizations now fully support freshness set
in ContentObjects:
- ns3::ndn::cs::Freshness::Lru
- ns3::ndn::cs::Freshness::Random
- ns3::ndn::cs::Freshness::Fifo

Example ndn-simple-with-content-freshness demonstrates basics of new
content stores.
diff --git a/model/cs/content-store-with-freshness.cc b/model/cs/content-store-with-freshness.cc
new file mode 100644
index 0000000..67030d1
--- /dev/null
+++ b/model/cs/content-store-with-freshness.cc
@@ -0,0 +1,85 @@
+/* -*-  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>
+ */
+
+#include "content-store-with-freshness.h"
+
+#include "../../utils/trie/random-policy.h"
+#include "../../utils/trie/lru-policy.h"
+#include "../../utils/trie/fifo-policy.h"
+
+#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ)  \
+  static struct X ## type ## templ ## RegistrationClass \
+  {                                                     \
+    X ## type ## templ ## RegistrationClass () {        \
+      ns3::TypeId tid = type<templ>::GetTypeId ();      \
+      tid.GetParent ();                                 \
+    }                                                   \
+  } x_ ## type ## templ ## RegistrationVariable
+
+namespace ns3 {
+namespace ndn {
+
+using namespace ndnSIM;
+
+namespace cs {
+
+// explicit instantiation and registering
+/**
+ * @brief ContentStore with freshness and LRU cache replacement policy
+ **/
+template class ContentStoreWithFreshness<lru_policy_traits>;
+
+/**
+ * @brief ContentStore with freshness and random cache replacement policy
+ **/
+template class ContentStoreWithFreshness<random_policy_traits>;
+
+/**
+ * @brief ContentStore with freshness and FIFO cache replacement policy
+ **/
+template class ContentStoreWithFreshness<fifo_policy_traits>;
+
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, lru_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, random_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithFreshness, fifo_policy_traits);
+
+
+#ifdef DOXYGEN
+// /**
+//  * \brief Content Store with freshness implementing LRU cache replacement policy
+//  */
+class Freshness::Lru : public ContentStoreWithFreshness<lru_policy_traits> { };
+
+/**
+ * \brief Content Store with freshness implementing FIFO cache replacement policy
+ */
+class Freshness::Fifo : public ContentStoreWithFreshness<fifo_policy_traits> { };
+
+/**
+ * \brief Content Store with freshness implementing Random cache replacement policy
+ */
+class Freshness::Random : public ContentStoreWithFreshness<random_policy_traits> { };
+
+#endif
+
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
diff --git a/model/cs/content-store-with-freshness.h b/model/cs/content-store-with-freshness.h
new file mode 100644
index 0000000..c15ded6
--- /dev/null
+++ b/model/cs/content-store-with-freshness.h
@@ -0,0 +1,164 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 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 NDN_CONTENT_STORE_WITH_FRESHNESS_H_
+#define NDN_CONTENT_STORE_WITH_FRESHNESS_H_
+
+#include "content-store-impl.h"
+
+#include "../../utils/trie/multi-policy.h"
+#include "../../utils/trie/freshness-policy.h"
+
+namespace ns3 {
+namespace ndn {
+namespace cs {
+
+template<class Policy>
+class ContentStoreWithFreshness :
+    public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
+{
+public:
+  typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super;
+
+  typedef typename super::policy_container::template index<1>::type freshness_policy_container;
+
+  static TypeId
+  GetTypeId ();
+  
+  virtual inline bool
+  Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
+
+private:
+  inline void
+  CleanExpired ();
+
+  inline void
+  RescheduleCleaning ();
+  
+private:
+  static LogComponent g_log; ///< @brief Logging variable
+
+  EventId m_cleanEvent;
+  Time m_scheduledCleaningTime;
+};
+
+//////////////////////////////////////////
+////////// Implementation ////////////////
+//////////////////////////////////////////
+
+
+template<class Policy>
+LogComponent
+ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
+
+
+template<class Policy>
+TypeId
+ContentStoreWithFreshness< Policy >::GetTypeId ()
+{
+  static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ())
+    .SetGroupName ("Ndn")
+    .SetParent<super> ()
+    .template AddConstructor< ContentStoreWithFreshness< Policy > > ()
+
+    // trace stuff here
+    ;
+
+  return tid;
+}
+
+
+template<class Policy>
+inline bool
+ContentStoreWithFreshness< Policy >::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
+{
+  bool ok = super::Add (header, packet);
+  if (!ok) return false;
+
+  NS_LOG_DEBUG (header->GetName () << " added to cache");
+  RescheduleCleaning ();
+  return true;
+}
+
+template<class Policy>
+inline void
+ContentStoreWithFreshness< Policy >::RescheduleCleaning ()
+{
+  const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
+
+  if (freshness.size () > 0)
+    {
+      Time nextStateTime = freshness_policy_container::policy::get_freshness (&(*freshness.begin ()));
+
+      if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled
+          m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
+        {
+          if (m_cleanEvent.IsRunning ())
+            {
+              Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
+            }
+
+          // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
+          m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this);
+          m_scheduledCleaningTime = nextStateTime;
+        }
+    }
+  else
+    {
+      if (m_cleanEvent.IsRunning ())
+        {
+          Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
+        }
+    }
+}
+
+
+template<class Policy>
+inline void
+ContentStoreWithFreshness< Policy >::CleanExpired ()
+{
+  freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
+
+  // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
+  Time now = Simulator::Now ();
+
+  while (!freshness.empty ())
+    {
+      typename freshness_policy_container::iterator entry = freshness.begin ();
+
+      if (freshness_policy_container::policy::get_freshness (&(*entry)) <= now) // is the record stale?
+        {
+          super::erase (&(*entry));
+        }
+      else
+        break; // nothing else to do. All later records will not be stale
+    }
+  // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
+
+  m_scheduledCleaningTime = Time ();
+  RescheduleCleaning ();
+}
+
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_
diff --git a/model/cs/content-store-with-stats.cc b/model/cs/content-store-with-stats.cc
index 1e39255..828cbb1 100644
--- a/model/cs/content-store-with-stats.cc
+++ b/model/cs/content-store-with-stats.cc
@@ -63,17 +63,17 @@
 
 #ifdef DOXYGEN
 // /**
-//  * \brief Content Store implementing LRU cache replacement policy
+//  * \brief Content Store with stats implementing LRU cache replacement policy
 //  */
 class Stats::Lru : public ContentStoreWithStats<lru_policy_traits> { };
 
 /**
- * \brief Content Store implementing FIFO cache replacement policy
+ * \brief Content Store with stats implementing FIFO cache replacement policy
  */
 class Stats::Fifo : public ContentStoreWithStats<fifo_policy_traits> { };
 
 /**
- * \brief Content Store implementing Random cache replacement policy
+ * \brief Content Store with stats implementing Random cache replacement policy
  */
 class Stats::Random : public ContentStoreWithStats<random_policy_traits> { };