blob: 7264bed195ec469c609ca7391f00af652eda927a [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef RANDOM_POLICY_H_
22#define RANDOM_POLICY_H_
23
24#include "ns3/random-variable.h"
25
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070026#include <boost/intrusive/options.hpp>
27#include <boost/intrusive/set.hpp>
28
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070029namespace ns3 {
30namespace ndn {
31namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070032
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070033/**
34 * @brief Traits for random replacement policy
35 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036struct random_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080037 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 static std::string
39 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070040 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 return "Random";
42 }
43
44 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
45 uint32_t randomOrder;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070046 };
47
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 template<class Container>
49 struct container_hook {
50 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
51 type;
52 };
53
54 template<class Base, class Container, class Hook>
55 struct policy {
56 static uint32_t&
57 get_order(typename Container::iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070058 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 return static_cast<typename policy_container::value_traits::hook_type*>(
60 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070061 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062
63 static const uint32_t&
64 get_order(typename Container::const_iterator item)
Alexander Afanasyev903062f2012-07-04 18:25:26 -070065 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 return static_cast<const typename policy_container::value_traits::hook_type*>(
67 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
Alexander Afanasyev903062f2012-07-04 18:25:26 -070068 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070070 template<class Key>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 struct MemberHookLess {
72 bool
73 operator()(const Key& a, const Key& b) const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070074 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 return get_order(&a) < get_order(&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076 }
77 };
78
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 typedef boost::intrusive::multiset<Container,
80 boost::intrusive::compare<MemberHookLess<Container>>,
81 Hook> policy_container;
82
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070083 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 public:
Alexander Afanasyev69786112012-12-13 11:53:36 -080086 typedef policy policy_base; // to get access to get_order methods from outside
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070087 typedef Container parent_trie;
88
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 type(Base& base)
90 : base_(base)
91 , u_rand(0, std::numeric_limits<uint32_t>::max())
92 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070093 {
94 }
95
96 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 {
99 // do nothing. it's random policy
100 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700102 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 get_order(item) = u_rand.GetValue();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 if (max_size_ != 0 && policy_container::size() >= max_size_) {
108 if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
109 // std::cout << "Cannot add. Signaling fail\n";
110 // just return false. Indicating that insert "failed"
111 return false;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700112 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 else {
114 // removing some random element
115 base_.erase(&(*policy_container::begin()));
116 }
117 }
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 policy_container::insert(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700120 return true;
121 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700123 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800124 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700125 {
126 // do nothing. it's random policy
127 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700129 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 erase(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700131 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700133 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700134
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700135 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 clear()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700137 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 policy_container::clear();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700139 }
140
141 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700143 {
144 max_size_ = max_size;
145 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700146
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700147 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700149 {
150 return max_size_;
151 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700152
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700153 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 type()
155 : base_(*((Base*)0)){};
156
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700157 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700159 ns3::UniformVariable u_rand;
160 size_t max_size_;
161 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700162 };
163};
164
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700165} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700166} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700167} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700168
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700169#endif // RANDOM_POLICY_H