blob: 044fd45a5d1ed19cc8b77b5ac542fefc171c2593 [file] [log] [blame]
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08001/* -*- 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 DiBenedettobf6a93d2014-03-21 14:03:02 -060010#include "core/logger.hpp"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080011
12namespace nfd {
13namespace cs {
14
15NFD_LOG_INIT("CsEntry");
16
17Entry::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
28Entry::~Entry()
29{
30}
31
32const Name&
33Entry::getName() const
34{
35 return m_nameWithDigest;
36}
37
38const Data&
39Entry::getData() const
40{
41 return *m_dataPacket;
42}
43
44void
45Entry::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
60void
61Entry::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
76bool
77Entry::isUnsolicited() const
78{
79 return m_isUnsolicited;
80}
81
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070082const time::steady_clock::TimePoint&
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080083Entry::getStaleTime() const
84{
85 return m_staleAt;
86}
87
88void
89Entry::updateStaleTime()
90{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070091 m_staleAt = time::steady_clock::now() + m_dataPacket->getFreshnessPeriod();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080092}
93
94bool
95Entry::wasRefreshedByDuplicate() const
96{
97 return m_wasRefreshedByDuplicate;
98}
99
100const ndn::ConstBufferPtr&
101Entry::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
112void
113Entry::setIterator(int layer, const Entry::LayerIterators::mapped_type& layerIterator)
114{
115 m_layerIterators[layer] = layerIterator;
116}
117
118void
119Entry::removeIterator(int layer)
120{
121 m_layerIterators.erase(layer);
122}
123
124const Entry::LayerIterators&
125Entry::getIterators() const
126{
127 return m_layerIterators;
128}
129
130void
131Entry::printIterators() const
132{
133 for (LayerIterators::const_iterator it = m_layerIterators.begin();
134 it != m_layerIterators.end();
135 ++it)
136 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700137 NFD_LOG_TRACE("[" << it->first << "]" << " " << &(*it->second));
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800138 }
139}
140
141} // namespace cs
142} // namespace nfd