NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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 #include "ns3/double.h"
27 
28 #include <boost/intrusive/options.hpp>
29 #include <boost/intrusive/set.hpp>
30 
31 namespace ns3 {
32 namespace ndn {
33 namespace ndnSIM {
34 
38 struct random_policy_traits {
40  static std::string
41  GetName()
42  {
43  return "Random";
44  }
45 
46  struct policy_hook_type : public boost::intrusive::set_member_hook<> {
47  uint32_t randomOrder;
48  };
49 
50  template<class Container>
51  struct container_hook {
52  typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
53  type;
54  };
55 
56  template<class Base, class Container, class Hook>
57  struct policy {
58  static uint32_t&
59  get_order(typename Container::iterator item)
60  {
61  return static_cast<typename policy_container::value_traits::hook_type*>(
62  policy_container::value_traits::to_node_ptr(*item))->randomOrder;
63  }
64 
65  static const uint32_t&
66  get_order(typename Container::const_iterator item)
67  {
68  return static_cast<const typename policy_container::value_traits::hook_type*>(
69  policy_container::value_traits::to_node_ptr(*item))->randomOrder;
70  }
71 
72  template<class Key>
73  struct MemberHookLess {
74  bool
75  operator()(const Key& a, const Key& b) const
76  {
77  return get_order(&a) < get_order(&b);
78  }
79  };
80 
81  typedef boost::intrusive::multiset<Container,
82  boost::intrusive::compare<MemberHookLess<Container>>,
83  Hook> policy_container;
84 
85  // could be just typedef
86  class type : public policy_container {
87  public:
88  typedef policy policy_base; // to get access to get_order methods from outside
89  typedef Container parent_trie;
90 
91  type(Base& base)
92  : base_(base)
93  , u_rand(CreateObject<UniformRandomVariable>())
94  , max_size_(100)
95  {
96  u_rand->SetAttribute("Min", DoubleValue(0));
97  u_rand->SetAttribute("Max", DoubleValue(std::numeric_limits<uint32_t>::max()));
98  }
99 
100  inline void
101  update(typename parent_trie::iterator item)
102  {
103  // do nothing. it's random policy
104  }
105 
106  inline bool
107  insert(typename parent_trie::iterator item)
108  {
109  get_order(item) = u_rand->GetValue();
110 
111  if (max_size_ != 0 && policy_container::size() >= max_size_) {
112  if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
113  // std::cout << "Cannot add. Signaling fail\n";
114  // just return false. Indicating that insert "failed"
115  return false;
116  }
117  else {
118  // removing some random element
119  base_.erase(&(*policy_container::begin()));
120  }
121  }
122 
123  policy_container::insert(*item);
124  return true;
125  }
126 
127  inline void
128  lookup(typename parent_trie::iterator item)
129  {
130  // do nothing. it's random policy
131  }
132 
133  inline void
134  erase(typename parent_trie::iterator item)
135  {
136  policy_container::erase(policy_container::s_iterator_to(*item));
137  }
138 
139  inline void
140  clear()
141  {
142  policy_container::clear();
143  }
144 
145  inline void
146  set_max_size(size_t max_size)
147  {
148  max_size_ = max_size;
149  }
150 
151  inline size_t
152  get_max_size() const
153  {
154  return max_size_;
155  }
156 
157  private:
158  type()
159  : base_(*((Base*)0)){};
160 
161  private:
162  Base& base_;
163  Ptr<UniformRandomVariable> u_rand;
164  size_t max_size_;
165  };
166  };
167 };
168 
169 } // ndnSIM
170 } // ndn
171 } // ns3
172 
174 
175 #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.