blob: 1800f95c9b57038713f5a1570e9e268c686b3432 [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:
85 typedef Container parent_trie;
86
87 type (Base &base)
88 : base_ (base)
89 , u_rand (0, std::numeric_limits<uint32_t>::max ())
90 , max_size_ (100)
91 {
92 }
93
94 inline void
95 update (typename parent_trie::iterator item)
96 {
97 // do nothing. it's random policy
98 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070099
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700100 inline bool
101 insert (typename parent_trie::iterator item)
102 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700103 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700104
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -0700105 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700106 {
107 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
108 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700109 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700110 // just return false. Indicating that insert "failed"
111 return false;
112 }
113 else
114 {
115 // removing some random element
116 base_.erase (&(*policy_container::begin ()));
117 }
118 }
119
120 policy_container::insert (*item);
121 return true;
122 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700123
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700124 inline void
125 lookup (typename parent_trie::iterator item)
126 {
127 // do nothing. it's random policy
128 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700129
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700130 inline void
131 erase (typename parent_trie::iterator item)
132 {
133 policy_container::erase (policy_container::s_iterator_to (*item));
134 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700135
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700136 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700137 clear ()
138 {
139 policy_container::clear ();
140 }
141
142 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700143 set_max_size (size_t max_size)
144 {
145 max_size_ = max_size;
146 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700147
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700148 inline size_t
149 get_max_size () const
150 {
151 return max_size_;
152 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700153
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700154 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700155 type () : base_(*((Base*)0)) { };
156
157 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700158 Base &base_;
159 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