Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 10 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 11 | namespace pit { |
| 12 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 13 | const Name Entry::LOCALHOST_NAME("ndn:/localhost"); |
| 14 | const Name Entry::LOCALHOP_NAME("ndn:/localhop"); |
| 15 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 16 | Entry::Entry(const Interest& interest) |
| 17 | : m_interest(interest) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | const Name& |
| 22 | Entry::getName() const |
| 23 | { |
| 24 | return m_interest.getName(); |
| 25 | } |
| 26 | |
| 27 | const InRecordCollection& |
| 28 | Entry::getInRecords() const |
| 29 | { |
| 30 | return m_inRecords; |
| 31 | } |
| 32 | |
| 33 | const OutRecordCollection& |
| 34 | Entry::getOutRecords() const |
| 35 | { |
| 36 | return m_outRecords; |
| 37 | } |
| 38 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 39 | static inline bool |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 40 | predicate_InRecord_isLocal(const InRecord& inRecord) |
| 41 | { |
| 42 | return inRecord.getFace()->isLocal(); |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | Entry::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 | |
| 53 | static inline bool |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 54 | predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 55 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 56 | return faceRecord.getFace().get() == face; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline bool |
| 60 | predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 61 | const Face* face, const time::steady_clock::TimePoint& now) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 62 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 63 | return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 67 | Entry::canForwardTo(const Face& face) const |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 68 | { |
| 69 | OutRecordCollection::const_iterator outIt = std::find_if( |
| 70 | m_outRecords.begin(), m_outRecords.end(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 71 | bind(&predicate_FaceRecord_Face, _1, &face)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 72 | bool hasUnexpiredOutRecord = outIt != m_outRecords.end() && |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 73 | outIt->getExpiry() >= time::steady_clock::now(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 74 | if (hasUnexpiredOutRecord) { |
| 75 | return false; |
| 76 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 77 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 78 | InRecordCollection::const_iterator inIt = std::find_if( |
| 79 | m_inRecords.begin(), m_inRecords.end(), |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 80 | bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::steady_clock::now())); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 81 | bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end(); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 82 | if (!hasUnexpiredOtherInRecord) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return !this->violatesScope(face); |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | Entry::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 Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 110 | bool |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 111 | Entry::addNonce(uint32_t nonce) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 112 | { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 113 | std::pair<std::set<uint32_t>::iterator, bool> insertResult = |
| 114 | m_nonces.insert(nonce); |
| 115 | |
| 116 | return insertResult.second; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 119 | InRecordCollection::iterator |
| 120 | Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest) |
| 121 | { |
| 122 | InRecordCollection::iterator it = std::find_if(m_inRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 123 | m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 124 | if (it == m_inRecords.end()) { |
| 125 | m_inRecords.push_front(InRecord(face)); |
| 126 | it = m_inRecords.begin(); |
| 127 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 129 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 130 | return it; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | Entry::deleteInRecords() |
| 135 | { |
| 136 | m_inRecords.clear(); |
| 137 | } |
| 138 | |
| 139 | OutRecordCollection::iterator |
| 140 | Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest) |
| 141 | { |
| 142 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 143 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 144 | if (it == m_outRecords.end()) { |
| 145 | m_outRecords.push_front(OutRecord(face)); |
| 146 | it = m_outRecords.begin(); |
| 147 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 148 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 149 | it->update(interest); |
| 150 | m_nonces.insert(interest.getNonce()); |
| 151 | return it; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | Entry::deleteOutRecord(shared_ptr<Face> face) |
| 156 | { |
| 157 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 158 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 159 | if (it != m_outRecords.end()) { |
| 160 | m_outRecords.erase(it); |
| 161 | } |
| 162 | } |
| 163 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 164 | static inline bool |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 165 | predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, const time::steady_clock::TimePoint& now) |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 166 | { |
| 167 | return faceRecord.getExpiry() >= now; |
| 168 | } |
| 169 | |
| 170 | bool |
| 171 | Entry::hasUnexpiredOutRecords() const |
| 172 | { |
| 173 | OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(), |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 174 | m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::steady_clock::now())); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 175 | return it != m_outRecords.end(); |
| 176 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 177 | |
| 178 | } // namespace pit |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 179 | } // namespace nfd |