NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
pit.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "pit.hpp"
27 
28 namespace nfd {
29 namespace pit {
30 
31 static inline bool
33 {
34  return nte.hasPitEntries();
35 }
36 
37 Pit::Pit(NameTree& nameTree)
38  : m_nameTree(nameTree)
39  , m_nItems(0)
40 {
41 }
42 
43 std::pair<shared_ptr<Entry>, bool>
44 Pit::findOrInsert(const Interest& interest, bool allowInsert)
45 {
46  // determine which NameTree entry should the PIT entry be attached onto
47  const Name& name = interest.getName();
48  bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
49  const Name& nteName = isEndWithDigest ? name.getPrefix(-1) : name;
50 
51  // ensure NameTree entry exists
52  name_tree::Entry* nte = nullptr;
53  if (allowInsert) {
54  nte = &m_nameTree.lookup(nteName);
55  }
56  else {
57  nte = m_nameTree.findExactMatch(nteName);
58  if (nte == nullptr) {
59  return {nullptr, true};
60  }
61  }
62 
63  // check if PIT entry already exists
64  size_t nteNameLen = nteName.size();
65  const std::vector<shared_ptr<Entry>>& pitEntries = nte->getPitEntries();
66  auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
67  [&interest, nteNameLen] (const shared_ptr<Entry>& entry) {
68  // initial part of name is guaranteed to be equal by NameTree
69  // check implicit digest (or its absence) only
70  return entry->canMatch(interest, nteNameLen);
71  });
72  if (it != pitEntries.end()) {
73  return {*it, false};
74  }
75 
76  if (!allowInsert) {
77  BOOST_ASSERT(!nte->isEmpty()); // nte shouldn't be created in this call
78  return {nullptr, true};
79  }
80 
81  auto entry = make_shared<Entry>(interest);
82  nte->insertPitEntry(entry);
83  ++m_nItems;
84  return {entry, true};
85 }
86 
88 Pit::findAllDataMatches(const Data& data) const
89 {
90  auto&& ntMatches = m_nameTree.findAllMatches(data.getName(), &nteHasPitEntries);
91 
92  DataMatchResult matches;
93  for (const name_tree::Entry& nte : ntMatches) {
94  for (const shared_ptr<Entry>& pitEntry : nte.getPitEntries()) {
95  if (pitEntry->getInterest().matchesData(data))
96  matches.emplace_back(pitEntry);
97  }
98  }
99 
100  return matches;
101 }
102 
103 void
104 Pit::erase(Entry* entry, bool canDeleteNte)
105 {
106  name_tree::Entry* nte = m_nameTree.getEntry(*entry);
107  BOOST_ASSERT(nte != nullptr);
108 
109  nte->erasePitEntry(entry);
110  if (canDeleteNte) {
111  m_nameTree.eraseIfEmpty(nte);
112  }
113  --m_nItems;
114 }
115 
116 void
118 {
119  BOOST_ASSERT(entry != nullptr);
120 
121  entry->deleteInRecord(face);
122  entry->deleteOutRecord(face);
123 
125 }
126 
128 Pit::begin() const
129 {
130  return const_iterator(m_nameTree.fullEnumerate(&nteHasPitEntries).begin());
131 }
132 
133 } // namespace pit
134 } // namespace nfd
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
void erase(Entry *entry)
deletes an entry
Definition: pit.hpp:90
generalization of a network interface
Definition: face.hpp:67
void deleteInOutRecords(Entry *entry, const Face &face)
deletes in-record and out-record for face
Definition: pit.cpp:117
const std::vector< shared_ptr< pit::Entry > > & getPitEntries() const
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
Entry * findExactMatch(const Name &name) const
exact match lookup
Definition: name-tree.cpp:148
PIT iterator.
void erasePitEntry(pit::Entry *pitEntry)
represents an Interest packet
Definition: interest.hpp:42
Iterator const_iterator
Definition: pit.hpp:101
DataMatchResult findAllDataMatches(const Data &data) const
performs a Data match
Definition: pit.cpp:88
Range findAllMatches(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
all-prefixes match lookup
Definition: name-tree.cpp:223
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
an Interest table entry
Definition: pit-entry.hpp:57
Entry & lookup(const Name &name)
find or insert an entry with specified name
Definition: name-tree.cpp:44
void deleteOutRecord(const Face &face)
delete the out-record for face if it exists
Definition: pit-entry.cpp:113
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
enumerate all entries
Definition: name-tree.cpp:236
void deleteInRecord(const Face &face)
delete the in-record for face if it exists
Definition: pit-entry.cpp:74
size_t size() const
Get the number of components.
Definition: name.hpp:400
static bool nteHasPitEntries(const name_tree::Entry &nte)
Definition: pit.cpp:32
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
delete the entry if it is empty
Definition: name-tree.cpp:121
a common index structure for FIB, PIT, StrategyChoice, and Measurements
Definition: name-tree.hpp:36
Entry * getEntry(const ENTRY &tableEntry) const
Definition: name-tree.hpp:64
const_iterator begin() const
Definition: pit.cpp:128
Pit(NameTree &nameTree)
Definition: pit.cpp:37
represents a Data packet
Definition: data.hpp:37
std::vector< shared_ptr< Entry > > DataMatchResult
Definition: pit.hpp:42
an entry in the name tree
const Name & getName() const
Definition: interest.hpp:215