Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [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 | * Author: Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
| 9 | #include "cs-entry.hpp" |
| 10 | |
| 11 | namespace nfd { |
| 12 | namespace cs { |
| 13 | |
| 14 | NFD_LOG_INIT("CsEntry"); |
| 15 | |
| 16 | Entry::Entry(const Data& data, bool isUnsolicited) |
| 17 | : m_dataPacket(data.shared_from_this()) |
| 18 | , m_isUnsolicited(isUnsolicited) |
| 19 | , m_wasRefreshedByDuplicate(false) |
| 20 | , m_nameWithDigest(data.getName()) |
| 21 | { |
| 22 | updateStaleTime(); |
| 23 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 24 | } |
| 25 | |
| 26 | |
| 27 | Entry::~Entry() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | const Name& |
| 32 | Entry::getName() const |
| 33 | { |
| 34 | return m_nameWithDigest; |
| 35 | } |
| 36 | |
| 37 | const Data& |
| 38 | Entry::getData() const |
| 39 | { |
| 40 | return *m_dataPacket; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | Entry::setData(const Data& data) |
| 45 | { |
| 46 | /// \todo This method may not be necessary (if it is real duplicate, |
| 47 | /// there is no reason to recalculate the same digest |
| 48 | |
| 49 | m_dataPacket = data.shared_from_this(); |
| 50 | m_digest.reset(); |
| 51 | m_wasRefreshedByDuplicate = true; |
| 52 | |
| 53 | updateStaleTime(); |
| 54 | |
| 55 | m_nameWithDigest = data.getName(); |
| 56 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | Entry::setData(const Data& data, const ndn::ConstBufferPtr& digest) |
| 61 | { |
| 62 | /// \todo This method may not be necessary (if it is real duplicate, |
| 63 | /// there is no reason to recalculate the same digest |
| 64 | |
| 65 | m_dataPacket = data.shared_from_this(); |
| 66 | m_digest = digest; |
| 67 | m_wasRefreshedByDuplicate = true; |
| 68 | |
| 69 | updateStaleTime(); |
| 70 | |
| 71 | m_nameWithDigest = data.getName(); |
| 72 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 73 | } |
| 74 | |
| 75 | bool |
| 76 | Entry::isUnsolicited() const |
| 77 | { |
| 78 | return m_isUnsolicited; |
| 79 | } |
| 80 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 81 | const time::steady_clock::TimePoint& |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 82 | Entry::getStaleTime() const |
| 83 | { |
| 84 | return m_staleAt; |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | Entry::updateStaleTime() |
| 89 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 90 | m_staleAt = time::steady_clock::now() + m_dataPacket->getFreshnessPeriod(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool |
| 94 | Entry::wasRefreshedByDuplicate() const |
| 95 | { |
| 96 | return m_wasRefreshedByDuplicate; |
| 97 | } |
| 98 | |
| 99 | const ndn::ConstBufferPtr& |
| 100 | Entry::getDigest() const |
| 101 | { |
| 102 | if (!static_cast<bool>(m_digest)) |
| 103 | { |
| 104 | const Block& block = m_dataPacket->wireEncode(); |
| 105 | m_digest = ndn::crypto::sha256(block.wire(), block.size()); |
| 106 | } |
| 107 | |
| 108 | return m_digest; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | Entry::setIterator(int layer, const Entry::LayerIterators::mapped_type& layerIterator) |
| 113 | { |
| 114 | m_layerIterators[layer] = layerIterator; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Entry::removeIterator(int layer) |
| 119 | { |
| 120 | m_layerIterators.erase(layer); |
| 121 | } |
| 122 | |
| 123 | const Entry::LayerIterators& |
| 124 | Entry::getIterators() const |
| 125 | { |
| 126 | return m_layerIterators; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | Entry::printIterators() const |
| 131 | { |
| 132 | for (LayerIterators::const_iterator it = m_layerIterators.begin(); |
| 133 | it != m_layerIterators.end(); |
| 134 | ++it) |
| 135 | { |
| 136 | NFD_LOG_DEBUG("[" << it->first << "]" << " " << &(*it->second)); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace cs |
| 141 | } // namespace nfd |