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/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'])