NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
cs-policy-priority-fifo.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 #include "cs.hpp"
28 #include <ndn-cxx/util/signal.hpp>
29 
30 namespace nfd {
31 namespace cs {
32 namespace priority_fifo {
33 
34 const std::string PriorityFifoPolicy::POLICY_NAME = "fifo";
35 
37  : Policy(POLICY_NAME)
38 {
39 }
40 
41 void
42 PriorityFifoPolicy::doAfterInsert(iterator i)
43 {
44  this->attachQueue(i);
45  this->evictEntries();
46 }
47 
48 void
49 PriorityFifoPolicy::doAfterRefresh(iterator i)
50 {
51  this->detachQueue(i);
52  this->attachQueue(i);
53 }
54 
55 void
56 PriorityFifoPolicy::doBeforeErase(iterator i)
57 {
58  this->detachQueue(i);
59 }
60 
61 void
62 PriorityFifoPolicy::doBeforeUse(iterator i)
63 {
64  BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
65 }
66 
67 void
68 PriorityFifoPolicy::evictEntries()
69 {
70  BOOST_ASSERT(this->getCs() != nullptr);
71 
72  while (this->getCs()->size() > this->getLimit()) {
73  this->evictOne();
74  }
75 }
76 
77 void
78 PriorityFifoPolicy::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 
99 void
100 PriorityFifoPolicy::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 
125 void
126 PriorityFifoPolicy::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 
139 void
140 PriorityFifoPolicy::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
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:58
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
signal::Signal< Policy, iterator > beforeEvict
emits when an entry is being evicted
Definition: cs-policy.hpp:81
Table::const_iterator iterator
Definition: cs-internal.hpp:41
represents a CS replacement policy
Definition: cs-policy.hpp:39
#define emitSignal(...)
(implementation detail)
Definition: signal-emit.hpp:76
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Definition: scheduler.cpp:50
size_t getLimit() const
gets hard limit (in number of entries)
Definition: cs-policy.hpp:183
Cs * getCs() const
gets cs
Definition: cs-policy.hpp:171