NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
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-stream.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(CreateObject<UniformRandomVariable>())
93  , max_size_(100)
94  {
95  u_rand->SetAttribute("Min", UintegerValue(0));
96  u_rand->SetAttribute("Max", UintegerValue(std::numeric_limits<uint32_t>::max()));
97  }
98 
99  inline void
100  update(typename parent_trie::iterator item)
101  {
102  // do nothing. it's random policy
103  }
104 
105  inline bool
106  insert(typename parent_trie::iterator item)
107  {
108  get_order(item) = u_rand->GetValue();
109 
110  if (max_size_ != 0 && policy_container::size() >= max_size_) {
111  if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
112  // std::cout << "Cannot add. Signaling fail\n";
113  // just return false. Indicating that insert "failed"
114  return false;
115  }
116  else {
117  // removing some random element
118  base_.erase(&(*policy_container::begin()));
119  }
120  }
121 
122  policy_container::insert(*item);
123  return true;
124  }
125 
126  inline void
127  lookup(typename parent_trie::iterator item)
128  {
129  // do nothing. it's random policy
130  }
131 
132  inline void
133  erase(typename parent_trie::iterator item)
134  {
135  policy_container::erase(policy_container::s_iterator_to(*item));
136  }
137 
138  inline void
139  clear()
140  {
141  policy_container::clear();
142  }
143 
144  inline void
145  set_max_size(size_t max_size)
146  {
147  max_size_ = max_size;
148  }
149 
150  inline size_t
151  get_max_size() const
152  {
153  return max_size_;
154  }
155 
156  private:
157  type()
158  : base_(*((Base*)0)){};
159 
160  private:
161  Base& base_;
162  Ptr<UniformRandomVariable> u_rand;
163  size_t max_size_;
164  };
165  };
166 };
167 
168 } // ndnSIM
169 } // ndn
170 } // ns3
171 
173 
174 #endif // RANDOM_POLICY_H
Copyright (c) 2011-2015 Regents of the University of California.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.