blob: a6e35176ad0e528d72d99095c18eec78043da4c3 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev9a989702012-06-29 17:44:00 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev9a989702012-06-29 17:44:00 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev9a989702012-06-29 17:44:00 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev9a989702012-06-29 17:44:00 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev9a989702012-06-29 17:44:00 -070019
20#ifndef RANDOM_POLICY_H_
21#define RANDOM_POLICY_H_
22
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070025#include "ns3/random-variable-stream.h"
Alexander Afanasyevb4102ce2016-06-26 21:37:40 -070026#include "ns3/double.h"
Alexander Afanasyev9a989702012-06-29 17:44:00 -070027
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070028#include <boost/intrusive/options.hpp>
29#include <boost/intrusive/set.hpp>
30
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070031namespace ns3 {
32namespace ndn {
33namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070034
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070035/**
36 * @brief Traits for random replacement policy
37 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038struct random_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080039 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 static std::string
41 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070042 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 return "Random";
44 }
45
46 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
47 uint32_t randomOrder;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070048 };
49
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 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)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070060 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 return static_cast<typename policy_container::value_traits::hook_type*>(
62 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070063 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064
65 static const uint32_t&
66 get_order(typename Container::const_iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070067 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 return static_cast<const typename policy_container::value_traits::hook_type*>(
69 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070070 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070072 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 struct MemberHookLess {
74 bool
75 operator()(const Key& a, const Key& b) const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 return get_order(&a) < get_order(&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070078 }
79 };
80
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 typedef boost::intrusive::multiset<Container,
82 boost::intrusive::compare<MemberHookLess<Container>>,
83 Hook> policy_container;
84
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070087 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080088 typedef policy policy_base; // to get access to get_order methods from outside
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070089 typedef Container parent_trie;
90
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 type(Base& base)
92 : base_(base)
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070093 , u_rand(CreateObject<UniformRandomVariable>())
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070095 {
Alexander Afanasyevb4102ce2016-06-26 21:37:40 -070096 u_rand->SetAttribute("Min", DoubleValue(0));
97 u_rand->SetAttribute("Max", DoubleValue(std::numeric_limits<uint32_t>::max()));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 }
99
100 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700102 {
103 // do nothing. it's random policy
104 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700106 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700108 {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700109 get_order(item) = u_rand->GetValue();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700110
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 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;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700116 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 else {
118 // removing some random element
119 base_.erase(&(*policy_container::begin()));
120 }
121 }
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700122
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 policy_container::insert(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700124 return true;
125 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700127 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700129 {
130 // do nothing. it's random policy
131 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700133 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 erase(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700135 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700137 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700138
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700139 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 clear()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700141 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 policy_container::clear();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700143 }
144
145 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700147 {
148 max_size_ = max_size;
149 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700150
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700151 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700153 {
154 return max_size_;
155 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700156
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700157 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 type()
159 : base_(*((Base*)0)){};
160
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700161 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 Base& base_;
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700163 Ptr<UniformRandomVariable> u_rand;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700164 size_t max_size_;
165 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700166 };
167};
168
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700169} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700170} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700171} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700172
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800173/// @endcond
174
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700175#endif // RANDOM_POLICY_H