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/apps/ndn-producer.cc b/apps/ndn-producer.cc
index 28c9551..4f7afc4 100644
--- a/apps/ndn-producer.cc
+++ b/apps/ndn-producer.cc
@@ -59,6 +59,10 @@
UintegerValue (1024),
MakeUintegerAccessor(&Producer::m_virtualPayloadSize),
MakeUintegerChecker<uint32_t>())
+ .AddAttribute ("Freshness", "Freshness of data packets, if 0, then unlimited freshness",
+ TimeValue (Seconds (0)),
+ MakeTimeAccessor (&Producer::m_freshness),
+ MakeTimeChecker ())
;
return tid;
@@ -114,6 +118,7 @@
static ContentObjectTail tail;
Ptr<ContentObjectHeader> header = Create<ContentObjectHeader> ();
header->SetName (Create<NameComponents> (interest->GetName ()));
+ header->SetFreshness (m_freshness);
NS_LOG_INFO ("node("<< GetNode()->GetId() <<") respodning with ContentObject:\n" << boost::cref(*header));
diff --git a/apps/ndn-producer.h b/apps/ndn-producer.h
index dc50ea7..dcc9581 100644
--- a/apps/ndn-producer.h
+++ b/apps/ndn-producer.h
@@ -61,6 +61,7 @@
private:
NameComponents m_prefix;
uint32_t m_virtualPayloadSize;
+ Time m_freshness;
};
} // namespace ndn
diff --git a/examples/custom-apps/custom-app.cc b/examples/custom-apps/custom-app.cc
index 06ec139..14c564e 100644
--- a/examples/custom-apps/custom-app.cc
+++ b/examples/custom-apps/custom-app.cc
@@ -33,7 +33,6 @@
#include "ns3/ndn-fib.h"
#include "ns3/random-variable.h"
-
NS_LOG_COMPONENT_DEFINE ("CustomApp");
namespace ns3 {
diff --git a/examples/custom-apps/dumb-requester.cc b/examples/custom-apps/dumb-requester.cc
new file mode 100644
index 0000000..1d068c4
--- /dev/null
+++ b/examples/custom-apps/dumb-requester.cc
@@ -0,0 +1,122 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-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>
+ */
+
+// dumb-requester.cc
+
+#include "dumb-requester.h"
+#include "ns3/ptr.h"
+#include "ns3/log.h"
+#include "ns3/simulator.h"
+#include "ns3/packet.h"
+#include "ns3/random-variable.h"
+#include "ns3/string.h"
+
+#include "ns3/ndn-app-face.h"
+#include "ns3/ndn-interest.h"
+#include "ns3/ndn-content-object.h"
+
+NS_LOG_COMPONENT_DEFINE ("DumbRequester");
+
+namespace ns3 {
+
+NS_OBJECT_ENSURE_REGISTERED (DumbRequester);
+
+// register NS-3 type
+TypeId
+DumbRequester::GetTypeId ()
+{
+ static TypeId tid = TypeId ("DumbRequester")
+ .SetParent<ndn::App> ()
+ .AddConstructor<DumbRequester> ()
+
+ .AddAttribute ("Prefix", "Requested name",
+ StringValue ("/dumb-interest"),
+ ndn::MakeNameComponentsAccessor (&DumbRequester::m_name),
+ ndn::MakeNameComponentsChecker ())
+ ;
+ return tid;
+}
+
+DumbRequester::DumbRequester ()
+ : m_isRunning (false)
+{
+}
+
+// Processing upon start of the application
+void
+DumbRequester::StartApplication ()
+{
+ // initialize ndn::App
+ ndn::App::StartApplication ();
+
+ m_isRunning = true;
+ Simulator::ScheduleNow (&DumbRequester::SendInterest, this);
+}
+
+// Processing when application is stopped
+void
+DumbRequester::StopApplication ()
+{
+ m_isRunning = false;
+ // cleanup ndn::App
+ ndn::App::StopApplication ();
+}
+
+void
+DumbRequester::SendInterest ()
+{
+ if (!m_isRunning) return;
+
+ /////////////////////////////////////
+ // Sending one Interest packet out //
+ /////////////////////////////////////
+
+ Ptr<ndn::NameComponents> prefix = Create<ndn::NameComponents> (m_name); // another way to create name
+
+ // Create and configure ndn::InterestHeader
+ ndn::InterestHeader interestHeader;
+ UniformVariable rand (0,std::numeric_limits<uint32_t>::max ());
+ interestHeader.SetNonce (rand.GetValue ());
+ interestHeader.SetName (prefix);
+ interestHeader.SetInterestLifetime (Seconds (1.0));
+
+ // Create packet and add ndn::InterestHeader
+ Ptr<Packet> packet = Create<Packet> ();
+ packet->AddHeader (interestHeader);
+
+ NS_LOG_DEBUG ("Sending Interest packet for " << *prefix);
+
+ // Forward packet to lower (network) layer
+ m_protocolHandler (packet);
+
+ // Call trace (for logging purposes)
+ m_transmittedInterests (&interestHeader, this, m_face);
+ Simulator::Schedule (Seconds (1.0), &DumbRequester::SendInterest, this);
+}
+
+void
+DumbRequester::OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject,
+ Ptr<Packet> payload)
+{
+ NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ());
+}
+
+
+} // namespace ns3
diff --git a/examples/custom-apps/dumb-requester.h b/examples/custom-apps/dumb-requester.h
new file mode 100644
index 0000000..050630d
--- /dev/null
+++ b/examples/custom-apps/dumb-requester.h
@@ -0,0 +1,69 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-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>
+ */
+
+// dumb-requester.h
+
+#ifndef DUMB_REQUESTER_H_
+#define DUMB_REQUESTER_H_
+
+#include "ns3/ndn-app.h"
+#include "ns3/ndn-name-components.h"
+
+namespace ns3 {
+
+/**
+ * @brief A dumb requester application
+ *
+ * This app keeps requesting every second the same content object
+ */
+class DumbRequester : public ndn::App
+{
+public:
+ // register NS-3 type "DumbRequester"
+ static TypeId
+ GetTypeId ();
+
+ DumbRequester ();
+
+ // (overridden from ndn::App) Processing upon start of the application
+ virtual void
+ StartApplication ();
+
+ // (overridden from ndn::App) Processing when application is stopped
+ virtual void
+ StopApplication ();
+
+ // (overridden from ndn::App) Callback that will be called when Data arrives
+ virtual void
+ OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject,
+ Ptr<Packet> payload);
+
+private:
+ void
+ SendInterest ();
+
+private:
+ bool m_isRunning;
+ ndn::NameComponents m_name;
+};
+
+} // namespace ns3
+
+#endif // DUMB_REQUESTER_H_
diff --git a/examples/custom-apps/hijacker.cc b/examples/custom-apps/hijacker.cc
new file mode 100644
index 0000000..252cf92
--- /dev/null
+++ b/examples/custom-apps/hijacker.cc
@@ -0,0 +1,74 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-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>
+ */
+
+// hijacker.cc
+
+#include "hijacker.h"
+
+NS_LOG_COMPONENT_DEFINE ("Hijacker");
+
+namespace ns3 {
+
+// Necessary if you are planning to use ndn::AppHelper
+NS_OBJECT_ENSURE_REGISTERED (Hijacker);
+
+TypeId
+Hijacker::GetTypeId ()
+{
+ static TypeId tid = TypeId ("Hijacker")
+ .SetParent<ndn::App> ()
+ .AddConstructor<Hijacker> ()
+ ;
+
+ return tid;
+}
+
+Hijacker::Hijacker ()
+{
+}
+
+void
+Hijacker::OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> packet)
+{
+ ndn::App::OnInterest (interest, packet); // forward call to perform app-level tracing
+ // do nothing else (hijack interest)
+
+ NS_LOG_DEBUG ("Do nothing for incoming interest for" << interest->GetName ());
+}
+
+void
+Hijacker::StartApplication ()
+{
+ App::StartApplication ();
+
+ // equivalent to setting interest filter for "/" prefix
+ Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> ();
+ Ptr<ndn::fib::Entry> fibEntry = fib->Add ("/", m_face, 0);
+ fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN);
+}
+
+void
+Hijacker::StopApplication ()
+{
+ App::StopApplication ();
+}
+
+} // namespace ns3
+
diff --git a/examples/custom-apps/hijacker.h b/examples/custom-apps/hijacker.h
new file mode 100644
index 0000000..84d9070
--- /dev/null
+++ b/examples/custom-apps/hijacker.h
@@ -0,0 +1,55 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-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>
+ */
+
+// hijacker.h
+
+#ifndef HIJACKER_H_
+#define HIJACKER_H_
+
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/ndnSIM-module.h"
+
+namespace ns3 {
+
+class Hijacker : public ndn::App
+{
+public:
+ static TypeId
+ GetTypeId ();
+
+ Hijacker ();
+
+ // Receive all Interests but do nothing in response
+ void
+ OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> packet);
+
+protected:
+ // inherited from Application base class.
+ virtual void
+ StartApplication ();
+
+ virtual void
+ StopApplication ();
+};
+
+} // namespace ns3
+
+#endif // HIJACKER_H_
diff --git a/examples/ndn-simple-with-content-freshness.cc b/examples/ndn-simple-with-content-freshness.cc
new file mode 100644
index 0000000..cefb232
--- /dev/null
+++ b/examples/ndn-simple-with-content-freshness.cc
@@ -0,0 +1,118 @@
+/* -*- 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>
+ */
+// ndn-simple.cc
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/point-to-point-module.h"
+#include "ns3/ndnSIM-module.h"
+
+using namespace ns3;
+
+/**
+ * This scenario simulates a very simple network topology:
+ *
+ *
+ * +----------+ 1Mbps +--------+ 1Mbps +----------+
+ * | consumer | <------------> | router | <------------> | producer |
+ * +----------+ 10ms +--------+ 10ms +----------+
+ *
+ * This scenario demonstrates how to use content store that responds to Freshness parameter set in ContentObjects.
+ * That is, if producer set Freshness field to 2 seconds, the corresponding content object will not be cached
+ * more than 2 seconds (can be cached for a shorter time, if entry is evicted earlier)
+ *
+ * NS_LOG=DumbRequester ./waf --run ndn-simple-with-content-freshness
+ */
+
+int
+main (int argc, char *argv[])
+{
+ // setting default parameters for PointToPoint links and channels
+ Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps"));
+ Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms"));
+ Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20"));
+
+ // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
+ CommandLine cmd;
+ cmd.Parse (argc, argv);
+
+ // Creating nodes
+ NodeContainer nodes;
+ nodes.Create (3);
+
+ // Connecting nodes using two links
+ PointToPointHelper p2p;
+ p2p.Install (nodes.Get (0), nodes.Get (1));
+ p2p.Install (nodes.Get (1), nodes.Get (2));
+
+ // Install CCNx stack on all nodes
+ ndn::StackHelper ccnxHelper;
+ ccnxHelper.SetDefaultRoutes (true);
+ ccnxHelper.SetContentStore ("ns3::ndn::cs::Freshness::Lru",
+ "MaxSize", "2"); // allow just 2 entries to be cached
+ ccnxHelper.InstallAll ();
+
+ // Installing applications
+
+ // Consumer
+ ndn::AppHelper consumerHelper ("DumbRequester");
+
+ // /*
+ // 1) at time 1 second requests Data from a producer that does not specify freshness
+ // 2) at time 10 seconds requests the same Data packet as client 1
+
+ // 3) at time 2 seconds requests Data from a producer that specifies freshness set to 2 seconds
+ // 4) at time 12 seconds requests the same Data packet as client 3
+
+ // Expectation:
+ // Interests from 1, 3 and 4 will reach producers
+ // Interset from 2 will be served from cache
+ // */
+
+ ApplicationContainer apps;
+
+ consumerHelper.SetPrefix ("/no-freshness");
+ apps = consumerHelper.Install (nodes.Get (0));
+ apps.Start (Seconds (0.1));
+ apps.Stop (Seconds (10.0));
+
+ consumerHelper.SetPrefix ("/with-freshness");
+ apps = consumerHelper.Install (nodes.Get (0));
+ apps.Start (Seconds (20.1));
+ apps.Stop (Seconds (30.0));
+
+ // Producer
+ ndn::AppHelper producerHelper ("ns3::ndn::Producer");
+ producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
+
+ producerHelper.SetAttribute ("Freshness", TimeValue (Seconds (0))); // unlimited freshness
+ producerHelper.SetPrefix ("/no-freshness");
+ producerHelper.Install (nodes.Get (2)); // last node
+
+ producerHelper.SetAttribute ("Freshness", TimeValue (Seconds (2.0))); // freshness 2 seconds (!!! freshness granularity is 1 seconds !!!)
+ producerHelper.SetPrefix ("/with-freshness");
+ producerHelper.Install (nodes.Get (2)); // last node
+
+ Simulator::Stop (Seconds (30.0));
+
+ Simulator::Run ();
+ Simulator::Destroy ();
+
+ return 0;
+}
diff --git a/examples/wscript b/examples/wscript
index cbd026c..13667ca 100644
--- a/examples/wscript
+++ b/examples/wscript
@@ -7,9 +7,14 @@
obj = bld.create_ns3_program('ndn-grid', ['ndnSIM', 'point-to-point-layout'])
obj.source = 'ndn-grid.cc'
+ obj = bld.create_ns3_program('ndn-simple-with-content-freshness', ['ndnSIM'])
+ obj.source = ['ndn-simple-with-content-freshness.cc',
+ 'custom-apps/dumb-requester.cc']
+
obj = bld.create_ns3_program('ndn-simple-with-custom-app', ['ndnSIM'])
obj.source = ['ndn-simple-with-custom-app.cc',
- 'custom-apps/custom-app.cc']
+ 'custom-apps/custom-app.cc',
+ 'custom-apps/hijacker.cc']
if 'topology' in bld.env['NDN_plugins']:
obj = bld.create_ns3_program('ndn-grid-topo-plugin', ['ndnSIM'])
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> { };
diff --git a/utils/trie/freshness-policy.h b/utils/trie/freshness-policy.h
new file mode 100644
index 0000000..eec8400
--- /dev/null
+++ b/utils/trie/freshness-policy.h
@@ -0,0 +1,167 @@
+/* -*- 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 FRESHNESS_POLICY_H_
+#define FRESHNESS_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+#include <ns3/nstime.h>
+#include <ns3/simulator.h>
+#include <ns3/traced-callback.h>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+
+/**
+ * @brief Traits for freshness policy
+ */
+struct freshness_policy_traits
+{
+ /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
+ static std::string GetName () { return "Freshness"; }
+
+ struct policy_hook_type : public boost::intrusive::set_member_hook<> { Time timeWhenShouldExpire; };
+
+ 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 Time& get_freshness (typename Container::iterator item)
+ {
+ return static_cast<typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
+ }
+
+ static const Time& get_freshness (typename Container::const_iterator item)
+ {
+ return static_cast<const typename policy_container::value_traits::hook_type*>
+ (policy_container::value_traits::to_node_ptr(*item))->timeWhenShouldExpire;
+ }
+
+ template<class Key>
+ struct MemberHookLess
+ {
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return get_freshness (&a) < get_freshness (&b);
+ }
+ };
+
+ typedef boost::intrusive::multiset< Container,
+ boost::intrusive::compare< MemberHookLess< Container > >,
+ Hook > policy_container;
+
+
+ class type : public policy_container
+ {
+ public:
+ typedef policy policy; // to get access to get_freshness methods from outside
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ // get_time (item) = Simulator::Now ();
+ Time freshness = item->payload ()->GetHeader ()->GetFreshness ();
+ if (!freshness.IsZero ())
+ {
+ get_freshness (item) = Simulator::Now () + freshness;
+
+ // push item only if freshness is non zero. otherwise, this payload is not controlled by the policy
+ // note that .size() on this policy would return only number of items with non-infinite freshness policy
+ policy_container::push_back (*item);
+ }
+
+ return true;
+ }
+
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
+
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ if (!item->payload ()->GetHeader ()->GetFreshness ().IsZero ())
+ {
+ // erase only if freshness is non zero (otherwise an item is not in the policy
+ 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_;
+ }
+
+ private:
+ type () : base_(*((Base*)0)) { };
+
+ private:
+ Base &base_;
+ size_t max_size_;
+ };
+ };
+};
+
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // LIFETIME_STATS_POLICY_H
diff --git a/utils/trie/lifetime-stats-policy.h b/utils/trie/lifetime-stats-policy.h
index 87a5891..ade48cc 100644
--- a/utils/trie/lifetime-stats-policy.h
+++ b/utils/trie/lifetime-stats-policy.h
@@ -72,6 +72,7 @@
class type : public policy_container
{
public:
+ typedef policy policy; // to get access to get_time methods from outside
typedef Container parent_trie;
type (Base &base)
diff --git a/utils/trie/random-policy.h b/utils/trie/random-policy.h
index 1800f95..b5e1d8a 100644
--- a/utils/trie/random-policy.h
+++ b/utils/trie/random-policy.h
@@ -82,6 +82,7 @@
class type : public policy_container
{
public:
+ typedef policy policy; // to get access to get_order methods from outside
typedef Container parent_trie;
type (Base &base)