blob: 1a7308cc499a481a304aceff9af888598ad68c34 [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 Afanasyev9a989702012-06-29 17:44:00 -070036struct random_policy_traits
37{
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)
39 static std::string GetName () { return "Random"; }
40
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070041 struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070042
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043 template<class Container>
44 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070045 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070046 typedef boost::intrusive::member_hook< Container,
47 policy_hook_type,
48 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070049 };
50
51 template<class Base,
52 class Container,
53 class Hook>
54 struct policy
55 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070056 static uint32_t& get_order (typename Container::iterator item)
57 {
58 return static_cast<typename policy_container::value_traits::hook_type*>
59 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
60 }
61
62 static const uint32_t& get_order (typename Container::const_iterator item)
63 {
64 return static_cast<const typename policy_container::value_traits::hook_type*>
65 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
66 }
67
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070068 template<class Key>
69 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070070 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070071 bool operator () (const Key &a, const Key &b) const
72 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070073 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070074 }
75 };
76
Alexander Afanasyevad82e252012-07-29 23:51:21 -070077 typedef boost::intrusive::multiset< Container,
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070078 boost::intrusive::compare< MemberHookLess< Container > >,
79 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070080
81 // could be just typedef
82 class type : public policy_container
83 {
84 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
88 type (Base &base)
89 : base_ (base)
90 , u_rand (0, std::numeric_limits<uint32_t>::max ())
91 , max_size_ (100)
92 {
93 }
94
95 inline void
96 update (typename parent_trie::iterator item)
97 {
98 // do nothing. it's random policy
99 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700100
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700101 inline bool
102 insert (typename parent_trie::iterator item)
103 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700104 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700105
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -0700106 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 {
108 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
109 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700110 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700111 // just return false. Indicating that insert "failed"
112 return false;
113 }
114 else
115 {
116 // removing some random element
117 base_.erase (&(*policy_container::begin ()));
118 }
119 }
120
121 policy_container::insert (*item);
122 return true;
123 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700124
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700125 inline void
126 lookup (typename parent_trie::iterator item)
127 {
128 // do nothing. it's random policy
129 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700130
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700131 inline void
132 erase (typename parent_trie::iterator item)
133 {
134 policy_container::erase (policy_container::s_iterator_to (*item));
135 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700136
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700137 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700138 clear ()
139 {
140 policy_container::clear ();
141 }
142
143 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700144 set_max_size (size_t max_size)
145 {
146 max_size_ = max_size;
147 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700148
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700149 inline size_t
150 get_max_size () const
151 {
152 return max_size_;
153 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700154
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700155 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700156 type () : base_(*((Base*)0)) { };
157
158 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700159 Base &base_;
160 ns3::UniformVariable u_rand;
161 size_t max_size_;
162 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700163 };
164};
165
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700166} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700168} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700169
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700170#endif // RANDOM_POLICY_H