blob: 7ad6fd0e54efce0df2247d5ba2b7b7a22b68d8c5 [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 Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070025#include "ns3/ndnSIM/model/ndn-common.hpp"
26
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070027#include <boost/intrusive/options.hpp>
28#include <boost/intrusive/list.hpp>
29
30#include <ns3/random-variable.h>
31
32namespace ns3 {
33namespace ndn {
34namespace ndnSIM {
35
36/**
37 * @brief Traits for freshness policy
38 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039struct probability_policy_traits {
40 static std::string
41 GetName()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070042 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 return "ProbabilityImpl";
44 }
45
46 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070047 };
48
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 template<class Container>
50 struct container_hook {
51 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
52 type;
53 };
54
55 template<class Base, class Container, class Hook>
56 struct policy {
57 typedef typename boost::intrusive::list<Container, Hook> policy_container;
58
59 class type : public policy_container {
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070060 public:
61 typedef policy policy_base; // to get access to get_freshness methods from outside
62 typedef Container parent_trie;
63
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 type(Base& base)
65 : base_(base)
66 , max_size_(100)
67 , probability_(1.0)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070068 {
69 }
70
71 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 update(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070073 {
74 }
75
76 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 insert(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070078 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 if (ns3_rand_.GetValue() < probability_) {
80 policy_container::push_back(*item);
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070081
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 // allow caching
83 return true;
84 }
85 else {
86 // don't allow caching
87 return false;
88 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070089 }
90
91 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 lookup(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070093 {
94 // do nothing. it's random policy
95 }
96
97 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 erase(typename parent_trie::iterator item)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -070099 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700101 }
102
103 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 clear()
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700105 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 policy_container::clear();
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700107 }
108
109 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 set_max_size(size_t max_size)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700111 {
112 max_size_ = max_size;
113 }
114
115 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 get_max_size() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700117 {
118 return max_size_;
119 }
120
121 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 set_probability(double probability)
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700123 {
124 probability_ = probability;
125 }
126
127 inline double
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 get_probability() const
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700129 {
130 return probability_;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800131 }
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700132
133 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 type()
135 : base_(*((Base*)0)){};
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700136
137 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 Base& base_;
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700139 size_t max_size_;
140 double probability_;
141 UniformVariable ns3_rand_;
142 };
143 };
144};
145
146} // ndnSIM
147} // ndn
148} // ns3
149
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800150/// @endcond
151
Alexander Afanasyevcd31abd2013-09-05 20:45:02 -0700152#endif // PROBABILITY_POLICY_H