Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 9 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame^] | 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | * \author Ilya Moiseenko <iliamo@ucla.edu> |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include "cs-entry.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
| 31 | namespace cs { |
| 32 | |
| 33 | NFD_LOG_INIT("CsEntry"); |
| 34 | |
| 35 | Entry::Entry(const Data& data, bool isUnsolicited) |
| 36 | : m_dataPacket(data.shared_from_this()) |
| 37 | , m_isUnsolicited(isUnsolicited) |
| 38 | , m_wasRefreshedByDuplicate(false) |
| 39 | , m_nameWithDigest(data.getName()) |
| 40 | { |
| 41 | updateStaleTime(); |
| 42 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | Entry::~Entry() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | const Name& |
| 51 | Entry::getName() const |
| 52 | { |
| 53 | return m_nameWithDigest; |
| 54 | } |
| 55 | |
| 56 | const Data& |
| 57 | Entry::getData() const |
| 58 | { |
| 59 | return *m_dataPacket; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | Entry::setData(const Data& data) |
| 64 | { |
| 65 | /// \todo This method may not be necessary (if it is real duplicate, |
| 66 | /// there is no reason to recalculate the same digest |
| 67 | |
| 68 | m_dataPacket = data.shared_from_this(); |
| 69 | m_digest.reset(); |
| 70 | m_wasRefreshedByDuplicate = true; |
| 71 | |
| 72 | updateStaleTime(); |
| 73 | |
| 74 | m_nameWithDigest = data.getName(); |
| 75 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Entry::setData(const Data& data, const ndn::ConstBufferPtr& digest) |
| 80 | { |
| 81 | /// \todo This method may not be necessary (if it is real duplicate, |
| 82 | /// there is no reason to recalculate the same digest |
| 83 | |
| 84 | m_dataPacket = data.shared_from_this(); |
| 85 | m_digest = digest; |
| 86 | m_wasRefreshedByDuplicate = true; |
| 87 | |
| 88 | updateStaleTime(); |
| 89 | |
| 90 | m_nameWithDigest = data.getName(); |
| 91 | m_nameWithDigest.append(ndn::name::Component(getDigest())); |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | Entry::isUnsolicited() const |
| 96 | { |
| 97 | return m_isUnsolicited; |
| 98 | } |
| 99 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 100 | const time::steady_clock::TimePoint& |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 101 | Entry::getStaleTime() const |
| 102 | { |
| 103 | return m_staleAt; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | Entry::updateStaleTime() |
| 108 | { |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 109 | m_staleAt = time::steady_clock::now() + m_dataPacket->getFreshnessPeriod(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool |
| 113 | Entry::wasRefreshedByDuplicate() const |
| 114 | { |
| 115 | return m_wasRefreshedByDuplicate; |
| 116 | } |
| 117 | |
| 118 | const ndn::ConstBufferPtr& |
| 119 | Entry::getDigest() const |
| 120 | { |
| 121 | if (!static_cast<bool>(m_digest)) |
| 122 | { |
| 123 | const Block& block = m_dataPacket->wireEncode(); |
| 124 | m_digest = ndn::crypto::sha256(block.wire(), block.size()); |
| 125 | } |
| 126 | |
| 127 | return m_digest; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | Entry::setIterator(int layer, const Entry::LayerIterators::mapped_type& layerIterator) |
| 132 | { |
| 133 | m_layerIterators[layer] = layerIterator; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | Entry::removeIterator(int layer) |
| 138 | { |
| 139 | m_layerIterators.erase(layer); |
| 140 | } |
| 141 | |
| 142 | const Entry::LayerIterators& |
| 143 | Entry::getIterators() const |
| 144 | { |
| 145 | return m_layerIterators; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | Entry::printIterators() const |
| 150 | { |
| 151 | for (LayerIterators::const_iterator it = m_layerIterators.begin(); |
| 152 | it != m_layerIterators.end(); |
| 153 | ++it) |
| 154 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 155 | NFD_LOG_TRACE("[" << it->first << "]" << " " << &(*it->second)); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | } // namespace cs |
| 160 | } // namespace nfd |