Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fib.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 8 | #include "pit-entry.hpp" |
| 9 | #include "measurements-entry.hpp" |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 11 | namespace nfd { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 12 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 13 | const shared_ptr<fib::Entry> Fib::s_emptyEntry = make_shared<fib::Entry>(Name()); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 14 | |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 15 | Fib::Fib(NameTree& nameTree) |
| 16 | : m_nameTree(nameTree) |
| 17 | , m_nItems(0) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 18 | { |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | Fib::~Fib() |
| 22 | { |
| 23 | } |
| 24 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 25 | static inline bool |
| 26 | predicate_NameTreeEntry_hasFibEntry(const name_tree::Entry& entry) |
| 27 | { |
| 28 | return static_cast<bool>(entry.getFibEntry()); |
| 29 | } |
| 30 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 31 | std::pair<shared_ptr<fib::Entry>, bool> |
| 32 | Fib::insert(const Name& prefix) |
| 33 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 34 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 35 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 36 | if (static_cast<bool>(entry)) |
| 37 | return std::make_pair(entry, false); |
| 38 | entry = make_shared<fib::Entry>(prefix); |
| 39 | nameTreeEntry->setFibEntry(entry); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 40 | ++m_nItems; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 41 | return std::make_pair(entry, true); |
| 42 | } |
| 43 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 44 | shared_ptr<fib::Entry> |
| 45 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 46 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 47 | shared_ptr<name_tree::Entry> nameTreeEntry = |
| 48 | m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasFibEntry); |
| 49 | if (static_cast<bool>(nameTreeEntry)) { |
| 50 | return nameTreeEntry->getFibEntry(); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 51 | } |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 52 | return s_emptyEntry; |
| 53 | } |
| 54 | |
| 55 | shared_ptr<fib::Entry> |
| 56 | Fib::findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const |
| 57 | { |
| 58 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 59 | if (static_cast<bool>(entry)) |
| 60 | return entry; |
| 61 | nameTreeEntry = m_nameTree.findLongestPrefixMatch(nameTreeEntry, |
| 62 | &predicate_NameTreeEntry_hasFibEntry); |
| 63 | if (static_cast<bool>(nameTreeEntry)) { |
| 64 | return nameTreeEntry->getFibEntry(); |
| 65 | } |
| 66 | return s_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 69 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 70 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 71 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 72 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry); |
| 73 | |
| 74 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 75 | |
| 76 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | shared_ptr<fib::Entry> |
| 80 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 81 | { |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 82 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(measurementsEntry); |
| 83 | |
| 84 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 85 | |
| 86 | return findLongestPrefixMatch(nameTreeEntry); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 90 | Fib::findExactMatch(const Name& prefix) const |
| 91 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 92 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 93 | if (static_cast<bool>(nameTreeEntry)) |
| 94 | return nameTreeEntry->getFibEntry(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 95 | return shared_ptr<fib::Entry>(); |
| 96 | } |
| 97 | |
| 98 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 99 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 100 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 101 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 102 | if (static_cast<bool>(nameTreeEntry)) |
| 103 | { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 104 | nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>()); |
| 105 | --m_nItems; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | Fib::erase(const fib::Entry& entry) |
| 111 | { |
| 112 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry); |
| 113 | if (static_cast<bool>(nameTreeEntry)) |
| 114 | { |
| 115 | nameTreeEntry->setFibEntry(shared_ptr<fib::Entry>()); |
| 116 | --m_nItems; |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 117 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void |
| 121 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 122 | { |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 123 | for (NameTree::const_iterator it = m_nameTree.fullEnumerate( |
| 124 | &predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); ++it) { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 125 | shared_ptr<fib::Entry> entry = it->getFibEntry(); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 126 | entry->removeNextHop(face); |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 127 | if (!entry->hasNextHops()) { |
| 128 | this->erase(*entry); |
| 129 | } |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 130 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 131 | } |
| 132 | |
HangZhang | 5d46942 | 2014-03-12 09:26:26 +0800 | [diff] [blame^] | 133 | Fib::const_iterator |
| 134 | Fib::begin() const |
| 135 | { |
| 136 | return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry)); |
| 137 | } |
| 138 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 139 | } // namespace nfd |