NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
probability-policy.h
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 University of California, Los Angeles
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19  */
20 
21 #ifndef PROBABILITY_POLICY_H_
22 #define PROBABILITY_POLICY_H_
23 
24 #include <boost/intrusive/options.hpp>
25 #include <boost/intrusive/list.hpp>
26 
27 #include <ns3/random-variable.h>
28 
29 namespace ns3 {
30 namespace ndn {
31 namespace ndnSIM {
32 
37 {
38  static std::string GetName () { return "ProbabilityImpl"; }
39 
40  struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
41 
42  template<class Container>
44  {
45  typedef boost::intrusive::member_hook< Container,
47  &Container::policy_hook_ > type;
48  };
49 
50  template<class Base,
51  class Container,
52  class Hook>
53  struct policy
54  {
55  typedef typename boost::intrusive::list< Container, Hook > policy_container;
56 
57  class type : public policy_container
58  {
59  public:
60  typedef policy policy_base; // to get access to get_freshness methods from outside
61  typedef Container parent_trie;
62 
63  type (Base &base)
64  : base_ (base)
65  , max_size_ (100)
66  , probability_ (1.0)
67  {
68  }
69 
70  inline void
71  update (typename parent_trie::iterator item)
72  {
73  }
74 
75  inline bool
76  insert (typename parent_trie::iterator item)
77  {
78  if (ns3_rand_.GetValue () < probability_)
79  {
80  policy_container::push_back (*item);
81 
82  // allow caching
83  return true;
84  }
85  else
86  {
87  // don't allow caching
88  return false;
89  }
90  }
91 
92  inline void
93  lookup (typename parent_trie::iterator item)
94  {
95  // do nothing. it's random policy
96  }
97 
98  inline void
99  erase (typename parent_trie::iterator item)
100  {
101  policy_container::erase (policy_container::s_iterator_to (*item));
102  }
103 
104  inline void
105  clear ()
106  {
107  policy_container::clear ();
108  }
109 
110  inline void
111  set_max_size (size_t max_size)
112  {
113  max_size_ = max_size;
114  }
115 
116  inline size_t
117  get_max_size () const
118  {
119  return max_size_;
120  }
121 
122  inline void
123  set_probability (double probability)
124  {
125  probability_ = probability;
126  }
127 
128  inline double
129  get_probability () const
130  {
131  return probability_;
132  }
133 
134  private:
135  type () : base_(*((Base*)0)) { };
136 
137  private:
138  Base &base_;
139  size_t max_size_;
140  double probability_;
141  UniformVariable ns3_rand_;
142  };
143  };
144 };
145 
146 } // ndnSIM
147 } // ndn
148 } // ns3
149 
150 #endif // PROBABILITY_POLICY_H