NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
random-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef RANDOM_POLICY_H_
21 #define RANDOM_POLICY_H_
22 
24 
25 #include "ns3/random-variable.h"
26 
27 #include <boost/intrusive/options.hpp>
28 #include <boost/intrusive/set.hpp>
29 
30 namespace ns3 {
31 namespace ndn {
32 namespace ndnSIM {
33 
37 struct random_policy_traits {
39  static std::string
40  GetName()
41  {
42  return "Random";
43  }
44 
45  struct policy_hook_type : public boost::intrusive::set_member_hook<> {
46  uint32_t randomOrder;
47  };
48 
49  template<class Container>
50  struct container_hook {
51  typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
52  type;
53  };
54 
55  template<class Base, class Container, class Hook>
56  struct policy {
57  static uint32_t&
58  get_order(typename Container::iterator item)
59  {
60  return static_cast<typename policy_container::value_traits::hook_type*>(
61  policy_container::value_traits::to_node_ptr(*item))->randomOrder;
62  }
63 
64  static const uint32_t&
65  get_order(typename Container::const_iterator item)
66  {
67  return static_cast<const typename policy_container::value_traits::hook_type*>(
68  policy_container::value_traits::to_node_ptr(*item))->randomOrder;
69  }
70 
71  template<class Key>
72  struct MemberHookLess {
73  bool
74  operator()(const Key& a, const Key& b) const
75  {
76  return get_order(&a) < get_order(&b);
77  }
78  };
79 
80  typedef boost::intrusive::multiset<Container,
81  boost::intrusive::compare<MemberHookLess<Container>>,
82  Hook> policy_container;
83 
84  // could be just typedef
85  class type : public policy_container {
86  public:
87  typedef policy policy_base; // to get access to get_order methods from outside
88  typedef Container parent_trie;
89 
90  type(Base& base)
91  : base_(base)
92  , u_rand(0, std::numeric_limits<uint32_t>::max())
93  , max_size_(100)
94  {
95  }
96 
97  inline void
98  update(typename parent_trie::iterator item)
99  {
100  // do nothing. it's random policy
101  }
102 
103  inline bool
104  insert(typename parent_trie::iterator item)
105  {
106  get_order(item) = u_rand.GetValue();
107 
108  if (max_size_ != 0 && policy_container::size() >= max_size_) {
109  if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
110  // std::cout << "Cannot add. Signaling fail\n";
111  // just return false. Indicating that insert "failed"
112  return false;
113  }
114  else {
115  // removing some random element
116  base_.erase(&(*policy_container::begin()));
117  }
118  }
119 
120  policy_container::insert(*item);
121  return true;
122  }
123 
124  inline void
125  lookup(typename parent_trie::iterator item)
126  {
127  // do nothing. it's random policy
128  }
129 
130  inline void
131  erase(typename parent_trie::iterator item)
132  {
133  policy_container::erase(policy_container::s_iterator_to(*item));
134  }
135 
136  inline void
137  clear()
138  {
139  policy_container::clear();
140  }
141 
142  inline void
143  set_max_size(size_t max_size)
144  {
145  max_size_ = max_size;
146  }
147 
148  inline size_t
149  get_max_size() const
150  {
151  return max_size_;
152  }
153 
154  private:
155  type()
156  : 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 
171 
172 #endif // RANDOM_POLICY_H