blob: e089a09d97551e291eeb09e17a0cce37ddd872b9 [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-entry.hpp"
27#include <algorithm>
28
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080029namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070030namespace pit {
31
Junxiao Shi57f0f312014-03-16 11:52:20 -070032const Name Entry::LOCALHOST_NAME("ndn:/localhost");
33const Name Entry::LOCALHOP_NAME("ndn:/localhop");
34
Junxiao Shicbba04c2014-01-26 14:21:22 -070035Entry::Entry(const Interest& interest)
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070036 : m_interest(interest.shared_from_this())
Junxiao Shicbba04c2014-01-26 14:21:22 -070037{
38}
39
40const Name&
41Entry::getName() const
42{
Alexander Afanasyev28d586a2014-07-10 20:10:54 -070043 return m_interest->getName();
Junxiao Shicbba04c2014-01-26 14:21:22 -070044}
45
46const InRecordCollection&
47Entry::getInRecords() const
48{
49 return m_inRecords;
50}
51
52const OutRecordCollection&
53Entry::getOutRecords() const
54{
55 return m_outRecords;
56}
57
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070058static inline bool
Junxiao Shi11bd9c22014-03-13 20:44:13 -070059predicate_InRecord_isLocal(const InRecord& inRecord)
60{
61 return inRecord.getFace()->isLocal();
62}
63
64bool
65Entry::hasLocalInRecord() const
66{
67 InRecordCollection::const_iterator it = std::find_if(
68 m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal);
69 return it != m_inRecords.end();
70}
71
72static inline bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070073predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070074{
Junxiao Shi57f0f312014-03-16 11:52:20 -070075 return faceRecord.getFace().get() == face;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070076}
77
78static inline bool
79predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070080 const Face* face, const time::steady_clock::TimePoint& now)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070081{
Junxiao Shi57f0f312014-03-16 11:52:20 -070082 return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070083}
84
85bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070086Entry::canForwardTo(const Face& face) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070087{
88 OutRecordCollection::const_iterator outIt = std::find_if(
89 m_outRecords.begin(), m_outRecords.end(),
Junxiao Shi57f0f312014-03-16 11:52:20 -070090 bind(&predicate_FaceRecord_Face, _1, &face));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070091 bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070092 outIt->getExpiry() >= time::steady_clock::now();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070093 if (hasUnexpiredOutRecord) {
94 return false;
95 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070096
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097 InRecordCollection::const_iterator inIt = std::find_if(
98 m_inRecords.begin(), m_inRecords.end(),
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070099 bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::steady_clock::now()));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700100 bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
Junxiao Shi57f0f312014-03-16 11:52:20 -0700101 if (!hasUnexpiredOtherInRecord) {
102 return false;
103 }
104
105 return !this->violatesScope(face);
106}
107
108bool
109Entry::violatesScope(const Face& face) const
110{
111 // /localhost scope
112 bool isViolatingLocalhost = !face.isLocal() &&
113 LOCALHOST_NAME.isPrefixOf(this->getName());
114 if (isViolatingLocalhost) {
115 return true;
116 }
117
118 // /localhop scope
119 bool isViolatingLocalhop = !face.isLocal() &&
120 LOCALHOP_NAME.isPrefixOf(this->getName()) &&
121 !this->hasLocalInRecord();
122 if (isViolatingLocalhop) {
123 return true;
124 }
125
126 return false;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700127}
128
Junxiao Shicbba04c2014-01-26 14:21:22 -0700129bool
Junxiao Shid3c792f2014-01-30 00:46:13 -0700130Entry::addNonce(uint32_t nonce)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700131{
Junxiao Shi651b75e2014-07-17 20:15:09 -0700132 return m_nonceList.add(nonce);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700133}
134
Junxiao Shicbba04c2014-01-26 14:21:22 -0700135InRecordCollection::iterator
136Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
137{
138 InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700139 m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700140 if (it == m_inRecords.end()) {
141 m_inRecords.push_front(InRecord(face));
142 it = m_inRecords.begin();
143 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700144
Junxiao Shicbba04c2014-01-26 14:21:22 -0700145 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700146 return it;
147}
148
Junxiao Shi66f91f82014-05-10 17:28:58 -0700149InRecordCollection::const_iterator
150Entry::getInRecord(shared_ptr<Face> face) const
151{
152 return std::find_if(m_inRecords.begin(), m_inRecords.end(),
153 bind(&predicate_FaceRecord_Face, _1, face.get()));
154}
155
Junxiao Shicbba04c2014-01-26 14:21:22 -0700156void
157Entry::deleteInRecords()
158{
159 m_inRecords.clear();
160}
161
162OutRecordCollection::iterator
163Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
164{
165 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700166 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700167 if (it == m_outRecords.end()) {
168 m_outRecords.push_front(OutRecord(face));
169 it = m_outRecords.begin();
170 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700171
Junxiao Shicbba04c2014-01-26 14:21:22 -0700172 it->update(interest);
Junxiao Shi651b75e2014-07-17 20:15:09 -0700173 m_nonceList.add(interest.getNonce());
Junxiao Shicbba04c2014-01-26 14:21:22 -0700174 return it;
175}
176
Junxiao Shi66f91f82014-05-10 17:28:58 -0700177OutRecordCollection::const_iterator
178Entry::getOutRecord(shared_ptr<Face> face) const
179{
180 return std::find_if(m_outRecords.begin(), m_outRecords.end(),
181 bind(&predicate_FaceRecord_Face, _1, face.get()));
182}
183
Junxiao Shicbba04c2014-01-26 14:21:22 -0700184void
185Entry::deleteOutRecord(shared_ptr<Face> face)
186{
187 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700188 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700189 if (it != m_outRecords.end()) {
190 m_outRecords.erase(it);
191 }
192}
193
Junxiao Shi57f0f312014-03-16 11:52:20 -0700194static inline bool
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700195predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, const time::steady_clock::TimePoint& now)
Junxiao Shi57f0f312014-03-16 11:52:20 -0700196{
197 return faceRecord.getExpiry() >= now;
198}
199
200bool
201Entry::hasUnexpiredOutRecords() const
202{
203 OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(),
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700204 m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::steady_clock::now()));
Junxiao Shi57f0f312014-03-16 11:52:20 -0700205 return it != m_outRecords.end();
206}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700207
208} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800209} // namespace nfd