digest-tree.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_DIGEST_TREE_HPP
24 #define NDN_DIGEST_TREE_HPP
25 
26 #include <ndn-cpp/common.hpp>
27 #include <string>
28 
29 namespace ndn {
30 
31 class DigestTree {
32 public:
33  DigestTree()
34  : root_("00")
35  {}
36 
37  class Node {
38  public:
45  Node(const std::string& dataPrefix, int sessionNo, int sequenceNo)
46  : dataPrefix_(dataPrefix),
47  sessionNo_(sessionNo),
48  sequenceNo_(sequenceNo)
49  {
50  recomputeDigest();
51  }
52 
53  const std::string&
54  getDataPrefix() const { return dataPrefix_; }
55 
56  int
57  getSessionNo() const { return sessionNo_; }
58 
59  int
60  getSequenceNo() const { return sequenceNo_; }
61 
66  const std::string&
67  getDigest() const { return digest_; }
68 
73  void
74  setSequenceNo(int sequenceNo)
75  {
76  sequenceNo_ = sequenceNo;
77  recomputeDigest();
78  }
79 
83  class Compare {
84  public:
85  bool
86  operator()
87  (const ptr_lib::shared_ptr<const Node>& node1,
88  const ptr_lib::shared_ptr<const Node>& node2) const
89  {
90  int nameComparison = node1->dataPrefix_.compare(node2->dataPrefix_);
91  if (nameComparison != 0)
92  return nameComparison < 0;
93 
94  return node1->sessionNo_ < node2->sessionNo_;
95  }
96  };
97 
98  private:
102  void
103  recomputeDigest();
104 
105  static void
106  int32ToLittleEndian(uint32_t value, uint8_t* result);
107 
108  std::string dataPrefix_;
109  int sessionNo_;
110  int sequenceNo_;
111  std::string digest_;
112  };
113 
124  bool
125  update(const std::string& dataPrefix, int sessionNo, int sequenceNo);
126 
127  int
128  find(const std::string& dataPrefix, int sessionNo) const;
129 
130  size_t
131  size() const { return digestNode_.size(); }
132 
133  const DigestTree::Node&
134  get(size_t i) const { return *digestNode_[i]; }
135 
140  const std::string&
141  getRoot() const { return root_; }
142 
143 private:
148  void
149  recomputeRoot();
150 
151  std::vector<ptr_lib::shared_ptr<DigestTree::Node> > digestNode_;
152  std::string root_;
153  Node::Compare nodeCompare_;
154 };
155 
156 }
157 
158 #endif
159 
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
const std::string & getDigest() const
Get the digest.
Definition: digest-tree.hpp:67
Node(const std::string &dataPrefix, int sessionNo, int sequenceNo)
Create a new DigestTree::Node with the given fields and compute the digest.
Definition: digest-tree.hpp:45
const std::string & getRoot() const
Get the root digest.
Definition: digest-tree.hpp:141
Compare shared_ptrs to Node based on dataPrefix_ and seqno_session_.
Definition: digest-tree.hpp:83
Definition: digest-tree.hpp:31
Definition: digest-tree.hpp:37
bool update(const std::string &dataPrefix, int sessionNo, int sequenceNo)
Update the digest tree and recompute the root digest.
Definition: digest-tree.cpp:73
void setSequenceNo(int sequenceNo)
Set the sequence number and recompute the digest.
Definition: digest-tree.hpp:74