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