blob: 86596df552cae244fd8e110acc782f63ac81c130 [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "pit-entry.hpp"
8#include <algorithm>
9
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080010namespace nfd {
Junxiao Shicbba04c2014-01-26 14:21:22 -070011namespace pit {
12
Junxiao Shi57f0f312014-03-16 11:52:20 -070013const Name Entry::LOCALHOST_NAME("ndn:/localhost");
14const Name Entry::LOCALHOP_NAME("ndn:/localhop");
15
Junxiao Shicbba04c2014-01-26 14:21:22 -070016Entry::Entry(const Interest& interest)
17 : m_interest(interest)
18{
19}
20
21const Name&
22Entry::getName() const
23{
24 return m_interest.getName();
25}
26
27const InRecordCollection&
28Entry::getInRecords() const
29{
30 return m_inRecords;
31}
32
33const OutRecordCollection&
34Entry::getOutRecords() const
35{
36 return m_outRecords;
37}
38
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070039static inline bool
Junxiao Shi11bd9c22014-03-13 20:44:13 -070040predicate_InRecord_isLocal(const InRecord& inRecord)
41{
42 return inRecord.getFace()->isLocal();
43}
44
45bool
46Entry::hasLocalInRecord() const
47{
48 InRecordCollection::const_iterator it = std::find_if(
49 m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal);
50 return it != m_inRecords.end();
51}
52
53static inline bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070054predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070055{
Junxiao Shi57f0f312014-03-16 11:52:20 -070056 return faceRecord.getFace().get() == face;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070057}
58
59static inline bool
60predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord,
Junxiao Shi57f0f312014-03-16 11:52:20 -070061 const Face* face, time::Point now)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070062{
Junxiao Shi57f0f312014-03-16 11:52:20 -070063 return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070064}
65
66bool
Junxiao Shi57f0f312014-03-16 11:52:20 -070067Entry::canForwardTo(const Face& face) const
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070068{
69 OutRecordCollection::const_iterator outIt = std::find_if(
70 m_outRecords.begin(), m_outRecords.end(),
Junxiao Shi57f0f312014-03-16 11:52:20 -070071 bind(&predicate_FaceRecord_Face, _1, &face));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070072 bool hasUnexpiredOutRecord = outIt != m_outRecords.end() &&
73 outIt->getExpiry() >= time::now();
74 if (hasUnexpiredOutRecord) {
75 return false;
76 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070077
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070078 InRecordCollection::const_iterator inIt = std::find_if(
79 m_inRecords.begin(), m_inRecords.end(),
Junxiao Shi57f0f312014-03-16 11:52:20 -070080 bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::now()));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070081 bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end();
Junxiao Shi57f0f312014-03-16 11:52:20 -070082 if (!hasUnexpiredOtherInRecord) {
83 return false;
84 }
85
86 return !this->violatesScope(face);
87}
88
89bool
90Entry::violatesScope(const Face& face) const
91{
92 // /localhost scope
93 bool isViolatingLocalhost = !face.isLocal() &&
94 LOCALHOST_NAME.isPrefixOf(this->getName());
95 if (isViolatingLocalhost) {
96 return true;
97 }
98
99 // /localhop scope
100 bool isViolatingLocalhop = !face.isLocal() &&
101 LOCALHOP_NAME.isPrefixOf(this->getName()) &&
102 !this->hasLocalInRecord();
103 if (isViolatingLocalhop) {
104 return true;
105 }
106
107 return false;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700108}
109
Junxiao Shicbba04c2014-01-26 14:21:22 -0700110bool
Junxiao Shid3c792f2014-01-30 00:46:13 -0700111Entry::addNonce(uint32_t nonce)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112{
Junxiao Shid3c792f2014-01-30 00:46:13 -0700113 std::pair<std::set<uint32_t>::iterator, bool> insertResult =
114 m_nonces.insert(nonce);
115
116 return insertResult.second;
Junxiao Shicbba04c2014-01-26 14:21:22 -0700117}
118
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119InRecordCollection::iterator
120Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest)
121{
122 InRecordCollection::iterator it = std::find_if(m_inRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700123 m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700124 if (it == m_inRecords.end()) {
125 m_inRecords.push_front(InRecord(face));
126 it = m_inRecords.begin();
127 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700128
Junxiao Shicbba04c2014-01-26 14:21:22 -0700129 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700130 return it;
131}
132
133void
134Entry::deleteInRecords()
135{
136 m_inRecords.clear();
137}
138
139OutRecordCollection::iterator
140Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest)
141{
142 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700143 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700144 if (it == m_outRecords.end()) {
145 m_outRecords.push_front(OutRecord(face));
146 it = m_outRecords.begin();
147 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700148
Junxiao Shicbba04c2014-01-26 14:21:22 -0700149 it->update(interest);
150 m_nonces.insert(interest.getNonce());
151 return it;
152}
153
154void
155Entry::deleteOutRecord(shared_ptr<Face> face)
156{
157 OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(),
Junxiao Shi57f0f312014-03-16 11:52:20 -0700158 m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get()));
Junxiao Shicbba04c2014-01-26 14:21:22 -0700159 if (it != m_outRecords.end()) {
160 m_outRecords.erase(it);
161 }
162}
163
Junxiao Shi57f0f312014-03-16 11:52:20 -0700164static inline bool
165predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, time::Point now)
166{
167 return faceRecord.getExpiry() >= now;
168}
169
170bool
171Entry::hasUnexpiredOutRecords() const
172{
173 OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(),
174 m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::now()));
175 return it != m_outRecords.end();
176}
Junxiao Shicbba04c2014-01-26 14:21:22 -0700177
178} // namespace pit
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800179} // namespace nfd