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 | |
| 13 | Entry::Entry(const Interest& interest) |
| 14 | : m_interest(interest) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | const Name& |
| 19 | Entry::getName() const |
| 20 | { |
| 21 | return m_interest.getName(); |
| 22 | } |
| 23 | |
| 24 | const InRecordCollection& |
| 25 | Entry::getInRecords() const |
| 26 | { |
| 27 | return m_inRecords; |
| 28 | } |
| 29 | |
| 30 | const OutRecordCollection& |
| 31 | Entry::getOutRecords() const |
| 32 | { |
| 33 | return m_outRecords; |
| 34 | } |
| 35 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 36 | static inline bool |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame^] | 37 | predicate_InRecord_isLocal(const InRecord& inRecord) |
| 38 | { |
| 39 | return inRecord.getFace()->isLocal(); |
| 40 | } |
| 41 | |
| 42 | bool |
| 43 | Entry::hasLocalInRecord() const |
| 44 | { |
| 45 | InRecordCollection::const_iterator it = std::find_if( |
| 46 | m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal); |
| 47 | return it != m_inRecords.end(); |
| 48 | } |
| 49 | |
| 50 | static inline bool |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 51 | predicate_FaceRecord_Face(const FaceRecord& faceRecord, shared_ptr<Face> face) |
| 52 | { |
| 53 | return faceRecord.getFace() == face; |
| 54 | } |
| 55 | |
| 56 | static inline bool |
| 57 | predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord, |
| 58 | shared_ptr<Face> face, time::Point now) |
| 59 | { |
| 60 | return faceRecord.getFace() != face && faceRecord.getExpiry() >= now; |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | Entry::canForwardTo(shared_ptr<Face> face) const |
| 65 | { |
| 66 | OutRecordCollection::const_iterator outIt = std::find_if( |
| 67 | m_outRecords.begin(), m_outRecords.end(), |
| 68 | bind(&predicate_FaceRecord_Face, _1, face)); |
| 69 | bool hasUnexpiredOutRecord = outIt != m_outRecords.end() && |
| 70 | outIt->getExpiry() >= time::now(); |
| 71 | if (hasUnexpiredOutRecord) { |
| 72 | return false; |
| 73 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame^] | 74 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 75 | InRecordCollection::const_iterator inIt = std::find_if( |
| 76 | m_inRecords.begin(), m_inRecords.end(), |
| 77 | bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, face, time::now())); |
| 78 | bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end(); |
| 79 | return hasUnexpiredOtherInRecord; |
| 80 | } |
| 81 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 82 | bool |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 83 | Entry::addNonce(uint32_t nonce) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 84 | { |
Junxiao Shi | d3c792f | 2014-01-30 00:46:13 -0700 | [diff] [blame] | 85 | std::pair<std::set<uint32_t>::iterator, bool> insertResult = |
| 86 | m_nonces.insert(nonce); |
| 87 | |
| 88 | return insertResult.second; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 91 | InRecordCollection::iterator |
| 92 | Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest) |
| 93 | { |
| 94 | InRecordCollection::iterator it = std::find_if(m_inRecords.begin(), |
| 95 | m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face)); |
| 96 | if (it == m_inRecords.end()) { |
| 97 | m_inRecords.push_front(InRecord(face)); |
| 98 | it = m_inRecords.begin(); |
| 99 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame^] | 100 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 101 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 102 | return it; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Entry::deleteInRecords() |
| 107 | { |
| 108 | m_inRecords.clear(); |
| 109 | } |
| 110 | |
| 111 | OutRecordCollection::iterator |
| 112 | Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest) |
| 113 | { |
| 114 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
| 115 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face)); |
| 116 | if (it == m_outRecords.end()) { |
| 117 | m_outRecords.push_front(OutRecord(face)); |
| 118 | it = m_outRecords.begin(); |
| 119 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame^] | 120 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 121 | it->update(interest); |
| 122 | m_nonces.insert(interest.getNonce()); |
| 123 | return it; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | Entry::deleteOutRecord(shared_ptr<Face> face) |
| 128 | { |
| 129 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
| 130 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face)); |
| 131 | if (it != m_outRecords.end()) { |
| 132 | m_outRecords.erase(it); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | } // namespace pit |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 138 | } // namespace nfd |