blob: 0708de4eb78a45d9cea017c885db135f4dcfbd5c [file] [log] [blame]
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Moiseenko76cf77a2014-03-05 14:35:51 -08009 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 * 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 Moiseenko76cf77a2014-03-05 14:35:51 -080025 */
26
27#include "cs-entry.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028#include "core/logger.hpp"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080029
30namespace nfd {
31namespace cs {
32
33NFD_LOG_INIT("CsEntry");
34
35Entry::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
46Entry::~Entry()
47{
48}
49
50const Name&
51Entry::getName() const
52{
53 return m_nameWithDigest;
54}
55
56const Data&
57Entry::getData() const
58{
59 return *m_dataPacket;
60}
61
62void
63Entry::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
78void
79Entry::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
94bool
95Entry::isUnsolicited() const
96{
97 return m_isUnsolicited;
98}
99
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700100const time::steady_clock::TimePoint&
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800101Entry::getStaleTime() const
102{
103 return m_staleAt;
104}
105
106void
107Entry::updateStaleTime()
108{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700109 m_staleAt = time::steady_clock::now() + m_dataPacket->getFreshnessPeriod();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110}
111
112bool
113Entry::wasRefreshedByDuplicate() const
114{
115 return m_wasRefreshedByDuplicate;
116}
117
118const ndn::ConstBufferPtr&
119Entry::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
130void
131Entry::setIterator(int layer, const Entry::LayerIterators::mapped_type& layerIterator)
132{
133 m_layerIterators[layer] = layerIterator;
134}
135
136void
137Entry::removeIterator(int layer)
138{
139 m_layerIterators.erase(layer);
140}
141
142const Entry::LayerIterators&
143Entry::getIterators() const
144{
145 return m_layerIterators;
146}
147
148void
149Entry::printIterators() const
150{
151 for (LayerIterators::const_iterator it = m_layerIterators.begin();
152 it != m_layerIterators.end();
153 ++it)
154 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700155 NFD_LOG_TRACE("[" << it->first << "]" << " " << &(*it->second));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800156 }
157}
158
159} // namespace cs
160} // namespace nfd