blob: 9e9988635ca27bb2d535c8b2d6922c05758a24c2 [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"
10
11namespace nfd {
12namespace cs {
13
14NFD_LOG_INIT("CsEntry");
15
16Entry::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
27Entry::~Entry()
28{
29}
30
31const Name&
32Entry::getName() const
33{
34 return m_nameWithDigest;
35}
36
37const Data&
38Entry::getData() const
39{
40 return *m_dataPacket;
41}
42
43void
44Entry::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
59void
60Entry::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
75bool
76Entry::isUnsolicited() const
77{
78 return m_isUnsolicited;
79}
80
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070081const time::steady_clock::TimePoint&
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080082Entry::getStaleTime() const
83{
84 return m_staleAt;
85}
86
87void
88Entry::updateStaleTime()
89{
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070090 m_staleAt = time::steady_clock::now() + m_dataPacket->getFreshnessPeriod();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080091}
92
93bool
94Entry::wasRefreshedByDuplicate() const
95{
96 return m_wasRefreshedByDuplicate;
97}
98
99const ndn::ConstBufferPtr&
100Entry::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
111void
112Entry::setIterator(int layer, const Entry::LayerIterators::mapped_type& layerIterator)
113{
114 m_layerIterators[layer] = layerIterator;
115}
116
117void
118Entry::removeIterator(int layer)
119{
120 m_layerIterators.erase(layer);
121}
122
123const Entry::LayerIterators&
124Entry::getIterators() const
125{
126 return m_layerIterators;
127}
128
129void
130Entry::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