blob: 21b4321d7e3768dcffce4c096829155dfb8c9ce2 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev28d586a2014-07-10 20:10:54 -07003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyev28d586a2014-07-10 20:10:54 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "pit.hpp"
Junxiao Shicbba04c2014-01-26 14:21:22 -070027
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070029
Junxiao Shi30d35992014-04-03 14:51:58 -070030Pit::Pit(NameTree& nameTree)
31 : m_nameTree(nameTree)
32 , m_nItems(0)
Junxiao Shicbba04c2014-01-26 14:21:22 -070033{
34}
35
36Pit::~Pit()
37{
38}
39
40static inline bool
Haowei Yuane1079fc2014-03-08 14:41:25 -060041predicate_NameTreeEntry_hasPitEntry(const name_tree::Entry& entry)
42{
43 return entry.hasPitEntries();
44}
45
46static inline bool
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070047predicate_PitEntry_similar_Interest(const shared_ptr<pit::Entry>& entry,
Junxiao Shicbba04c2014-01-26 14:21:22 -070048 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 Shi30d35992014-04-03 14:51:58 -070054 pi.getPublisherPublicKeyLocator() == interest.getPublisherPublicKeyLocator() &&
Junxiao Shicbba04c2014-01-26 14:21:22 -070055 pi.getExclude() == interest.getExclude() &&
56 pi.getChildSelector() == interest.getChildSelector() &&
57 pi.getMustBeFresh() == interest.getMustBeFresh();
58}
59
60std::pair<shared_ptr<pit::Entry>, bool>
61Pit::insert(const Interest& interest)
62{
Haowei Yuan78c84d12014-02-27 15:35:13 -060063 // - first lookup() the Interest Name in the NameTree, which will creates all
64 // the intermedia nodes, starting from the shortest prefix.
Haowei Yuane1079fc2014-03-08 14:41:25 -060065 // - if it is guaranteed that this Interest already has a NameTree Entry, we
Haowei Yuan78c84d12014-02-27 15:35:13 -060066 // could use findExactMatch() instead.
Haowei Yuane1079fc2014-03-08 14:41:25 -060067 // - Alternatively, we could try to do findExactMatch() first, if not found,
68 // then do lookup().
Haowei Yuan78c84d12014-02-27 15:35:13 -060069 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName());
Haowei Yuan78c84d12014-02-27 15:35:13 -060070 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
71
Junxiao Shi9f7455b2014-04-07 21:02:16 -070072 const std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -060073
74 // then check if this Interest is already in the PIT entries
Junxiao Shi9f7455b2014-04-07 21:02:16 -070075 std::vector<shared_ptr<pit::Entry> >::const_iterator it =
76 std::find_if(pitEntries.begin(), pitEntries.end(),
Alexander Afanasyevf6980282014-05-13 18:28:40 -070077 bind(&predicate_PitEntry_similar_Interest, _1, cref(interest)));
Haowei Yuan78c84d12014-02-27 15:35:13 -060078
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 Shicbba04c2014-01-26 14:21:22 -070093}
94
95shared_ptr<pit::DataMatchResult>
96Pit::findAllDataMatches(const Data& data) const
97{
98 shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>();
Haowei Yuan78c84d12014-02-27 15:35:13 -060099
Haowei Yuane1079fc2014-03-08 14:41:25 -0600100 for (NameTree::const_iterator it =
101 m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry);
102 it != m_nameTree.end(); it++)
Haowei Yuan78c84d12014-02-27 15:35:13 -0600103 {
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700104 const std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -0600105 for (size_t i = 0; i < pitEntries.size(); i++)
106 {
Junxiao Shi30d35992014-04-03 14:51:58 -0700107 if (pitEntries[i]->getInterest().matchesData(data))
Haowei Yuan78c84d12014-02-27 15:35:13 -0600108 result->push_back(pitEntries[i]);
109 }
Junxiao Shicbba04c2014-01-26 14:21:22 -0700110 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600111
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112 return result;
113}
114
115void
Haowei Yuan78c84d12014-02-27 15:35:13 -0600116Pit::erase(shared_ptr<pit::Entry> pitEntry)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700117{
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700118 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600119 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
120
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700121 nameTreeEntry->erasePitEntry(pitEntry);
122 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600123
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700124 --m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700125}
126
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800127} // namespace nfd