blob: eb3e453c1c7c126a87f479a48957a2ac7ad6bc66 [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
Junxiao Shicbba04c2014-01-26 14:21:22 -070047operator==(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
55static inline bool
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070056predicate_PitEntry_similar_Interest(const shared_ptr<pit::Entry>& entry,
Junxiao Shicbba04c2014-01-26 14:21:22 -070057 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 Shi30d35992014-04-03 14:51:58 -070063 pi.getPublisherPublicKeyLocator() == interest.getPublisherPublicKeyLocator() &&
Junxiao Shicbba04c2014-01-26 14:21:22 -070064 pi.getExclude() == interest.getExclude() &&
65 pi.getChildSelector() == interest.getChildSelector() &&
66 pi.getMustBeFresh() == interest.getMustBeFresh();
67}
68
69std::pair<shared_ptr<pit::Entry>, bool>
70Pit::insert(const Interest& interest)
71{
Haowei Yuan78c84d12014-02-27 15:35:13 -060072 // - first lookup() the Interest Name in the NameTree, which will creates all
73 // the intermedia nodes, starting from the shortest prefix.
Haowei Yuane1079fc2014-03-08 14:41:25 -060074 // - if it is guaranteed that this Interest already has a NameTree Entry, we
Haowei Yuan78c84d12014-02-27 15:35:13 -060075 // could use findExactMatch() instead.
Haowei Yuane1079fc2014-03-08 14:41:25 -060076 // - Alternatively, we could try to do findExactMatch() first, if not found,
77 // then do lookup().
Haowei Yuan78c84d12014-02-27 15:35:13 -060078 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(interest.getName());
Haowei Yuan78c84d12014-02-27 15:35:13 -060079 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
80
Junxiao Shi9f7455b2014-04-07 21:02:16 -070081 const std::vector<shared_ptr<pit::Entry> >& pitEntries = nameTreeEntry->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -060082
83 // then check if this Interest is already in the PIT entries
Junxiao Shi9f7455b2014-04-07 21:02:16 -070084 std::vector<shared_ptr<pit::Entry> >::const_iterator it =
85 std::find_if(pitEntries.begin(), pitEntries.end(),
Alexander Afanasyevf6980282014-05-13 18:28:40 -070086 bind(&predicate_PitEntry_similar_Interest, _1, cref(interest)));
Haowei Yuan78c84d12014-02-27 15:35:13 -060087
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 Shicbba04c2014-01-26 14:21:22 -0700102}
103
104shared_ptr<pit::DataMatchResult>
105Pit::findAllDataMatches(const Data& data) const
106{
107 shared_ptr<pit::DataMatchResult> result = make_shared<pit::DataMatchResult>();
Haowei Yuan78c84d12014-02-27 15:35:13 -0600108
Haowei Yuane1079fc2014-03-08 14:41:25 -0600109 for (NameTree::const_iterator it =
110 m_nameTree.findAllMatches(data.getName(), &predicate_NameTreeEntry_hasPitEntry);
111 it != m_nameTree.end(); it++)
Haowei Yuan78c84d12014-02-27 15:35:13 -0600112 {
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700113 const std::vector<shared_ptr<pit::Entry> >& pitEntries = it->getPitEntries();
Haowei Yuan78c84d12014-02-27 15:35:13 -0600114 for (size_t i = 0; i < pitEntries.size(); i++)
115 {
Junxiao Shi30d35992014-04-03 14:51:58 -0700116 if (pitEntries[i]->getInterest().matchesData(data))
Haowei Yuan78c84d12014-02-27 15:35:13 -0600117 result->push_back(pitEntries[i]);
118 }
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119 }
Haowei Yuan78c84d12014-02-27 15:35:13 -0600120
Junxiao Shicbba04c2014-01-26 14:21:22 -0700121 return result;
122}
123
124void
Haowei Yuan78c84d12014-02-27 15:35:13 -0600125Pit::erase(shared_ptr<pit::Entry> pitEntry)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700126{
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700127 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*pitEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600128 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
129
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700130 nameTreeEntry->erasePitEntry(pitEntry);
131 m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
Haowei Yuan78c84d12014-02-27 15:35:13 -0600132
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700133 --m_nItems;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700134}
135
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800136} // namespace nfd