NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
fib.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "fib.hpp"
27 #include "pit-entry.hpp"
28 #include "measurements-entry.hpp"
29 
30 #include <boost/concept/assert.hpp>
31 #include <boost/concept_check.hpp>
32 #include <type_traits>
33 
34 namespace nfd {
35 namespace fib {
36 
37 const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
38 
39 // http://en.cppreference.com/w/cpp/concept/ForwardIterator
40 BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Fib::const_iterator>));
41 // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
42 // which doesn't require DefaultConstructible
43 #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
44 static_assert(std::is_default_constructible<Fib::const_iterator>::value,
45  "Fib::const_iterator must be default-constructible");
46 #else
47 BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Fib::const_iterator>));
48 #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
49 
50 static inline bool
52 {
53  return nte.getFibEntry() != nullptr;
54 }
55 
56 Fib::Fib(NameTree& nameTree)
57  : m_nameTree(nameTree)
58  , m_nItems(0)
59 {
60 }
61 
62 template<typename K>
63 const Entry&
64 Fib::findLongestPrefixMatchImpl(const K& key) const
65 {
67  if (nte != nullptr) {
68  return *nte->getFibEntry();
69  }
70  return *s_emptyEntry;
71 }
72 
73 const Entry&
74 Fib::findLongestPrefixMatch(const Name& prefix) const
75 {
76  return this->findLongestPrefixMatchImpl(prefix);
77 }
78 
79 const Entry&
81 {
82  return this->findLongestPrefixMatchImpl(pitEntry);
83 }
84 
85 const Entry&
86 Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
87 {
88  return this->findLongestPrefixMatchImpl(measurementsEntry);
89 }
90 
91 Entry*
92 Fib::findExactMatch(const Name& prefix)
93 {
94  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
95  if (nte != nullptr)
96  return nte->getFibEntry();
97 
98  return nullptr;
99 }
100 
101 std::pair<Entry*, bool>
102 Fib::insert(const Name& prefix)
103 {
104  name_tree::Entry& nte = m_nameTree.lookup(prefix);
105  Entry* entry = nte.getFibEntry();
106  if (entry != nullptr) {
107  return std::make_pair(entry, false);
108  }
109 
110  nte.setFibEntry(make_unique<Entry>(prefix));
111  ++m_nItems;
112  return std::make_pair(nte.getFibEntry(), true);
113 }
114 
115 void
116 Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
117 {
118  BOOST_ASSERT(nte != nullptr);
119 
120  nte->setFibEntry(nullptr);
121  if (canDeleteNte) {
122  m_nameTree.eraseIfEmpty(nte);
123  }
124  --m_nItems;
125 }
126 
127 void
128 Fib::erase(const Name& prefix)
129 {
130  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
131  if (nte != nullptr) {
132  this->erase(nte);
133  }
134 }
135 
136 void
137 Fib::erase(const Entry& entry)
138 {
139  name_tree::Entry* nte = m_nameTree.getEntry(entry);
140  if (nte == nullptr) { // don't try to erase s_emptyEntry
141  BOOST_ASSERT(&entry == s_emptyEntry.get());
142  return;
143  }
144  this->erase(nte);
145 }
146 
147 void
149 {
150  entry.removeNextHop(face);
151 
152  if (!entry.hasNextHops()) {
153  name_tree::Entry* nte = m_nameTree.getEntry(entry);
154  this->erase(nte, false);
155  }
156 }
157 
159 Fib::getRange() const
160 {
161  return m_nameTree.fullEnumerate(&nteHasFibEntry) |
162  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
163 }
164 
165 } // namespace fib
166 } // namespace nfd
std::pair< Entry *, bool > insert(const Name &prefix)
inserts a FIB entry for prefix
Definition: fib.cpp:102
Fib(NameTree &nameTree)
Definition: fib.cpp:56
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:92
void erase(const Name &prefix)
Definition: fib.cpp:128
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
Entry * findExactMatch(const Name &name) const
exact match lookup
Definition: name-tree.cpp:148
fib::Entry * getFibEntry() const
const Entry & findLongestPrefixMatch(const Name &prefix) const
performs a longest prefix match
Definition: fib.cpp:74
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
longest prefix matching
Definition: name-tree.cpp:155
represents a Measurements entry
void removeNextHop(const Face &face)
removes a NextHop record
Definition: fib-entry.cpp:66
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
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 removeNextHop(Entry &entry, const Face &face)
removes the NextHop record for face
Definition: fib.cpp:148
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
a functor to get a table entry from a name tree entry
static bool nteHasFibEntry(const name_tree::Entry &nte)
Definition: fib.cpp:51
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
Definition: fib.hpp:105
bool hasNextHops() const
Definition: fib-entry.hpp:72
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
an entry in the name tree