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
random-policy.h
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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 RANDOM_POLICY_H_
22 #define RANDOM_POLICY_H_
23 
24 #include "ns3/random-variable.h"
25 
26 #include <boost/intrusive/options.hpp>
27 #include <boost/intrusive/set.hpp>
28 
29 namespace ns3 {
30 namespace ndn {
31 namespace ndnSIM {
32 
37 {
39  static std::string GetName () { return "Random"; }
40 
41  struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
42 
43  template<class Container>
45  {
46  typedef boost::intrusive::member_hook< Container,
48  &Container::policy_hook_ > type;
49  };
50 
51  template<class Base,
52  class Container,
53  class Hook>
54  struct policy
55  {
56  static uint32_t& get_order (typename Container::iterator item)
57  {
58  return static_cast<typename policy_container::value_traits::hook_type*>
59  (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
60  }
61 
62  static const uint32_t& get_order (typename Container::const_iterator item)
63  {
64  return static_cast<const typename policy_container::value_traits::hook_type*>
65  (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
66  }
67 
68  template<class Key>
70  {
71  bool operator () (const Key &a, const Key &b) const
72  {
73  return get_order (&a) < get_order (&b);
74  }
75  };
76 
77  typedef boost::intrusive::multiset< Container,
78  boost::intrusive::compare< MemberHookLess< Container > >,
79  Hook > policy_container;
80 
81  // could be just typedef
82  class type : public policy_container
83  {
84  public:
85  typedef policy policy_base; // to get access to get_order methods from outside
86  typedef Container parent_trie;
87 
88  type (Base &base)
89  : base_ (base)
90  , u_rand (0, std::numeric_limits<uint32_t>::max ())
91  , max_size_ (100)
92  {
93  }
94 
95  inline void
96  update (typename parent_trie::iterator item)
97  {
98  // do nothing. it's random policy
99  }
100 
101  inline bool
102  insert (typename parent_trie::iterator item)
103  {
104  get_order (item) = u_rand.GetValue ();
105 
106  if (max_size_ != 0 && policy_container::size () >= max_size_)
107  {
108  if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
109  {
110  // std::cout << "Cannot add. Signaling fail\n";
111  // just return false. Indicating that insert "failed"
112  return false;
113  }
114  else
115  {
116  // removing some random element
117  base_.erase (&(*policy_container::begin ()));
118  }
119  }
120 
121  policy_container::insert (*item);
122  return true;
123  }
124 
125  inline void
126  lookup (typename parent_trie::iterator item)
127  {
128  // do nothing. it's random policy
129  }
130 
131  inline void
132  erase (typename parent_trie::iterator item)
133  {
134  policy_container::erase (policy_container::s_iterator_to (*item));
135  }
136 
137  inline void
138  clear ()
139  {
140  policy_container::clear ();
141  }
142 
143  inline void
144  set_max_size (size_t max_size)
145  {
146  max_size_ = max_size;
147  }
148 
149  inline size_t
150  get_max_size () const
151  {
152  return max_size_;
153  }
154 
155  private:
156  type () : base_(*((Base*)0)) { };
157 
158  private:
159  Base &base_;
160  ns3::UniformVariable u_rand;
161  size_t max_size_;
162  };
163  };
164 };
165 
166 } // ndnSIM
167 } // ndn
168 } // ns3
169 
170 #endif // RANDOM_POLICY_H
static std::string GetName()
Name that can be used to identify the policy (for NS-3 object model and logging)
Definition: random-policy.h:39
Traits for random replacement policy.
Definition: random-policy.h:36