NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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 #include "core/asserts.hpp"
30 
31 namespace nfd {
32 namespace fib {
33 
35 
36 const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
37 
38 static inline bool
40 {
41  return nte.getFibEntry() != nullptr;
42 }
43 
44 Fib::Fib(NameTree& nameTree)
45  : m_nameTree(nameTree)
46  , m_nItems(0)
47 {
48 }
49 
50 template<typename K>
51 const Entry&
52 Fib::findLongestPrefixMatchImpl(const K& key) const
53 {
55  if (nte != nullptr) {
56  return *nte->getFibEntry();
57  }
58  return *s_emptyEntry;
59 }
60 
61 const Entry&
62 Fib::findLongestPrefixMatch(const Name& prefix) const
63 {
64  return this->findLongestPrefixMatchImpl(prefix);
65 }
66 
67 const Entry&
69 {
70  return this->findLongestPrefixMatchImpl(pitEntry);
71 }
72 
73 const Entry&
74 Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const
75 {
76  return this->findLongestPrefixMatchImpl(measurementsEntry);
77 }
78 
79 Entry*
80 Fib::findExactMatch(const Name& prefix)
81 {
82  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
83  if (nte != nullptr)
84  return nte->getFibEntry();
85 
86  return nullptr;
87 }
88 
89 std::pair<Entry*, bool>
90 Fib::insert(const Name& prefix)
91 {
92  name_tree::Entry& nte = m_nameTree.lookup(prefix);
93  Entry* entry = nte.getFibEntry();
94  if (entry != nullptr) {
95  return std::make_pair(entry, false);
96  }
97 
98  nte.setFibEntry(make_unique<Entry>(prefix));
99  ++m_nItems;
100  return std::make_pair(nte.getFibEntry(), true);
101 }
102 
103 void
104 Fib::erase(name_tree::Entry* nte, bool canDeleteNte)
105 {
106  BOOST_ASSERT(nte != nullptr);
107 
108  nte->setFibEntry(nullptr);
109  if (canDeleteNte) {
110  m_nameTree.eraseIfEmpty(nte);
111  }
112  --m_nItems;
113 }
114 
115 void
116 Fib::erase(const Name& prefix)
117 {
118  name_tree::Entry* nte = m_nameTree.findExactMatch(prefix);
119  if (nte != nullptr) {
120  this->erase(nte);
121  }
122 }
123 
124 void
125 Fib::erase(const Entry& entry)
126 {
127  name_tree::Entry* nte = m_nameTree.getEntry(entry);
128  if (nte == nullptr) { // don't try to erase s_emptyEntry
129  BOOST_ASSERT(&entry == s_emptyEntry.get());
130  return;
131  }
132  this->erase(nte);
133 }
134 
135 void
137 {
138  entry.removeNextHop(face);
139 
140  if (!entry.hasNextHops()) {
141  name_tree::Entry* nte = m_nameTree.getEntry(entry);
142  this->erase(nte, false);
143  }
144 }
145 
147 Fib::getRange() const
148 {
149  return m_nameTree.fullEnumerate(&nteHasFibEntry) |
150  boost::adaptors::transformed(name_tree::GetTableEntry<Entry>(&name_tree::Entry::getFibEntry));
151 }
152 
153 } // namespace fib
154 } // namespace nfd
std::pair< Entry *, bool > insert(const Name &prefix)
inserts a FIB entry for prefix
Definition: fib.cpp:90
Fib(NameTree &nameTree)
Definition: fib.cpp:44
Entry * findExactMatch(const Name &prefix)
performs an exact match lookup
Definition: fib.cpp:80
void erase(const Name &prefix)
Definition: fib.cpp:116
generalization of a network interface
Definition: face.hpp:67
represents a FIB entry
Definition: fib-entry.hpp:51
fib::Entry * getFibEntry() const
const Entry & findLongestPrefixMatch(const Name &prefix) const
performs a longest prefix match
Definition: fib.cpp:62
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
longest prefix matching
Definition: name-tree.cpp:156
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, bool enforceMaxDepth=false)
find or insert an entry with specified name
Definition: name-tree.cpp:44
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:80
Represents an absolute name.
Definition: name.hpp:42
boost::range_iterator< Range >::type const_iterator
Definition: fib.hpp:119
Range fullEnumerate(const EntrySelector &entrySelector=AnyEntry()) const
enumerate all entries
Definition: name-tree.cpp:217
void removeNextHop(Entry &entry, const Face &face)
removes the NextHop record for face
Definition: fib.cpp:136
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
delete the entry if it is empty
Definition: name-tree.cpp:122
a common index structure for FIB, PIT, StrategyChoice, and Measurements
Definition: name-tree.hpp:36
NFD_ASSERT_FORWARD_ITERATOR(Fib::const_iterator)
Entry * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
exact match lookup
Definition: name-tree.cpp:149
a functor to get a table entry from a name tree entry
static bool nteHasFibEntry(const name_tree::Entry &nte)
Definition: fib.cpp:39
boost::transformed_range< name_tree::GetTableEntry< Entry >, const name_tree::Range > Range
Definition: fib.hpp:118
bool hasNextHops() const
Definition: fib-entry.hpp:72
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
an entry in the name tree