blob: 57d6835e33f812e5ba82dff3d87f28dbcf0b5df0 [file] [log] [blame]
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 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 PROBABILITY_POLICY_H_
22#define PROBABILITY_POLICY_H_
23
24#include <boost/intrusive/options.hpp>
25#include <boost/intrusive/list.hpp>
26
27#include <ns3/random-variable.h>
28
29namespace ns3 {
30namespace ndn {
31namespace ndnSIM {
32
33/**
34 * @brief Traits for freshness policy
35 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036struct probability_policy_traits {
37 static std::string
38 GetName()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070039 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 return "ProbabilityImpl";
41 }
42
43 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070044 };
45
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 template<class Container>
47 struct container_hook {
48 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
49 type;
50 };
51
52 template<class Base, class Container, class Hook>
53 struct policy {
54 typedef typename boost::intrusive::list<Container, Hook> policy_container;
55
56 class type : public policy_container {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070057 public:
58 typedef policy policy_base; // to get access to get_freshness methods from outside
59 typedef Container parent_trie;
60
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 type(Base& base)
62 : base_(base)
63 , max_size_(100)
64 , probability_(1.0)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070065 {
66 }
67
68 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 update(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070070 {
71 }
72
73 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 insert(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070075 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 if (ns3_rand_.GetValue() < probability_) {
77 policy_container::push_back(*item);
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070078
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 // allow caching
80 return true;
81 }
82 else {
83 // don't allow caching
84 return false;
85 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070086 }
87
88 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 lookup(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070090 {
91 // do nothing. it's random policy
92 }
93
94 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 erase(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070096 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070098 }
99
100 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 clear()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700102 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 policy_container::clear();
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700104 }
105
106 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 set_max_size(size_t max_size)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700108 {
109 max_size_ = max_size;
110 }
111
112 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 get_max_size() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700114 {
115 return max_size_;
116 }
117
118 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 set_probability(double probability)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700120 {
121 probability_ = probability;
122 }
123
124 inline double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 get_probability() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700126 {
127 return probability_;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700129
130 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 type()
132 : base_(*((Base*)0)){};
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700133
134 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 Base& base_;
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700136 size_t max_size_;
137 double probability_;
138 UniformVariable ns3_rand_;
139 };
140 };
141};
142
143} // ndnSIM
144} // ndn
145} // ns3
146
147#endif // PROBABILITY_POLICY_H