model: Adding experimental support for caching policies with probability caching

This caching can be enabled, for example, for LRU policy with caching
probability 50%:

    ndnHelper.SetContentStore ("ns3::ndn::cs::Probability::Lru",
                               "MaxSize", "100",
                               "CacheProbability", "0.5");

Similar way, probability can be enabled for other existing caching policies.
diff --git a/model/cs/content-store-with-probability.cc b/model/cs/content-store-with-probability.cc
new file mode 100644
index 0000000..f3def33
--- /dev/null
+++ b/model/cs/content-store-with-probability.cc
@@ -0,0 +1,98 @@
+/* -*-  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-probability.h"
+
+#include "../../utils/trie/random-policy.h"
+#include "../../utils/trie/lru-policy.h"
+#include "../../utils/trie/fifo-policy.h"
+#include "../../utils/trie/lfu-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 ContentStoreWithProbability<lru_policy_traits>;
+
+/**
+ * @brief ContentStore with freshness and random cache replacement policy
+ **/
+template class ContentStoreWithProbability<random_policy_traits>;
+
+/**
+ * @brief ContentStore with freshness and FIFO cache replacement policy
+ **/
+template class ContentStoreWithProbability<fifo_policy_traits>;
+
+/**
+ * @brief ContentStore with freshness and Least Frequently Used (LFU) cache replacement policy
+ **/
+template class ContentStoreWithProbability<lfu_policy_traits>;
+
+
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithProbability, lru_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithProbability, random_policy_traits);
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithProbability, fifo_policy_traits);
+
+NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreWithProbability, lfu_policy_traits);
+
+#ifdef DOXYGEN
+// /**
+//  * \brief Content Store with freshness implementing LRU cache replacement policy
+//  */
+class Probability::Lru : public ContentStoreWithProbability<lru_policy_traits> { };
+
+/**
+ * \brief Content Store with freshness implementing FIFO cache replacement policy
+ */
+class Probability::Fifo : public ContentStoreWithProbability<fifo_policy_traits> { };
+
+/**
+ * \brief Content Store with freshness implementing Random cache replacement policy
+ */
+class Probability::Random : public ContentStoreWithProbability<random_policy_traits> { };
+
+/**
+ * \brief Content Store with freshness implementing Least Frequently Used cache replacement policy
+ */
+class Probability::Lfu : public ContentStoreWithProbability<lfu_policy_traits> { };
+
+#endif
+
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
diff --git a/model/cs/content-store-with-probability.h b/model/cs/content-store-with-probability.h
new file mode 100644
index 0000000..42ad716
--- /dev/null
+++ b/model/cs/content-store-with-probability.h
@@ -0,0 +1,101 @@
+/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012-2013 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_PROBABILITY_H_
+#define NDN_CONTENT_STORE_WITH_PROBABILITY_H_
+
+#include "content-store-impl.h"
+
+#include "../../utils/trie/multi-policy.h"
+#include "custom-policies/probability-policy.h"
+#include "ns3/double.h"
+#include "ns3/type-id.h"
+
+namespace ns3 {
+namespace ndn {
+namespace cs {
+
+/**
+ * @ingroup ndn-cs
+ * @brief Special content store realization that honors Freshness parameter in Data packets
+ */
+template<class Policy>
+class ContentStoreWithProbability :
+    public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< ndnSIM::probability_policy_traits, Policy > > >
+{
+public:
+  typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< ndnSIM::probability_policy_traits, Policy > > > super;
+
+  typedef typename super::policy_container::template index<0>::type probability_policy_container;
+
+  ContentStoreWithProbability () {};
+  
+  static TypeId
+  GetTypeId ();
+private:
+
+  void SetCacheProbability (double probability)
+  {
+    this->getPolicy ()
+      .template get<probability_policy_container> ()
+      .set_probability (probability);
+  }
+
+  double
+  GetCacheProbability () const
+  {
+    return 
+      this->getPolicy ()
+      .template get<probability_policy_container> ()
+      .get_probability ();
+  }
+};
+
+//////////////////////////////////////////
+////////// Implementation ////////////////
+//////////////////////////////////////////
+
+template<class Policy>
+TypeId
+ContentStoreWithProbability< Policy >::GetTypeId ()
+{
+  static TypeId tid = TypeId (("ns3::ndn::cs::Probability::"+Policy::GetName ()).c_str ())
+    .SetGroupName ("Ndn")
+    .SetParent<super> ()
+    .template AddConstructor< ContentStoreWithProbability< Policy > > ()
+
+    .AddAttribute ("CacheProbability",
+                   "Set probability of caching in ContentStore. "
+                   "If 1, every content is cached. If 0, no content is cached.",
+                   DoubleValue (1.0),//(+)
+                   MakeDoubleAccessor (&ContentStoreWithProbability< Policy >::GetCacheProbability,
+                                       &ContentStoreWithProbability< Policy >::SetCacheProbability),
+                   MakeDoubleChecker<double> ())
+    ;
+
+  return tid;
+}
+
+
+} // namespace cs
+} // namespace ndn
+} // namespace ns3
+
+#endif // NDN_CONTENT_STORE_WITH_PROBABILITY_H_
diff --git a/model/cs/custom-policies/probability-policy.h b/model/cs/custom-policies/probability-policy.h
new file mode 100644
index 0000000..0c3088c
--- /dev/null
+++ b/model/cs/custom-policies/probability-policy.h
@@ -0,0 +1,150 @@
+/* -*-  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 PROBABILITY_POLICY_H_
+#define PROBABILITY_POLICY_H_
+
+#include <boost/intrusive/options.hpp>
+#include <boost/intrusive/list.hpp>
+
+#include <ns3/random-variable.h>
+
+namespace ns3 {
+namespace ndn {
+namespace ndnSIM {
+
+/**
+ * @brief Traits for freshness policy
+ */
+struct probability_policy_traits
+{
+  static std::string GetName () { return "ProbabilityImpl"; }
+
+  struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
+
+  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
+  {
+    typedef typename boost::intrusive::list< Container, Hook > policy_container;
+    
+    class type : public policy_container
+    {
+    public:
+      typedef policy policy_base; // to get access to get_freshness methods from outside
+      typedef Container parent_trie;
+
+      type (Base &base)
+        : base_ (base)
+        , max_size_ (100)
+        , probability_ (1.0)
+      {
+      }
+
+      inline void
+      update (typename parent_trie::iterator item)
+      {
+      }
+
+      inline bool
+      insert (typename parent_trie::iterator item)
+      {
+        if (ns3_rand_.GetValue () < probability_)
+          {
+            policy_container::push_back (*item);
+
+            // allow caching
+            return true;
+          }
+        else
+          {
+            // don't allow caching
+            return false;
+          }
+      }
+
+      inline void
+      lookup (typename parent_trie::iterator item)
+      {
+        // do nothing. it's random policy
+      }
+
+      inline void
+      erase (typename parent_trie::iterator 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 void
+      set_probability (double probability)
+      {
+        probability_ = probability;
+      }
+
+      inline double
+      get_probability () const
+      {
+        return probability_;
+      }      
+
+    private:
+      type () : base_(*((Base*)0)) { };
+
+    private:
+      Base &base_;
+      size_t max_size_;
+      double probability_;
+      UniformVariable ns3_rand_;
+    };
+  };
+};
+
+} // ndnSIM
+} // ndn
+} // ns3
+
+#endif // PROBABILITY_POLICY_H