blob: e607dba4f7e7dbf67cf88b3df30ccb70a4616948 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
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 Shicbba04c2014-01-26 14:21:22 -070024
25#include "pit-entry.hpp"
26#include <algorithm>
27
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080028namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070029namespace pit {
30
Junxiao Shi57f0f312014-03-16 11:52:20 -070031const Name Entry::LOCALHOST_NAME("ndn:/localhost");
32const Name Entry::LOCALHOP_NAME("ndn:/localhop");
33
Junxiao Shicbba04c2014-01-26 14:21:22 -070034Entry::Entry(const Interest& interest)
35 : m_interest(interest)
36{
37}
38
39const Name&
40Entry::getName() const
41{
42 return m_interest.getName();
43}
44
45const InRecordCollection&
46Entry::getInRecords() const
47{
48 return m_inRecords;
49}
50
51const OutRecordCollection&
52Entry::getOutRecords() const
53{
54 return m_outRecords;
55}
56
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070057static inline bool
Junxiao Shi11bd9c22014-03-13 20:44:13 -070058predicate_InRecord_isLocal(const InRecord& inRecord)
59{
60 return inRecord.getFace()->isLocal();
61}
62
63bool
64Entry::hasLocalInRecord() const
65{
66 InRecordCollection::const_iterator it = std::find_if(
67 m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal);
68 return it != m_inRecords.end();
69}
70
71static inline bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070072predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070073{
Junxiao Shi57f0f312014-03-16 11:52:20 -070074 return faceRecord.getFace().get() == face;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070075}
76
77static inline bool
78predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070079 const Face* face, const time::steady_clock::TimePoint& now)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070080{
Junxiao Shi57f0f312014-03-16 11:52:20 -070081 return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070082}
83
84bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070085Entry::canForwardTo(const Face& face) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070086{
87 OutRecordCollection::const_iterator outIt = std::find_if(
88 m_outRecords.begin(), m_outRecords.end(),
Junxiao Shi57f0f312014-03-16 11:52:20 -070089 bind(&predicate_FaceRecord_Face, _1, &face));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090 bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070091 outIt->getExpiry() >= time::steady_clock::now();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070092 if (hasUnexpiredOutRecord) {
93 return false;
94 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070095
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096 InRecordCollection::const_iterator inIt = std::find_if(
97 m_inRecords.begin(), m_inRecords.end(),
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070098 bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::steady_clock::now()));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070099 bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
Junxiao Shi57f0f312014-03-16 11:52:20 -0700100 if (!hasUnexpiredOtherInRecord) {
101 return false;
102 }
103
104 return !this->violatesScope(face);
105}
106
107bool
108Entry::violatesScope(const Face& face) const
109{
110 // /localhost scope
111 bool isViolatingLocalhost = !face.isLocal() &&
112 LOCALHOST_NAME.isPrefixOf(this->getName());
113 if (isViolatingLocalhost) {
114 return true;
115 }
116
117 // /localhop scope
118 bool isViolatingLocalhop = !face.isLocal() &&
119 LOCALHOP_NAME.isPrefixOf(this->getName()) &&
120 !this->hasLocalInRecord();
121 if (isViolatingLocalhop) {
122 return true;
123 }
124
125 return false;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700126}
127
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128bool
Junxiao Shid3c792f2014-01-30 00:46:13 -0700129Entry::addNonce(uint32_t nonce)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130{
Junxiao Shid3c792f2014-01-30 00:46:13 -0700131 std::pair<std::set<uint32_t>::iterator, bool> insertResult =
132 m_nonces.insert(nonce);
133
134 return insertResult.second;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700135}
136
Junxiao Shicbba04c2014-01-26 14:21:22 -0700137InRecordCollection::iterator
138Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
139{
140 InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700141 m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700142 if (it == m_inRecords.end()) {
143 m_inRecords.push_front(InRecord(face));
144 it = m_inRecords.begin();
145 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700146
Junxiao Shicbba04c2014-01-26 14:21:22 -0700147 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700148 return it;
149}
150
151void
152Entry::deleteInRecords()
153{
154 m_inRecords.clear();
155}
156
157OutRecordCollection::iterator
158Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
159{
160 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700161 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700162 if (it == m_outRecords.end()) {
163 m_outRecords.push_front(OutRecord(face));
164 it = m_outRecords.begin();
165 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700166
Junxiao Shicbba04c2014-01-26 14:21:22 -0700167 it->update(interest);
168 m_nonces.insert(interest.getNonce());
169 return it;
170}
171
172void
173Entry::deleteOutRecord(shared_ptr<Face> face)
174{
175 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700176 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700177 if (it != m_outRecords.end()) {
178 m_outRecords.erase(it);
179 }
180}
181
Junxiao Shi57f0f312014-03-16 11:52:20 -0700182static inline bool
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700183predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, const time::steady_clock::TimePoint& now)
Junxiao Shi57f0f312014-03-16 11:52:20 -0700184{
185 return faceRecord.getExpiry() >= now;
186}
187
188bool
189Entry::hasUnexpiredOutRecords() const
190{
191 OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(),
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700192 m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::steady_clock::now()));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700193 return it != m_outRecords.end();
194}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700195
196} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800197} // namespace nfd