NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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 #include <type_traits>
28 
29 #include <boost/concept/assert.hpp>
30 #include <boost/concept_check.hpp>
31 #include <type_traits>
32 
33 namespace nfd {
34 namespace pit {
35 
36 #if HAVE_IS_MOVE_CONSTRUCTIBLE
37 static_assert(std::is_move_constructible<DataMatchResult>::value,
38  "DataMatchResult must be MoveConstructible");
39 #endif // HAVE_IS_MOVE_CONSTRUCTIBLE
40 
41 } // namespace pit
42 
43 // http://en.cppreference.com/w/cpp/concept/ForwardIterator
44 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Pit::const_iterator>));
45 // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
46 // which doesn't require DefaultConstructible
47 #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
48 static_assert(std::is_default_constructible<Pit::const_iterator>::value,
49  "Pit::const_iterator must be default-constructible");
50 #else
51 BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Pit::const_iterator>));
52 #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
53 
54 Pit::Pit(NameTree& nameTree)
55  : m_nameTree(nameTree)
56  , m_nItems(0)
57 {
58 }
59 
60 std::pair<shared_ptr<pit::Entry>, bool>
61 Pit::findOrInsert(const Interest& interest, bool allowInsert)
62 {
63  // ensure NameTree entry exists
64  const Name& name = interest.getName();
65  bool isEndWithDigest = name.size() > 0 && name[-1].isImplicitSha256Digest();
66  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(isEndWithDigest ? name.getPrefix(-1) : name);
67  BOOST_ASSERT(nte != nullptr);
68  size_t nteNameLen = nte->getPrefix().size();
69 
70  // check if PIT entry already exists
71  const std::vector<shared_ptr<pit::Entry>>& pitEntries = nte->getPitEntries();
72  auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
73  [&interest, nteNameLen] (const shared_ptr<pit::Entry>& entry) -> bool {
74  // initial part of the name is guaranteed to be the same
75  BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen,
76  interest.getName(), 0, nteNameLen) == 0);
77  // compare implicit digest (or its absence) only
78  return entry->getInterest().getName().compare(nteNameLen, Name::npos,
79  interest.getName(), nteNameLen) == 0 &&
80  entry->getInterest().getSelectors() == interest.getSelectors();
81  });
82  if (it != pitEntries.end()) {
83  return {*it, false};
84  }
85 
86  if (!allowInsert) {
87  return {nullptr, true};
88  }
89 
90  auto entry = make_shared<pit::Entry>(interest);
91  nte->insertPitEntry(entry);
92  m_nItems++;
93  return {entry, true};
94 }
95 
97 Pit::findAllDataMatches(const Data& data) const
98 {
99  auto&& ntMatches = m_nameTree.findAllMatches(data.getName(),
100  [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); });
101 
102  pit::DataMatchResult matches;
103  for (const name_tree::Entry& nte : ntMatches) {
104  for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
105  if (pitEntry->getInterest().matchesData(data))
106  matches.emplace_back(pitEntry);
107  }
108  }
109 
110  return matches;
111 }
112 
113 void
114 Pit::erase(shared_ptr<pit::Entry> pitEntry)
115 {
116  shared_ptr<name_tree::Entry> nameTreeEntry = pitEntry->m_nameTreeEntry;
117  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
118 
119  nameTreeEntry->erasePitEntry(pitEntry);
120  m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
121 
122  --m_nItems;
123 }
124 
126 Pit::begin() const
127 {
128  return const_iterator(m_nameTree.fullEnumerate(
129  [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin());
130 }
131 
132 } // namespace nfd
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
void erase(shared_ptr< pit::Entry > pitEntry)
erases a PIT Entry
Definition: pit.cpp:114
const Selectors & getSelectors() const
Definition: interest.hpp:328
bool eraseEntryIfEmpty(shared_ptr< name_tree::Entry > entry)
Delete a Name Tree Entry if this entry is empty.
Definition: name-tree.cpp:245
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:360
static const size_t npos
indicates "until the end" in getSubName and compare
Definition: name.hpp:617
represents an Interest packet
Definition: interest.hpp:45
pit::DataMatchResult findAllDataMatches(const Data &data) const
performs a Data match
Definition: pit.cpp:97
const_iterator begin() const
returns an iterator pointing to the first PIT entry
Definition: pit.cpp:126
Pit(NameTree &nameTree)
Definition: pit.cpp:54
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Class Name Tree.
Definition: name-tree.hpp:79
Name abstraction to represent an absolute name.
Definition: name.hpp:46
shared_ptr< name_tree::Entry > lookup(const Name &prefix)
Look for the Name Tree Entry that contains this name prefix.
Definition: name-tree.cpp:207
boost::iterator_range< const_iterator > findAllMatches(const Name &prefix, const name_tree::EntrySelector &entrySelector=name_tree::AnyEntry()) const
Enumerate all the name prefixes that satisfy the prefix and entrySelector.
Definition: name-tree.cpp:439
size_t size() const
Get the number of components.
Definition: name.hpp:408
represents a Data packet
Definition: data.hpp:39
boost::iterator_range< const_iterator > fullEnumerate(const name_tree::EntrySelector &entrySelector=name_tree::AnyEntry()) const
Enumerate all entries, optionally filtered by an EntrySelector.
Definition: name-tree.cpp:461
Name Tree Entry Class.
const Name & getName() const
Definition: interest.hpp:218
std::vector< shared_ptr< pit::Entry > > DataMatchResult
Definition: pit.hpp:42