Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | |
| 26 | #include "pit.hpp" |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 28 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 30 | Pit::Pit(NameTree& nameTree) |
| 31 | : m_nameTree(nameTree) |
| 32 | , m_nItems(0) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | Pit::~Pit() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | static inline bool |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 41 | predicate_NameTreeEntry_hasPitEntry(const name_tree::Entry& entry) |
| 42 | { |
| 43 | return entry.hasPitEntries(); |
| 44 | } |
| 45 | |
| 46 | static inline bool |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 47 | operator==(const Exclude& a, const Exclude& b) |
| 48 | { |
| 49 | const Block& aBlock = a.wireEncode(); |
| 50 | const Block& bBlock = b.wireEncode(); |
| 51 | return aBlock.size() == bBlock.size() && |
| 52 | 0 == memcmp(aBlock.wire(), bBlock.wire(), aBlock.size()); |
| 53 | } |
| 54 | |
| 55 | static inline bool |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 56 | predicate_PitEntry_similar_Interest(const shared_ptr<pit::Entry>& entry, |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 57 | const Interest& interest) |
| 58 | { |
| 59 | const Interest& pi = entry->getInterest(); |
| 60 | return pi.getName().equals(interest.getName()) && |
| 61 | pi.getMinSuffixComponents() == interest.getMinSuffixComponents() && |
| 62 | pi.getMaxSuffixComponents() == interest.getMaxSuffixComponents() && |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 63 | pi.getPublisherPublicKeyLocator() == interest.getPublisherPublicKeyLocator() && |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 64 | pi.getExclude() == interest.getExclude() && |
| 65 | pi.getChildSelector() == interest.getChildSelector() && |
| 66 | pi.getMustBeFresh() == interest.getMustBeFresh(); |
| 67 | } |
| 68 | |
| 69 | std::pair<shared_ptr<pit::Entry>, bool> |
| 70 | Pit::insert(const Interest& interest) |
| 71 | { |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 72 | // - first lookup() the Interest Name in the NameTree, which will creates all |
| 73 | // the intermedia nodes, starting from the shortest prefix. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 74 | // - if it is guaranteed that this Interest already has a NameTree Entry, we |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 75 | // could use findExactMatch() instead. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 76 | // - Alternatively, we could try to do findExactMatch() first, if not found, |
| 77 | // then do lookup(). |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 78 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName()); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 79 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 80 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 81 | const std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 82 | |
| 83 | // then check if this Interest is already in the PIT entries |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 84 | std::vector<shared_ptr<pit::Entry> >::const_iterator it = |
| 85 | std::find_if(pitEntries.begin(), pitEntries.end(), |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 86 | bind(&predicate_PitEntry_similar_Interest, _1, cref(interest))); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 87 | |
| 88 | if (it != pitEntries.end()) |
| 89 | { |
| 90 | return std::make_pair(*it, false); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest); |
| 95 | nameTreeEntry->insertPitEntry(entry); |
| 96 | |
| 97 | // Increase m_nItmes only if we create a new PIT Entry |
| 98 | m_nItems++; |
| 99 | |
| 100 | return std::make_pair(entry, true); |
| 101 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | shared_ptr<pit::DataMatchResult> |
| 105 | Pit::findAllDataMatches(const Data& data) const |
| 106 | { |
| 107 | shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 108 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 109 | for (NameTree::const_iterator it = |
| 110 | m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry); |
| 111 | it != m_nameTree.end(); it++) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 112 | { |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 113 | const std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 114 | for (size_t i = 0; i < pitEntries.size(); i++) |
| 115 | { |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 116 | if (pitEntries[i]->getInterest().matchesData(data)) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 117 | result->push_back(pitEntries[i]); |
| 118 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 119 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 120 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 121 | return result; |
| 122 | } |
| 123 | |
| 124 | void |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 125 | Pit::erase(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 126 | { |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 127 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 128 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 129 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 130 | nameTreeEntry->erasePitEntry(pitEntry); |
| 131 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 132 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 133 | --m_nItems; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 136 | } // namespace nfd |