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 
42 {
43  for (auto entryInfoMapPair : m_entryInfoMap) {
44  delete entryInfoMapPair.second;
45  }
46 }
47 
48 void
49 PriorityFifoPolicy::doAfterInsert(iterator i)
50 {
51  this->attachQueue(i);
52  this->evictEntries();
53 }
54 
55 void
56 PriorityFifoPolicy::doAfterRefresh(iterator i)
57 {
58  this->detachQueue(i);
59  this->attachQueue(i);
60 }
61 
62 void
63 PriorityFifoPolicy::doBeforeErase(iterator i)
64 {
65  this->detachQueue(i);
66 }
67 
68 void
69 PriorityFifoPolicy::doBeforeUse(iterator i)
70 {
71  BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
72 }
73 
74 void
75 PriorityFifoPolicy::evictEntries()
76 {
77  BOOST_ASSERT(this->getCs() != nullptr);
78 
79  while (this->getCs()->size() > this->getLimit()) {
80  this->evictOne();
81  }
82 }
83 
84 void
85 PriorityFifoPolicy::evictOne()
86 {
87  BOOST_ASSERT(!m_queues[QUEUE_UNSOLICITED].empty() ||
88  !m_queues[QUEUE_STALE].empty() ||
89  !m_queues[QUEUE_FIFO].empty());
90 
91  iterator i;
92  if (!m_queues[QUEUE_UNSOLICITED].empty()) {
93  i = m_queues[QUEUE_UNSOLICITED].front();
94  }
95  else if (!m_queues[QUEUE_STALE].empty()) {
96  i = m_queues[QUEUE_STALE].front();
97  }
98  else if (!m_queues[QUEUE_FIFO].empty()) {
99  i = m_queues[QUEUE_FIFO].front();
100  }
101 
102  this->detachQueue(i);
103  this->emitSignal(beforeEvict, i);
104 }
105 
106 void
107 PriorityFifoPolicy::attachQueue(iterator i)
108 {
109  BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end());
110 
111  EntryInfo* entryInfo = new EntryInfo();
112  if (i->isUnsolicited()) {
113  entryInfo->queueType = QUEUE_UNSOLICITED;
114  }
115  else if (i->isStale()) {
116  entryInfo->queueType = QUEUE_STALE;
117  }
118  else {
119  entryInfo->queueType = QUEUE_FIFO;
120 
121  if (i->canStale()) {
122  entryInfo->moveStaleEventId = scheduler::schedule(i->getData().getFreshnessPeriod(),
123  bind(&PriorityFifoPolicy::moveToStaleQueue, this, i));
124  }
125  }
126 
127  Queue& queue = m_queues[entryInfo->queueType];
128  entryInfo->queueIt = queue.insert(queue.end(), i);
129  m_entryInfoMap[i] = entryInfo;
130 }
131 
132 void
133 PriorityFifoPolicy::detachQueue(iterator i)
134 {
135  BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
136 
137  EntryInfo* entryInfo = m_entryInfoMap[i];
138  if (entryInfo->queueType == QUEUE_FIFO) {
139  scheduler::cancel(entryInfo->moveStaleEventId);
140  }
141 
142  m_queues[entryInfo->queueType].erase(entryInfo->queueIt);
143  m_entryInfoMap.erase(i);
144  delete entryInfo;
145 }
146 
147 void
148 PriorityFifoPolicy::moveToStaleQueue(iterator i)
149 {
150  BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
151 
152  EntryInfo* entryInfo = m_entryInfoMap[i];
153  BOOST_ASSERT(entryInfo->queueType == QUEUE_FIFO);
154 
155  m_queues[QUEUE_FIFO].erase(entryInfo->queueIt);
156 
157  entryInfo->queueType = QUEUE_STALE;
158  Queue& queue = m_queues[QUEUE_STALE];
159  entryInfo->queueIt = queue.insert(queue.end(), i);
160  m_entryInfoMap[i] = entryInfo;
161 }
162 
163 } // namespace priorityfifo
164 } // namespace cs
165 } // namespace nfd
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:53
Cs * getCs() const
gets cs
Definition: cs-policy.hpp:171
Copyright (c) 2014-2016, 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:40
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
size_t getLimit() const
gets hard limit (in number of entries)
Definition: cs-policy.hpp:183