blob: 8638c0a596983a6e1a2f45e73f0f93829beadf11 [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
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070024#include "ns3/ndnSIM/model/ndn-common.hpp"
25
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070026#include <boost/intrusive/options.hpp>
27#include <boost/intrusive/list.hpp>
28
29#include <ns3/random-variable.h>
30
31namespace ns3 {
32namespace ndn {
33namespace ndnSIM {
34
35/**
36 * @brief Traits for freshness policy
37 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038struct probability_policy_traits {
39 static std::string
40 GetName()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070041 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080042 return "ProbabilityImpl";
43 }
44
45 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070046 };
47
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048 template<class Container>
49 struct container_hook {
50 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
51 type;
52 };
53
54 template<class Base, class Container, class Hook>
55 struct policy {
56 typedef typename boost::intrusive::list<Container, Hook> policy_container;
57
58 class type : public policy_container {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070059 public:
60 typedef policy policy_base; // to get access to get_freshness methods from outside
61 typedef Container parent_trie;
62
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 type(Base& base)
64 : base_(base)
65 , max_size_(100)
66 , probability_(1.0)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070067 {
68 }
69
70 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 update(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070072 {
73 }
74
75 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 insert(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070077 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 if (ns3_rand_.GetValue() < probability_) {
79 policy_container::push_back(*item);
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070080
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 // allow caching
82 return true;
83 }
84 else {
85 // don't allow caching
86 return false;
87 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070088 }
89
90 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 lookup(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070092 {
93 // do nothing. it's random policy
94 }
95
96 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 erase(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070098 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700100 }
101
102 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 clear()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700104 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 policy_container::clear();
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700106 }
107
108 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 set_max_size(size_t max_size)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700110 {
111 max_size_ = max_size;
112 }
113
114 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 get_max_size() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700116 {
117 return max_size_;
118 }
119
120 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 set_probability(double probability)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700122 {
123 probability_ = probability;
124 }
125
126 inline double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 get_probability() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700128 {
129 return probability_;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700131
132 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 type()
134 : base_(*((Base*)0)){};
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700135
136 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800137 Base& base_;
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700138 size_t max_size_;
139 double probability_;
140 UniformVariable ns3_rand_;
141 };
142 };
143};
144
145} // ndnSIM
146} // ndn
147} // ns3
148
149#endif // PROBABILITY_POLICY_H