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 | |
| 13 | const shared_ptr<fib::Entry> Fib::m_emptyEntry = make_shared<fib::Entry>(Name()); |
| 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); |
| 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 | } |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 52 | return m_emptyEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 55 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 56 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 57 | { |
| 58 | return this->findLongestPrefixMatch(pitEntry.getName()); |
| 59 | } |
| 60 | |
| 61 | shared_ptr<fib::Entry> |
| 62 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 63 | { |
| 64 | return this->findLongestPrefixMatch(measurementsEntry.getName()); |
| 65 | } |
| 66 | |
| 67 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 68 | Fib::findExactMatch(const Name& prefix) const |
| 69 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 70 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 71 | if (static_cast<bool>(nameTreeEntry)) |
| 72 | return nameTreeEntry->getFibEntry(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 73 | return shared_ptr<fib::Entry>(); |
| 74 | } |
| 75 | |
| 76 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 77 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 78 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 79 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 80 | if (static_cast<bool>(nameTreeEntry)) |
| 81 | { |
| 82 | nameTreeEntry->eraseFibEntry(nameTreeEntry->getFibEntry()); |
| 83 | m_nItems--; |
| 84 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 89 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame^] | 90 | for (NameTree::const_iterator it = |
| 91 | m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasFibEntry); it != m_nameTree.end(); it++) |
| 92 | { |
| 93 | shared_ptr<fib::Entry> entry = it->getFibEntry(); |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 94 | entry->removeNextHop(face); |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame] | 95 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 98 | } // namespace nfd |