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 
61 {
62 }
63 
64 std::pair<shared_ptr<pit::Entry>, bool>
65 Pit::insert(const Interest& interest)
66 {
67  // first lookup() the Interest Name in the NameTree, which will creates all
68  // the intermedia nodes, starting from the shortest prefix.
69  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName());
70  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
71 
72  const std::vector<shared_ptr<pit::Entry>>& pitEntries = nameTreeEntry->getPitEntries();
73 
74  // then check if this Interest is already in the PIT entries
75  auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
76  [&interest] (const shared_ptr<pit::Entry>& entry) {
77  return entry->getInterest().getName() == interest.getName() &&
78  entry->getInterest().getSelectors() == interest.getSelectors();
79  });
80  if (it != pitEntries.end()) {
81  return { *it, false };
82  }
83 
84  shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest);
85  nameTreeEntry->insertPitEntry(entry);
86  m_nItems++;
87  return { entry, true };
88 }
89 
91 Pit::findAllDataMatches(const Data& data) const
92 {
93  auto&& ntMatches = m_nameTree.findAllMatches(data.getName(),
94  [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); });
95 
96  pit::DataMatchResult matches;
97  for (const name_tree::Entry& nte : ntMatches) {
98  for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
99  if (pitEntry->getInterest().matchesData(data))
100  matches.emplace_back(pitEntry);
101  }
102  }
103 
104  return matches;
105 }
106 
107 void
108 Pit::erase(shared_ptr<pit::Entry> pitEntry)
109 {
110  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*pitEntry);
111  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
112 
113  nameTreeEntry->erasePitEntry(pitEntry);
114  m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
115 
116  --m_nItems;
117 }
118 
120 Pit::begin() const
121 {
122  return const_iterator(m_nameTree.fullEnumerate(
123  [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin());
124 }
125 
126 } // namespace nfd
const Name & getName() const
Definition: interest.hpp:216
void erase(shared_ptr< pit::Entry > pitEntry)
erases a PIT Entry
Definition: pit.cpp:108
bool eraseEntryIfEmpty(shared_ptr< name_tree::Entry > entry)
Delete a Name Tree Entry if this entry is empty.
Definition: name-tree.cpp:327
shared_ptr< name_tree::Entry > get(const fib::Entry &fibEntry) const
get NameTree entry from attached FIB entry
Definition: name-tree.hpp:348
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:454
represents an Interest packet
Definition: interest.hpp:45
pit::DataMatchResult findAllDataMatches(const Data &data) const
performs a Data match
Definition: pit.cpp:91
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
Pit(NameTree &nameTree)
Definition: pit.cpp:54
const Selectors & getSelectors() const
Definition: interest.hpp:328
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
Class Name Tree.
Definition: name-tree.hpp:79
std::pair< shared_ptr< pit::Entry >, bool > insert(const Interest &interest)
inserts a PIT entry for Interest
Definition: pit.cpp:65
shared_ptr< name_tree::Entry > lookup(const Name &prefix)
Look for the Name Tree Entry that contains this name prefix.
Definition: name-tree.cpp:205
const_iterator begin() const
returns an iterator pointing to the first PIT entry
Definition: pit.cpp:120
represents a Data packet
Definition: data.hpp:39
~Pit()
Definition: pit.cpp:60
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:406
Name Tree Entry Class.
std::vector< shared_ptr< pit::Entry > > DataMatchResult
Definition: pit.hpp:42