blob: 9e0dde629bc5de8d2c36b80da08163b24f09f277 [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 Afanasyev30cb1172012-07-06 10:47:39 -070038 struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070039
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070040 template<class Container>
41 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070042 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070043 typedef boost::intrusive::member_hook< Container,
44 policy_hook_type,
45 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070046 };
47
48 template<class Base,
49 class Container,
50 class Hook>
51 struct policy
52 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070053 static uint32_t& get_order (typename Container::iterator item)
54 {
55 return static_cast<typename policy_container::value_traits::hook_type*>
56 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
57 }
58
59 static const uint32_t& get_order (typename Container::const_iterator item)
60 {
61 return static_cast<const typename policy_container::value_traits::hook_type*>
62 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
63 }
64
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070065 template<class Key>
66 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070067 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070068 bool operator () (const Key &a, const Key &b) const
69 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070070 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070071 }
72 };
73
Alexander Afanasyevad82e252012-07-29 23:51:21 -070074 typedef boost::intrusive::multiset< Container,
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070075 boost::intrusive::compare< MemberHookLess< Container > >,
76 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077
78 // could be just typedef
79 class type : public policy_container
80 {
81 public:
82 typedef Container parent_trie;
83
84 type (Base &base)
85 : base_ (base)
86 , u_rand (0, std::numeric_limits<uint32_t>::max ())
87 , max_size_ (100)
88 {
89 }
90
91 inline void
92 update (typename parent_trie::iterator item)
93 {
94 // do nothing. it's random policy
95 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070096
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 inline bool
98 insert (typename parent_trie::iterator item)
99 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700100 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700101
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -0700102 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700103 {
104 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
105 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700106 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 // just return false. Indicating that insert "failed"
108 return false;
109 }
110 else
111 {
112 // removing some random element
113 base_.erase (&(*policy_container::begin ()));
114 }
115 }
116
117 policy_container::insert (*item);
118 return true;
119 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700120
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700121 inline void
122 lookup (typename parent_trie::iterator item)
123 {
124 // do nothing. it's random policy
125 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700126
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700127 inline void
128 erase (typename parent_trie::iterator item)
129 {
130 policy_container::erase (policy_container::s_iterator_to (*item));
131 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700132
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700133 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700134 clear ()
135 {
136 policy_container::clear ();
137 }
138
139 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700140 set_max_size (size_t max_size)
141 {
142 max_size_ = max_size;
143 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700144
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700145 inline size_t
146 get_max_size () const
147 {
148 return max_size_;
149 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700150
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700151 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700152 type () : base_(*((Base*)0)) { };
153
154 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700155 Base &base_;
156 ns3::UniformVariable u_rand;
157 size_t max_size_;
158 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700159 };
160};
161
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700162} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700164} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700165
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700166#endif // RANDOM_POLICY_H