blob: a9cb487198fa6191901fa6f79a8f37c2fcf91c53 [file] [log] [blame]
peizhen guocf4df2d2014-08-12 13:22:32 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
peizhen guocf4df2d2014-08-12 13:22:32 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
peizhen guocf4df2d2014-08-12 13:22:32 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
peizhen guocf4df2d2014-08-12 13:22:32 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
peizhen guocf4df2d2014-08-12 13:22:32 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
peizhen guocf4df2d2014-08-12 13:22:32 -070020 */
21
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070022#ifndef NDN_DELOREAN_CORE_NODE_HPP
23#define NDN_DELOREAN_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
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700124#endif // NDN_DELOREAN_CORE_NODE_HPP