blob: 705bcf3aadf2b4bb896c9d5ada36b40d2f8b5739 [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
29namespace ndnSIM
30{
31
Alexander Afanasyev9a989702012-06-29 17:44:00 -070032struct random_policy_traits
33{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070034 struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070035
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070036 template<class Container>
37 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070039 typedef boost::intrusive::member_hook< Container,
40 policy_hook_type,
41 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042 };
43
44 template<class Base,
45 class Container,
46 class Hook>
47 struct policy
48 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070049 static uint32_t& get_order (typename Container::iterator item)
50 {
51 return static_cast<typename policy_container::value_traits::hook_type*>
52 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
53 }
54
55 static const uint32_t& get_order (typename Container::const_iterator item)
56 {
57 return static_cast<const typename policy_container::value_traits::hook_type*>
58 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
59 }
60
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070061 template<class Key>
62 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070063 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 bool operator () (const Key &a, const Key &b) const
65 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070066 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070067 }
68 };
69
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070070 typedef boost::intrusive::set< Container,
71 boost::intrusive::compare< MemberHookLess< Container > >,
72 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073
74 // could be just typedef
75 class type : public policy_container
76 {
77 public:
78 typedef Container parent_trie;
79
80 type (Base &base)
81 : base_ (base)
82 , u_rand (0, std::numeric_limits<uint32_t>::max ())
83 , max_size_ (100)
84 {
85 }
86
87 inline void
88 update (typename parent_trie::iterator item)
89 {
90 // do nothing. it's random policy
91 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070092
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070093 inline bool
94 insert (typename parent_trie::iterator item)
95 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070096 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -070097
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 if (policy_container::size () >= max_size_)
99 {
100 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
101 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700102 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700103 // just return false. Indicating that insert "failed"
104 return false;
105 }
106 else
107 {
108 // removing some random element
109 base_.erase (&(*policy_container::begin ()));
110 }
111 }
112
113 policy_container::insert (*item);
114 return true;
115 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700116
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700117 inline void
118 lookup (typename parent_trie::iterator item)
119 {
120 // do nothing. it's random policy
121 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700122
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700123 inline void
124 erase (typename parent_trie::iterator item)
125 {
126 policy_container::erase (policy_container::s_iterator_to (*item));
127 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700128
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700129 inline void
130 set_max_size (size_t max_size)
131 {
132 max_size_ = max_size;
133 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700134
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700135 inline size_t
136 get_max_size () const
137 {
138 return max_size_;
139 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700140
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700141 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700142 type () : base_(*((Base*)0)) { };
143
144 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700145 Base &base_;
146 ns3::UniformVariable u_rand;
147 size_t max_size_;
148 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700149 };
150};
151
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700152} // ndnSIM
153
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700154#endif // RANDOM_POLICY_H