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