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