blob: 1e2aa4a43e4dcfb2ad20e10a07facbcbc795a0fd [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 Afanasyev9a989702012-06-29 17:44:00 -070026
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070027#include <boost/intrusive/options.hpp>
28#include <boost/intrusive/set.hpp>
29
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070030namespace ns3 {
31namespace ndn {
32namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070033
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070034/**
35 * @brief Traits for random replacement policy
36 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037struct random_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080038 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 static std::string
40 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070041 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 return "Random";
43 }
44
45 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
46 uint32_t randomOrder;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070047 };
48
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 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)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070059 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 return static_cast<typename policy_container::value_traits::hook_type*>(
61 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070062 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063
64 static const uint32_t&
65 get_order(typename Container::const_iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070066 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 return static_cast<const typename policy_container::value_traits::hook_type*>(
68 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070069 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070071 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 struct MemberHookLess {
73 bool
74 operator()(const Key& a, const Key& b) const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 return get_order(&a) < get_order(&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077 }
78 };
79
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 typedef boost::intrusive::multiset<Container,
81 boost::intrusive::compare<MemberHookLess<Container>>,
82 Hook> policy_container;
83
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070084 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080087 typedef policy policy_base; // to get access to get_order methods from outside
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070088 typedef Container parent_trie;
89
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 type(Base& base)
91 : base_(base)
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070092 , u_rand(CreateObject<UniformRandomVariable>())
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070094 {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070095 u_rand->SetAttribute("Min", UintegerValue(0));
96 u_rand->SetAttribute("Max", UintegerValue(std::numeric_limits<uint32_t>::max()));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 }
98
99 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700101 {
102 // do nothing. it's random policy
103 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700105 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 {
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700108 get_order(item) = u_rand->GetValue();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700109
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 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;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700115 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 else {
117 // removing some random element
118 base_.erase(&(*policy_container::begin()));
119 }
120 }
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700121
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 policy_container::insert(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700123 return true;
124 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700126 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700128 {
129 // do nothing. it's random policy
130 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700132 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 erase(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700134 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700136 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700137
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700138 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 clear()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700140 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 policy_container::clear();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700142 }
143
144 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700146 {
147 max_size_ = max_size;
148 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700149
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700150 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700152 {
153 return max_size_;
154 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700155
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700156 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800157 type()
158 : base_(*((Base*)0)){};
159
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700160 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 Base& base_;
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -0700162 Ptr<UniformRandomVariable> u_rand;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700163 size_t max_size_;
164 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700165 };
166};
167
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700168} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700169} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700170} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700171
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800172/// @endcond
173
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700174#endif // RANDOM_POLICY_H