blob: 108a05061a6f9d7536492218b387a5374104a25f [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
81const time::Point&
82Entry::getStaleTime() const
83{
84 return m_staleAt;
85}
86
87void
88Entry::updateStaleTime()
89{
90 time::Duration freshness = time::milliseconds(m_dataPacket->getFreshnessPeriod());
91 m_staleAt = time::now() + freshness;
92}
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 {
137 NFD_LOG_DEBUG("[" << it->first << "]" << " " << &(*it->second));
138 }
139}
140
141} // namespace cs
142} // namespace nfd