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 |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 47 | predicate_PitEntry_similar_Interest(const shared_ptr<pit::Entry>& entry, |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 48 | const Interest& interest) |
| 49 | { |
| 50 | const Interest& pi = entry->getInterest(); |
| 51 | return pi.getName().equals(interest.getName()) && |
| 52 | pi.getMinSuffixComponents() == interest.getMinSuffixComponents() && |
| 53 | pi.getMaxSuffixComponents() == interest.getMaxSuffixComponents() && |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 54 | pi.getPublisherPublicKeyLocator() == interest.getPublisherPublicKeyLocator() && |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 55 | pi.getExclude() == interest.getExclude() && |
| 56 | pi.getChildSelector() == interest.getChildSelector() && |
| 57 | pi.getMustBeFresh() == interest.getMustBeFresh(); |
| 58 | } |
| 59 | |
| 60 | std::pair<shared_ptr<pit::Entry>, bool> |
| 61 | Pit::insert(const Interest& interest) |
| 62 | { |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 63 | // - first lookup() the Interest Name in the NameTree, which will creates all |
| 64 | // the intermedia nodes, starting from the shortest prefix. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 65 | // - 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] | 66 | // could use findExactMatch() instead. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 67 | // - Alternatively, we could try to do findExactMatch() first, if not found, |
| 68 | // then do lookup(). |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 69 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName()); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 70 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 71 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 72 | const std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 73 | |
| 74 | // then check if this Interest is already in the PIT entries |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 75 | std::vector<shared_ptr<pit::Entry> >::const_iterator it = |
| 76 | std::find_if(pitEntries.begin(), pitEntries.end(), |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 77 | bind(&predicate_PitEntry_similar_Interest, _1, cref(interest))); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 78 | |
| 79 | if (it != pitEntries.end()) |
| 80 | { |
| 81 | return std::make_pair(*it, false); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | shared_ptr<pit::Entry> entry = make_shared<pit::Entry>(interest); |
| 86 | nameTreeEntry->insertPitEntry(entry); |
| 87 | |
| 88 | // Increase m_nItmes only if we create a new PIT Entry |
| 89 | m_nItems++; |
| 90 | |
| 91 | return std::make_pair(entry, true); |
| 92 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | shared_ptr<pit::DataMatchResult> |
| 96 | Pit::findAllDataMatches(const Data& data) const |
| 97 | { |
| 98 | shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 99 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 100 | for (NameTree::const_iterator it = |
| 101 | m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry); |
| 102 | it != m_nameTree.end(); it++) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 103 | { |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 104 | const std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries(); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 105 | for (size_t i = 0; i < pitEntries.size(); i++) |
| 106 | { |
Junxiao Shi | 30d3599 | 2014-04-03 14:51:58 -0700 | [diff] [blame] | 107 | if (pitEntries[i]->getInterest().matchesData(data)) |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 108 | result->push_back(pitEntries[i]); |
| 109 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 110 | } |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 111 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 112 | return result; |
| 113 | } |
| 114 | |
| 115 | void |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 116 | Pit::erase(shared_ptr<pit::Entry> pitEntry) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 117 | { |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 118 | shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*pitEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 119 | BOOST_ASSERT(static_cast<bool>(nameTreeEntry)); |
| 120 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 121 | nameTreeEntry->erasePitEntry(pitEntry); |
| 122 | m_nameTree.eraseEntryIfEmpty(nameTreeEntry); |
Haowei Yuan | 78c84d1 | 2014-02-27 15:35:13 -0600 | [diff] [blame] | 123 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 124 | --m_nItems; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 127 | } // namespace nfd |