blob: 669b8b239377261abad71d2230b4a3308d3b7846 [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 Afanasyev30f60e32012-07-10 14:21:16 -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 Afanasyev30f60e32012-07-10 14:21:16 -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 Afanasyev30f60e32012-07-10 14:21:16 -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 Afanasyev30f60e32012-07-10 14:21:16 -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 Afanasyev30f60e32012-07-10 14:21:16 -070019
20#ifndef PERSISTENT_POLICY_H_
21#define PERSISTENT_POLICY_H_
22
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070025#include <boost/intrusive/options.hpp>
26#include <boost/intrusive/list.hpp>
27
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028namespace ns3 {
29namespace ndn {
30namespace ndnSIM {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070031
32/**
33 * @brief Traits for persistent replacement policy
34 *
35 * In this policy entries are added until there is a space (controlled by set_max_size call).
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036 * If maximum is reached, new entries will not be added and nothing will be removed from the
37 *container
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070038 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039struct persistent_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080040 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080041 static std::string
42 GetName()
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070043 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 return "Persistent";
45 }
46
47 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070048 };
49
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 template<class Container>
51 struct container_hook {
52 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
53 type;
54 };
55
56 template<class Base, class Container, class Hook>
57 struct policy {
58 typedef typename boost::intrusive::list<Container, Hook> policy_container;
59
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070060 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 class type : public policy_container {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070062 public:
63 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064
65 type(Base& base)
66 : base_(base)
67 , max_size_(100) // when 0, policy is not enforced
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070068 {
69 }
70
71 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072 update(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070073 {
74 // do nothing
75 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070077 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 insert(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070079 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 if (max_size_ != 0 && policy_container::size() >= max_size_)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070081 return false;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082
83 policy_container::push_back(*item);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070084 return true;
85 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070087 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 lookup(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070089 {
90 // do nothing
91 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070093 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 erase(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070095 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070097 }
98
99 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 clear()
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700101 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 policy_container::clear();
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700103 }
104
105 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800106 set_max_size(size_t max_size)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700107 {
108 max_size_ = max_size;
109 }
110
111 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 get_max_size() const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700113 {
114 return max_size_;
115 }
116
117 private:
118 // type () : base_(*((Base*)0)) { };
119
120 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 Base& base_;
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700122 size_t max_size_;
123 };
124 };
125};
126
127} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700129} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700130
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800131/// @endcond
132
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700133#endif // PERSISTENT_POLICY_H_