blob: 93ae8846be39ac9c679b68c8c174d398a1a67905 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#ifndef NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP
27#define NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP
28
29#include "cs-policy.hpp"
30#include "common.hpp"
31#include "core/scheduler.hpp"
32
33namespace nfd {
34namespace cs {
35namespace priority_fifo {
36
37typedef std::list<iterator> Queue;
38typedef Queue::iterator QueueIt;
39
40enum QueueType {
41 QUEUE_UNSOLICITED,
42 QUEUE_STALE,
43 QUEUE_FIFO,
44 QUEUE_MAX
45};
46
47struct EntryInfo
48{
49 QueueType queueType;
50 QueueIt queueIt;
51 scheduler::EventId moveStaleEventId;
52};
53
54struct EntryItComparator
55{
56 bool
57 operator()(const iterator& a, const iterator& b) const
58 {
59 return *a < *b;
60 }
61};
62
63typedef std::map<iterator, EntryInfo*, EntryItComparator> EntryInfoMapFifo;
64
65/** \brief Priority Fifo cs replacement policy
66 *
67 * The entries that get removed first are unsolicited Data packets,
68 * which are the Data packets that got cached opportunistically without preceding
69 * forwarding of the corresponding Interest packet.
70 * Next, the Data packets with expired freshness are removed.
71 * Last, the Data packets are removed from the Content Store on a pure FIFO basis.
72 */
73class PriorityFifoPolicy : public Policy
74{
75public:
76 PriorityFifoPolicy();
77
78public:
79 static const std::string POLICY_NAME;
80
81private:
82 virtual void
83 doAfterInsert(iterator i) DECL_OVERRIDE;
84
85 virtual void
86 doAfterRefresh(iterator i) DECL_OVERRIDE;
87
88 virtual void
89 doBeforeErase(iterator i) DECL_OVERRIDE;
90
91 virtual void
92 doBeforeUse(iterator i) DECL_OVERRIDE;
93
94 virtual void
95 evictEntries() DECL_OVERRIDE;
96
97private:
98 /** \brief evicts one entry
99 * \pre CS is not empty
100 */
101 void
102 evictOne();
103
104 /** \brief attaches the entry to an appropriate queue
105 * \pre the entry is not in any queue
106 */
107 void
108 attachQueue(iterator i);
109
110 /** \brief detaches the entry from its current queue
111 * \post the entry is not in any queue
112 */
113 void
114 detachQueue(iterator i);
115
116 /** \brief moves an entry from FIFO queue to STALE queue
117 */
118 void
119 moveToStaleQueue(iterator i);
120
121private:
122 Queue m_queues[QUEUE_MAX];
123 EntryInfoMapFifo m_entryInfoMap;
124};
125
126} // namespace priorityfifo
127
128using priority_fifo::PriorityFifoPolicy;
129
130} // namespace cs
131} // namespace nfd
132
133#endif // NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP