blob: e7231a90e05d992566ca1391dd1d6213be5ced15 [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#include "cs-policy-priority-fifo.hpp"
27#include "cs.hpp"
28#include <ndn-cxx/util/signal.hpp>
29
30namespace nfd {
31namespace cs {
32namespace priority_fifo {
33
34const std::string PriorityFifoPolicy::POLICY_NAME = "fifo";
35
36PriorityFifoPolicy::PriorityFifoPolicy()
37 : Policy(POLICY_NAME)
38{
39}
40
41void
42PriorityFifoPolicy::doAfterInsert(iterator i)
43{
44 this->attachQueue(i);
45 this->evictEntries();
46}
47
48void
49PriorityFifoPolicy::doAfterRefresh(iterator i)
50{
51 this->detachQueue(i);
52 this->attachQueue(i);
53}
54
55void
56PriorityFifoPolicy::doBeforeErase(iterator i)
57{
58 this->detachQueue(i);
59}
60
61void
62PriorityFifoPolicy::doBeforeUse(iterator i)
63{
64 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
65}
66
67void
68PriorityFifoPolicy::evictEntries()
69{
70 BOOST_ASSERT(this->getCs() != nullptr);
71
72 while (this->getCs()->size() > this->getLimit()) {
73 this->evictOne();
74 }
75}
76
77void
78PriorityFifoPolicy::evictOne()
79{
80 BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() ||
81 !m_queues[QUEUE_STALE].empty() ||
82 !m_queues[QUEUE_FIFO].empty());
83
84 iterator i;
85 if (!m_queues[QUEUE_UNSOLICITED].empty()) {
86 i = m_queues[QUEUE_UNSOLICITED].front();
87 }
88 else if (!m_queues[QUEUE_STALE].empty()) {
89 i = m_queues[QUEUE_STALE].front();
90 }
91 else if (!m_queues[QUEUE_FIFO].empty()) {
92 i = m_queues[QUEUE_FIFO].front();
93 }
94
95 this->detachQueue(i);
96 this->emitSignal(beforeEvict, i);
97}
98
99void
100PriorityFifoPolicy::attachQueue(iterator i)
101{
102 BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end());
103
104 EntryInfo* entryInfo = new EntryInfo();
105 if (i->isUnsolicited()) {
106 entryInfo->queueType = QUEUE_UNSOLICITED;
107 }
108 else if (i->isStale()) {
109 entryInfo->queueType = QUEUE_STALE;
110 }
111 else {
112 entryInfo->queueType = QUEUE_FIFO;
113
114 if (i->canStale()) {
115 entryInfo->moveStaleEventId = scheduler::schedule(i->getData().getFreshnessPeriod(),
116 bind(&PriorityFifoPolicy::moveToStaleQueue, this, i));
117 }
118 }
119
120 Queue& queue = m_queues[entryInfo->queueType];
121 entryInfo->queueIt = queue.insert(queue.end(), i);
122 m_entryInfoMap[i] = entryInfo;
123}
124
125void
126PriorityFifoPolicy::detachQueue(iterator i)
127{
128 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
129
130 EntryInfo* entryInfo = m_entryInfoMap[i];
131 if (entryInfo->queueType == QUEUE_FIFO) {
132 scheduler::cancel(entryInfo->moveStaleEventId);
133 }
134
135 m_queues[entryInfo->queueType].erase(entryInfo->queueIt);
136 m_entryInfoMap.erase(i);
137}
138
139void
140PriorityFifoPolicy::moveToStaleQueue(iterator i)
141{
142 BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
143
144 EntryInfo* entryInfo = m_entryInfoMap[i];
145 BOOST_ASSERT(entryInfo->queueType == QUEUE_FIFO);
146
147 m_queues[QUEUE_FIFO].erase(entryInfo->queueIt);
148
149 entryInfo->queueType = QUEUE_STALE;
150 Queue& queue = m_queues[QUEUE_STALE];
151 entryInfo->queueIt = queue.insert(queue.end(), i);
152 m_entryInfoMap[i] = entryInfo;
153}
154
155} // namespace priorityfifo
156} // namespace cs
157} // namespace nfd