blob: c3d057259277e2e84c637af315f2c6797f841848 [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
Yingdi Yu0c3e5912015-03-17 14:22:38 -070022#ifndef NSL_CORE_NODE_HPP
23#define NSL_CORE_NODE_HPP
peizhen guocf4df2d2014-08-12 13:22:32 -070024
Yingdi Yu0c3e5912015-03-17 14:22:38 -070025#include "common.hpp"
26#include "util/non-negative-integer.hpp"
27#include <ndn-cxx/encoding/buffer.hpp>
peizhen guocf4df2d2014-08-12 13:22:32 -070028
29namespace nsl {
30
peizhen guocf4df2d2014-08-12 13:22:32 -070031class Node
32{
33public:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070034 class Error : public std::runtime_error
peizhen guocf4df2d2014-08-12 13:22:32 -070035 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070036 public:
37 explicit
38 Error(const std::string& what)
39 : std::runtime_error(what)
40 {
41 }
42 };
peizhen guocf4df2d2014-08-12 13:22:32 -070043
Yingdi Yu0c3e5912015-03-17 14:22:38 -070044 class Index
peizhen guocf4df2d2014-08-12 13:22:32 -070045 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070046 public:
47 explicit
48 Index(const NonNegativeInteger& seqNo = 0, size_t level = 0);
peizhen guocf4df2d2014-08-12 13:22:32 -070049
Yingdi Yu0c3e5912015-03-17 14:22:38 -070050 /**
51 * @brief compare two indices
52 *
53 * A index is larger than the other if its seqNo is larger than the other,
54 * or their seqNos are equal but its level is lower.
55 */
56 bool
57 operator<(const Index& other) const;
58
59 bool
60 operator==(const Index& other) const;
61
62 bool
63 operator!=(const Index& other) const;
64
65 bool
66 equals(const Index& other) const;
67
68 public:
69 NonNegativeInteger seqNo;
70 size_t level;
71 NonNegativeInteger range;
72 };
73
74public:
75 Node(const NonNegativeInteger& seqNo,
76 size_t level,
77 const NonNegativeInteger& leafSeqNo = 0,
78 ndn::ConstBufferPtr hash = nullptr);
peizhen guocf4df2d2014-08-12 13:22:32 -070079
80 const Index&
Yingdi Yu0c3e5912015-03-17 14:22:38 -070081 getIndex() const
82 {
83 return m_index;
84 }
peizhen guocf4df2d2014-08-12 13:22:32 -070085
86 void
Yingdi Yu0c3e5912015-03-17 14:22:38 -070087 setLeafSeqNo(const NonNegativeInteger& leafSeqNo);
peizhen guocf4df2d2014-08-12 13:22:32 -070088
Yingdi Yu0c3e5912015-03-17 14:22:38 -070089 const NonNegativeInteger&
90 getLeafSeqNo() const
91 {
92 return m_leafSeqNo;
93 }
94
95 void
96 setHash(ndn::ConstBufferPtr hash);
peizhen guocf4df2d2014-08-12 13:22:32 -070097
98 ndn::ConstBufferPtr
Yingdi Yu0c3e5912015-03-17 14:22:38 -070099 getHash() const
100 {
101 return m_hash;
102 }
103
104 bool
105 isFull() const;
106
107 static ndn::ConstBufferPtr
108 getEmptyHash();
109
110protected:
111 Index m_index;
112 NonNegativeInteger m_leafSeqNo;
113 ndn::ConstBufferPtr m_hash;
peizhen guocf4df2d2014-08-12 13:22:32 -0700114
115private:
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700116 static ndn::ConstBufferPtr EMPTY_HASH;
peizhen guocf4df2d2014-08-12 13:22:32 -0700117};
118
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700119typedef shared_ptr<Node> NodePtr;
120typedef shared_ptr<const Node> ConstNodePtr;
121
peizhen guocf4df2d2014-08-12 13:22:32 -0700122} // namespace nsl
123
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700124#endif // NSL_CORE_NODE_HPP