producer-base.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The University of Memphis
4  *
5  * This file is part of PSync.
6  * See AUTHORS.md for complete list of PSync authors and contributors.
7  *
8  * PSync is free software: you can redistribute it and/or modify it under the terms
9  * of the GNU Lesser 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  * PSync 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License along with
17  * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18  **/
19 
20 #ifndef PSYNC_PRODUCER_BASE_HPP
21 #define PSYNC_PRODUCER_BASE_HPP
22 
23 #include "PSync/detail/iblt.hpp"
25 #include "PSync/detail/util.hpp"
28 
29 #include <ndn-cxx/face.hpp>
30 #include <ndn-cxx/util/random.hpp>
31 #include <ndn-cxx/util/scheduler.hpp>
32 #include <ndn-cxx/util/time.hpp>
33 #include <ndn-cxx/security/key-chain.hpp>
34 #include <ndn-cxx/security/validator-config.hpp>
35 
36 #include <map>
37 #include <unordered_set>
38 
39 namespace psync {
40 
41 using namespace ndn::time_literals;
42 
43 const ndn::time::milliseconds SYNC_REPLY_FRESHNESS = 1_s;
44 const ndn::time::milliseconds HELLO_REPLY_FRESHNESS = 1_s;
45 
52 {
53  class Error : public std::runtime_error
54  {
55  public:
56  using std::runtime_error::runtime_error;
57  };
58 
70  ProducerBase(size_t expectedNumEntries,
71  ndn::Face& face,
72  const ndn::Name& syncPrefix,
73  const ndn::Name& userPrefix,
74  ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS,
75  ndn::time::milliseconds helloReplyFreshness = HELLO_REPLY_FRESHNESS);
76 public:
82  ndn::optional<uint64_t>
83  getSeqNo(const ndn::Name& prefix) const
84  {
85  auto it = m_prefixes.find(prefix);
86  if (it == m_prefixes.end()) {
87  return ndn::nullopt;
88  }
89  return it->second;
90  }
91 
103  bool
104  addUserNode(const ndn::Name& prefix);
105 
113  void
114  removeUserNode(const ndn::Name& prefix);
115 
128  void
129  updateSeqNo(const ndn::Name& prefix, uint64_t seq);
130 
131  bool
132  isUserNode(const ndn::Name& prefix) {
133  if (m_prefixes.find(prefix) == m_prefixes.end()) {
134  return false;
135  }
136  return true;
137  }
138 
147  void
148  sendApplicationNack(const ndn::Name& name);
149 
156  void
157  onRegisterFailed(const ndn::Name& prefix, const std::string& msg) const;
158 
162  // Threshold is used check if the differences are greater
163  // than it and whether we need to update the other side.
164  uint32_t m_threshold;
165 
166  // prefix and sequence number
167  std::map <ndn::Name, uint64_t> m_prefixes;
168  // Just for looking up hash faster (instead of calculating it again)
169  // Only used in updateSeqNo, prefix/seqNo is the key
170  std::map <ndn::Name, uint32_t> m_prefix2hash;
171  // Value is prefix (and not prefix/seqNo)
172  std::map <uint32_t, ndn::Name> m_hash2prefix;
173 
174  ndn::Face& m_face;
175  ndn::KeyChain m_keyChain;
176  ndn::Scheduler m_scheduler;
177 
178  ndn::Name m_syncPrefix;
179  ndn::Name m_userPrefix;
180 
181  ndn::time::milliseconds m_syncReplyFreshness;
182  ndn::time::milliseconds m_helloReplyFreshness;
183 
185 
186  ndn::random::RandomNumberEngine& m_rng;
187 };
188 
189 } // namespace psync
190 
191 #endif // PSYNC_PRODUCER_BASE_HPP
const ndn::time::milliseconds HELLO_REPLY_FRESHNESS
const ndn::time::milliseconds SYNC_REPLY_FRESHNESS
ndn::optional< uint64_t > getSeqNo(const ndn::Name &prefix) const
Returns the current sequence number of the given prefix.
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:80
bool isUserNode(const ndn::Name &prefix)
Segment Publisher to publish segmented data.
ndn::KeyChain m_keyChain
std::map< uint32_t, ndn::Name > m_hash2prefix
#define PUBLIC_WITH_TESTS_ELSE_PROTECTED
ndn::random::RandomNumberEngine & m_rng
std::map< ndn::Name, uint32_t > m_prefix2hash
ndn::Scheduler m_scheduler
ndn::time::milliseconds m_syncReplyFreshness
SegmentPublisher m_segmentPublisher
Base class for PartialProducer and FullProducer.
ndn::time::milliseconds m_helloReplyFreshness
std::map< ndn::Name, uint64_t > m_prefixes