blob: ef0db611e49e7ab76ddf64b3267a84ae0b689e0b [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
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070029namespace ndn {
30namespace delorean {
peizhen guocf4df2d2014-08-12 13:22:32 -070031
peizhen guocf4df2d2014-08-12 13:22:32 -070032class Node
33{
34public:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070035 class Error : public std::runtime_error
peizhen guocf4df2d2014-08-12 13:22:32 -070036 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070037 public:
38 explicit
39 Error(const std::string& what)
40 : std::runtime_error(what)
41 {
42 }
43 };
peizhen guocf4df2d2014-08-12 13:22:32 -070044
Yingdi Yu0c3e5912015-03-17 14:22:38 -070045 class Index
peizhen guocf4df2d2014-08-12 13:22:32 -070046 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070047 public:
48 explicit
49 Index(const NonNegativeInteger& seqNo = 0, size_t level = 0);
peizhen guocf4df2d2014-08-12 13:22:32 -070050
Yingdi Yu0c3e5912015-03-17 14:22:38 -070051 /**
52 * @brief compare two indices
53 *
54 * A index is larger than the other if its seqNo is larger than the other,
55 * or their seqNos are equal but its level is lower.
56 */
57 bool
58 operator<(const Index& other) const;
59
60 bool
61 operator==(const Index& other) const;
62
63 bool
64 operator!=(const Index& other) const;
65
66 bool
67 equals(const Index& other) const;
68
69 public:
70 NonNegativeInteger seqNo;
71 size_t level;
72 NonNegativeInteger range;
73 };
74
75public:
76 Node(const NonNegativeInteger& seqNo,
77 size_t level,
78 const NonNegativeInteger& leafSeqNo = 0,
79 ndn::ConstBufferPtr hash = nullptr);
peizhen guocf4df2d2014-08-12 13:22:32 -070080
81 const Index&
Yingdi Yu0c3e5912015-03-17 14:22:38 -070082 getIndex() const
83 {
84 return m_index;
85 }
peizhen guocf4df2d2014-08-12 13:22:32 -070086
87 void
Yingdi Yu0c3e5912015-03-17 14:22:38 -070088 setLeafSeqNo(const NonNegativeInteger& leafSeqNo);
peizhen guocf4df2d2014-08-12 13:22:32 -070089
Yingdi Yu0c3e5912015-03-17 14:22:38 -070090 const NonNegativeInteger&
91 getLeafSeqNo() const
92 {
93 return m_leafSeqNo;
94 }
95
96 void
97 setHash(ndn::ConstBufferPtr hash);
peizhen guocf4df2d2014-08-12 13:22:32 -070098
99 ndn::ConstBufferPtr
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700100 getHash() const
101 {
102 return m_hash;
103 }
104
105 bool
106 isFull() const;
107
108 static ndn::ConstBufferPtr
109 getEmptyHash();
110
111protected:
112 Index m_index;
113 NonNegativeInteger m_leafSeqNo;
114 ndn::ConstBufferPtr m_hash;
peizhen guocf4df2d2014-08-12 13:22:32 -0700115
116private:
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700117 static ndn::ConstBufferPtr EMPTY_HASH;
peizhen guocf4df2d2014-08-12 13:22:32 -0700118};
119
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700120typedef shared_ptr<Node> NodePtr;
121typedef shared_ptr<const Node> ConstNodePtr;
122
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700123} // namespace delorean
124} // namespace ndn
peizhen guocf4df2d2014-08-12 13:22:32 -0700125
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700126#endif // NDN_DELOREAN_CORE_NODE_HPP