blob: 6beaeba6dad8e5782fc8c050eb2d9befe9843589 [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
23#include "ns3/random-variable.h"
24
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070025#include <boost/intrusive/options.hpp>
26#include <boost/intrusive/set.hpp>
27
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028namespace ns3 {
29namespace ndn {
30namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070031
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070032/**
33 * @brief Traits for random replacement policy
34 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035struct random_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080036 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037 static std::string
38 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070039 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 return "Random";
41 }
42
43 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
44 uint32_t randomOrder;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070045 };
46
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 template<class Container>
48 struct container_hook {
49 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
50 type;
51 };
52
53 template<class Base, class Container, class Hook>
54 struct policy {
55 static uint32_t&
56 get_order(typename Container::iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070057 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058 return static_cast<typename policy_container::value_traits::hook_type*>(
59 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070060 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061
62 static const uint32_t&
63 get_order(typename Container::const_iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070064 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080065 return static_cast<const typename policy_container::value_traits::hook_type*>(
66 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070067 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070069 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 struct MemberHookLess {
71 bool
72 operator()(const Key& a, const Key& b) const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 return get_order(&a) < get_order(&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 }
76 };
77
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 typedef boost::intrusive::multiset<Container,
79 boost::intrusive::compare<MemberHookLess<Container>>,
80 Hook> policy_container;
81
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070082 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070084 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080085 typedef policy policy_base; // to get access to get_order methods from outside
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 typedef Container parent_trie;
87
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 type(Base& base)
89 : base_(base)
90 , u_rand(0, std::numeric_limits<uint32_t>::max())
91 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 {
93 }
94
95 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 {
98 // do nothing. it's random policy
99 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700101 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700103 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 get_order(item) = u_rand.GetValue();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700105
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 if (max_size_ != 0 && policy_container::size() >= max_size_) {
107 if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
108 // std::cout << "Cannot add. Signaling fail\n";
109 // just return false. Indicating that insert "failed"
110 return false;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700111 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 else {
113 // removing some random element
114 base_.erase(&(*policy_container::begin()));
115 }
116 }
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700117
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 policy_container::insert(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700119 return true;
120 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700122 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700124 {
125 // do nothing. it's random policy
126 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700128 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 erase(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700130 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700132 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700133
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700134 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 clear()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700136 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 policy_container::clear();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700138 }
139
140 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700142 {
143 max_size_ = max_size;
144 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700145
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700146 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700148 {
149 return max_size_;
150 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700151
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700152 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 type()
154 : base_(*((Base*)0)){};
155
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700156 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800157 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700158 ns3::UniformVariable u_rand;
159 size_t max_size_;
160 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700161 };
162};
163
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700164} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700166} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700167
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700168#endif // RANDOM_POLICY_H