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 | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 12 | |
| 13 | Fib::Fib() |
| 14 | { |
| 15 | std::pair<shared_ptr<fib::Entry>, bool> pair_rootEntry_dummy = |
| 16 | this->insert(Name()); |
| 17 | m_rootEntry = pair_rootEntry_dummy.first; |
| 18 | } |
| 19 | |
| 20 | Fib::~Fib() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | static inline bool |
| 25 | predicate_FibEntry_eq_Name(const shared_ptr<fib::Entry>& entry, |
| 26 | const Name& name) |
| 27 | { |
| 28 | return entry->getPrefix().equals(name); |
| 29 | } |
| 30 | |
| 31 | std::pair<shared_ptr<fib::Entry>, bool> |
| 32 | Fib::insert(const Name& prefix) |
| 33 | { |
| 34 | std::list<shared_ptr<fib::Entry> >::iterator it = std::find_if( |
| 35 | m_table.begin(), m_table.end(), |
| 36 | bind(&predicate_FibEntry_eq_Name, _1, prefix)); |
| 37 | if (it != m_table.end()) return std::make_pair(*it, false); |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 39 | shared_ptr<fib::Entry> entry = make_shared<fib::Entry>(prefix); |
| 40 | m_table.push_back(entry); |
| 41 | return std::make_pair(entry, true); |
| 42 | } |
| 43 | |
| 44 | static inline const shared_ptr<fib::Entry>& |
| 45 | accumulate_FibEntry_longestPrefixMatch( |
| 46 | const shared_ptr<fib::Entry>& bestMatch, |
| 47 | const shared_ptr<fib::Entry>& entry, const Name& name) |
| 48 | { |
| 49 | if (!entry->getPrefix().isPrefixOf(name)) return bestMatch; |
| 50 | if (bestMatch->getPrefix().size() < entry->getPrefix().size()) return entry; |
| 51 | return bestMatch; |
| 52 | } |
| 53 | |
| 54 | shared_ptr<fib::Entry> |
| 55 | Fib::findLongestPrefixMatch(const Name& prefix) const |
| 56 | { |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 57 | shared_ptr<fib::Entry> bestMatch = |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 58 | std::accumulate(m_table.begin(), m_table.end(), m_rootEntry, |
| 59 | bind(&accumulate_FibEntry_longestPrefixMatch, _1, _2, prefix)); |
| 60 | return bestMatch; |
| 61 | } |
| 62 | |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 63 | shared_ptr<fib::Entry> |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame^] | 64 | Fib::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
| 65 | { |
| 66 | return this->findLongestPrefixMatch(pitEntry.getName()); |
| 67 | } |
| 68 | |
| 69 | shared_ptr<fib::Entry> |
| 70 | Fib::findLongestPrefixMatch(const measurements::Entry& measurementsEntry) const |
| 71 | { |
| 72 | return this->findLongestPrefixMatch(measurementsEntry.getName()); |
| 73 | } |
| 74 | |
| 75 | shared_ptr<fib::Entry> |
Steve DiBenedetto | d5f8793 | 2014-02-05 15:11:39 -0700 | [diff] [blame] | 76 | Fib::findExactMatch(const Name& prefix) const |
| 77 | { |
| 78 | std::list<shared_ptr<fib::Entry> >::const_iterator it = |
| 79 | std::find_if(m_table.begin(), m_table.end(), |
| 80 | bind(&predicate_FibEntry_eq_Name, _1, prefix)); |
| 81 | |
| 82 | if (it != m_table.end()) |
| 83 | { |
| 84 | return *it; |
| 85 | } |
| 86 | return shared_ptr<fib::Entry>(); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Fib::remove(const Name& prefix) |
| 91 | { |
| 92 | m_table.remove_if(bind(&predicate_FibEntry_eq_Name, _1, prefix)); |
| 93 | } |
| 94 | |
Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 95 | static inline void |
| 96 | FibEntry_removeNextHop(shared_ptr<fib::Entry> entry, |
| 97 | shared_ptr<Face> face) |
| 98 | { |
| 99 | entry->removeNextHop(face); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | Fib::removeNextHopFromAllEntries(shared_ptr<Face> face) |
| 104 | { |
| 105 | std::for_each(m_table.begin(), m_table.end(), |
| 106 | bind(&FibEntry_removeNextHop, _1, face)); |
| 107 | } |
| 108 | |
| 109 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 110 | } // namespace nfd |