peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 20 | */ |
| 21 | #include "node.hpp" |
| 22 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/util/digest.hpp> |
| 24 | #include <boost/lexical_cast.hpp> |
| 25 | |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 26 | namespace nsl { |
| 27 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 28 | ndn::ConstBufferPtr Node::EMPTY_HASH; |
| 29 | |
| 30 | Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel) |
| 31 | : seqNo(nodeSeq) |
| 32 | , level(nodeLevel) |
| 33 | , range(1 << nodeLevel) |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 34 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 35 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 41 | bool |
| 42 | Node::Index::operator<(const Index& other) const |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 43 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 44 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 55 | bool |
| 56 | Node::Index::operator==(const Index& other) const |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 57 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 58 | return equals(other); |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 61 | bool |
| 62 | Node::Index::operator!=(const Index& other) const |
| 63 | { |
| 64 | return !equals(other); |
| 65 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 66 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 67 | bool |
| 68 | Node::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 | |
| 78 | Node::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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 90 | |
| 91 | void |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 92 | Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo) |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 93 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 94 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 100 | void |
| 101 | Node::setHash(ndn::ConstBufferPtr hash) |
| 102 | { |
| 103 | m_hash = hash; |
| 104 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 105 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 106 | bool |
| 107 | Node::isFull() const |
| 108 | { |
| 109 | return m_index.seqNo + m_index.range == m_leafSeqNo; |
| 110 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 111 | |
| 112 | ndn::ConstBufferPtr |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 113 | Node::getEmptyHash() |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 114 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 115 | if (EMPTY_HASH == nullptr) { |
| 116 | ndn::util::Sha256 sha256; |
| 117 | EMPTY_HASH = sha256.computeDigest(); |
| 118 | } |
| 119 | |
| 120 | return EMPTY_HASH; |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace nsl |