blob: 3fb93a5a66c6d04d837de73a85c16932674cbaf2 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070019
20#ifndef PROBABILITY_POLICY_H_
21#define PROBABILITY_POLICY_H_
22
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070025#include <boost/intrusive/options.hpp>
26#include <boost/intrusive/list.hpp>
27
28#include <ns3/random-variable.h>
29
30namespace ns3 {
31namespace ndn {
32namespace ndnSIM {
33
34/**
35 * @brief Traits for freshness policy
36 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037struct probability_policy_traits {
38 static std::string
39 GetName()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070040 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 return "ProbabilityImpl";
42 }
43
44 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070045 };
46
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047 template<class Container>
48 struct container_hook {
49 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
50 type;
51 };
52
53 template<class Base, class Container, class Hook>
54 struct policy {
55 typedef typename boost::intrusive::list<Container, Hook> policy_container;
56
57 class type : public policy_container {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070058 public:
59 typedef policy policy_base; // to get access to get_freshness methods from outside
60 typedef Container parent_trie;
61
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080062 type(Base& base)
63 : base_(base)
64 , max_size_(100)
65 , probability_(1.0)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070066 {
67 }
68
69 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 update(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070071 {
72 }
73
74 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 insert(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070076 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 if (ns3_rand_.GetValue() < probability_) {
78 policy_container::push_back(*item);
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070079
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 // allow caching
81 return true;
82 }
83 else {
84 // don't allow caching
85 return false;
86 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070087 }
88
89 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 lookup(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070091 {
92 // do nothing. it's random policy
93 }
94
95 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 erase(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070097 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070099 }
100
101 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 clear()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700103 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 policy_container::clear();
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700105 }
106
107 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 set_max_size(size_t max_size)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700109 {
110 max_size_ = max_size;
111 }
112
113 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 get_max_size() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700115 {
116 return max_size_;
117 }
118
119 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 set_probability(double probability)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700121 {
122 probability_ = probability;
123 }
124
125 inline double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 get_probability() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700127 {
128 return probability_;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700130
131 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 type()
133 : base_(*((Base*)0)){};
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700134
135 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 Base& base_;
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700137 size_t max_size_;
138 double probability_;
139 UniformVariable ns3_rand_;
140 };
141 };
142};
143
144} // ndnSIM
145} // ndn
146} // ns3
147
148#endif // PROBABILITY_POLICY_H