blob: 11e1cd08fb425a2372900a3548be1f44a1982fce [file] [log] [blame]
peizhen guocf4df2d2014-08-12 13:22:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California
4 *
5 * This file is part of NSL (NDN Signature Logger).
6 * See AUTHORS.md for complete list of NSL authors and contributors.
7 *
8 * NSL is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
Yingdi Yu0c3e5912015-03-17 14:22:38 -070019 * See AUTHORS.md for complete list of nsl authors and contributors.
peizhen guocf4df2d2014-08-12 13:22:32 -070020 */
21#include "node.hpp"
22
Yingdi Yu0c3e5912015-03-17 14:22:38 -070023#include <ndn-cxx/util/digest.hpp>
24#include <boost/lexical_cast.hpp>
25
peizhen guocf4df2d2014-08-12 13:22:32 -070026namespace nsl {
27
Yingdi Yu0c3e5912015-03-17 14:22:38 -070028ndn::ConstBufferPtr Node::EMPTY_HASH;
29
30Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel)
31 : seqNo(nodeSeq)
32 , level(nodeLevel)
33 , range(1 << nodeLevel)
peizhen guocf4df2d2014-08-12 13:22:32 -070034{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070035 if (seqNo % range != 0)
36 throw Error("Index: index level and seqNo do not match: (" +
37 boost::lexical_cast<std::string>(seqNo) + ", " +
38 boost::lexical_cast<std::string>(level) + ")");
peizhen guocf4df2d2014-08-12 13:22:32 -070039}
40
Yingdi Yu0c3e5912015-03-17 14:22:38 -070041bool
42Node::Index::operator<(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070043{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070044 if (seqNo < other.seqNo) {
45 return true;
46 }
47 else if (seqNo == other.seqNo) {
48 return level < other.level;
49 }
50 else {
51 return false;
52 }
peizhen guocf4df2d2014-08-12 13:22:32 -070053}
54
Yingdi Yu0c3e5912015-03-17 14:22:38 -070055bool
56Node::Index::operator==(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070057{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070058 return equals(other);
peizhen guocf4df2d2014-08-12 13:22:32 -070059}
60
Yingdi Yu0c3e5912015-03-17 14:22:38 -070061bool
62Node::Index::operator!=(const Index& other) const
63{
64 return !equals(other);
65}
peizhen guocf4df2d2014-08-12 13:22:32 -070066
Yingdi Yu0c3e5912015-03-17 14:22:38 -070067bool
68Node::Index::equals(const Index& other) const
69{
70 if (seqNo == other.seqNo && level == other.level) {
71 return true;
72 }
73 else {
74 return false;
75 }
76}
77
78Node::Node(const NonNegativeInteger& nodeSeqNo,
79 size_t nodeLevel,
80 const NonNegativeInteger& leafSeqNo,
81 ndn::ConstBufferPtr hash)
82 : m_index(nodeSeqNo, nodeLevel)
83 , m_hash(hash)
84{
85 if (leafSeqNo == 0 && m_index.seqNo > leafSeqNo)
86 m_leafSeqNo = m_index.seqNo;
87 else
88 setLeafSeqNo(leafSeqNo);
89}
peizhen guocf4df2d2014-08-12 13:22:32 -070090
91void
Yingdi Yu0c3e5912015-03-17 14:22:38 -070092Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo)
peizhen guocf4df2d2014-08-12 13:22:32 -070093{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070094 if (leafSeqNo > m_index.seqNo + m_index.range || leafSeqNo < m_index.seqNo)
95 throw Error("Node: leaf seqNo is out of range");
96
97 m_leafSeqNo = leafSeqNo;
peizhen guocf4df2d2014-08-12 13:22:32 -070098}
99
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700100void
101Node::setHash(ndn::ConstBufferPtr hash)
102{
103 m_hash = hash;
104}
peizhen guocf4df2d2014-08-12 13:22:32 -0700105
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700106bool
107Node::isFull() const
108{
109 return m_index.seqNo + m_index.range == m_leafSeqNo;
110}
peizhen guocf4df2d2014-08-12 13:22:32 -0700111
112ndn::ConstBufferPtr
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700113Node::getEmptyHash()
peizhen guocf4df2d2014-08-12 13:22:32 -0700114{
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700115 if (EMPTY_HASH == nullptr) {
116 ndn::util::Sha256 sha256;
117 EMPTY_HASH = sha256.computeDigest();
118 }
119
120 return EMPTY_HASH;
peizhen guocf4df2d2014-08-12 13:22:32 -0700121}
122
123} // namespace nsl