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 { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 12 | Fib::Fib(NameTree& nameTree) |
| 13 | : m_nameTree(nameTree) |
| 14 | , m_nItems(0) |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 15 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 16 | m_rootEntry = (this->insert(Name())).first; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | Fib::~Fib() |
| 20 | { |
| 21 | } |
| 22 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 23 | std::pair<shared_ptr<fib::Entry>, bool> |
| 24 | Fib::insert(const Name& prefix) |
| 25 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 26 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix); |
| 27 | shared_ptr<fib::Entry> entry = nameTreeEntry->getFibEntry(); |
| 28 | if (static_cast<bool>(entry)) |
| 29 | return std::make_pair(entry, false); |
| 30 | entry = make_shared<fib::Entry>(prefix); |
| 31 | nameTreeEntry->setFibEntry(entry); |
| 32 | m_nItems++; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 33 | return std::make_pair(entry, true); |
| 34 | } |
| 35 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 36 | shared_ptr<fib::Entry> |
| 37 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 38 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 39 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findLongestPrefixMatch(prefix); |
| 40 | while (static_cast<bool>(nameTreeEntry)) |
| 41 | { |
| 42 | if (static_cast<bool>(nameTreeEntry->getFibEntry())) |
| 43 | return nameTreeEntry->getFibEntry(); |
| 44 | else |
| 45 | nameTreeEntry = nameTreeEntry->getParent(); |
| 46 | } |
| 47 | return m_rootEntry; |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 50 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 51 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 52 | { |
| 53 | return this->findLongestPrefixMatch(pitEntry.getName()); |
| 54 | } |
| 55 | |
| 56 | shared_ptr<fib::Entry> |
| 57 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 58 | { |
| 59 | return this->findLongestPrefixMatch(measurementsEntry.getName()); |
| 60 | } |
| 61 | |
| 62 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 63 | Fib::findExactMatch(const Name& prefix) const |
| 64 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 65 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 66 | if (static_cast<bool>(nameTreeEntry)) |
| 67 | return nameTreeEntry->getFibEntry(); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 68 | return shared_ptr<fib::Entry>(); |
| 69 | } |
| 70 | |
| 71 | void |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 72 | Fib::erase(const Name& prefix) |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 73 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 74 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix); |
| 75 | if (static_cast<bool>(nameTreeEntry)) |
| 76 | { |
| 77 | nameTreeEntry->eraseFibEntry(nameTreeEntry->getFibEntry()); |
| 78 | m_nItems--; |
| 79 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 84 | { |
HangZhang | ad4afd1 | 2014-03-01 11:03:08 +0800 | [diff] [blame^] | 85 | shared_ptr<fib::Entry> entry; |
| 86 | shared_ptr<std::vector<shared_ptr<name_tree::Entry > > > res = m_nameTree.fullEnumerate(); |
| 87 | for (int i = 0; i < res->size(); i++) |
| 88 | { |
| 89 | entry = (*res)[i]->getFibEntry(); |
| 90 | if (static_cast<bool>(entry)) |
| 91 | entry->removeNextHop(face); |
| 92 | } |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 96 | } // namespace nfd |