blob: 55a0f53d420b388803d000c208d833c9815e86ae [file] [log] [blame]
peizhen guo410e0e12014-08-12 13:24:14 -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 *
19 * @author Peizhen Guo <patrick.guopz@gmail.com>
20 */
21#ifndef NLS_CORE_SUB_TREE_CACHE_HPP
22#define NLS_CORE_SUB_TREE_CACHE_HPP
23
24#include <ndn-cxx/encoding/buffer.hpp>
25#include <stdint.h>
26#include <string.h>
27
28#include "node.hpp"
29
30namespace nsl {
31
32typedef ndn::function<void(uint8_t, ndn::ConstBufferPtr)> CallBack;
33
34class SubTree
35{
36public:
37 SubTree()
38 {
39 }
40
41 SubTree(Index rootIndex, CallBack callBackFunction)
42 : m_root(rootIndex),
43 m_remainPosition(64),
44 m_callBackUpdate(callBackFunction)
45 {
46 for (int i = 0; i < 127; i++)
47 {
48 m_nodeHashes.push_back(ndn::BufferPtr(new ndn::Buffer));
49 }
50 }
51
52 ~SubTree()
53 {
54 }
55
56 void
57 addNode(ndn::ConstBufferPtr hash_ptr);
58
59 Index
60 getRootIndex();
61
62 uint8_t
63 getRemainPosition();
64
65 Index
66 getParentRootIndex();
67
68 ndn::ConstBufferPtr
69 getHash(Index nodeIndex);
70
71 // change when subtree's root hash below it changes
72 void
73 updateLeafHash(Index subRootIndex, ndn::ConstBufferPtr hash);
74
75
76 // decode is implemented in MerkleTreeCache class
77 std::string
78 encoding();
79
80 void
81 resumeFromString(uint8_t remain, std::vector<ndn::BufferPtr> hashes);
82
83private:
84
85 void
86 updateHash();
87
88
89private:
90
91 //Based on the following sequence number
92 // 0
93 // 1 2
94 // 3 4 5 6
95 // 7 8 9 10 11 12 13 14
96 std::vector<ndn::BufferPtr> m_nodeHashes;
97 Index m_root;
98 uint8_t m_remainPosition;
99 CallBack m_callBackUpdate;
100};
101
102
103} // namespace nsl
104
105#endif // NLS_CORE_SUB_TREE_CACHE_HPP