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-2020, 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 
25 #include "PSync/detail/iblt.hpp"
26 #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 <boost/bimap.hpp>
37 #include <boost/bimap/unordered_set_of.hpp>
38 
39 #include <map>
40 
41 namespace psync {
42 
43 namespace bm = boost::bimaps;
44 using namespace ndn::time_literals;
45 
46 const ndn::time::milliseconds SYNC_REPLY_FRESHNESS = 1_s;
47 
54 {
55  class Error : public std::runtime_error
56  {
57  public:
58  using std::runtime_error::runtime_error;
59  };
60 
73  ProducerBase(size_t expectedNumEntries,
74  ndn::Face& face,
75  const ndn::Name& syncPrefix,
76  const ndn::Name& userPrefix,
77  ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS,
79  CompressionScheme contentCompression = CompressionScheme::ZLIB);
80 public:
86  ndn::optional<uint64_t>
87  getSeqNo(const ndn::Name& prefix) const
88  {
89  auto it = m_prefixes.find(prefix);
90  if (it == m_prefixes.end()) {
91  return ndn::nullopt;
92  }
93  return it->second;
94  }
95 
107  bool
108  addUserNode(const ndn::Name& prefix);
109 
117  void
118  removeUserNode(const ndn::Name& prefix);
119 
132  void
133  updateSeqNo(const ndn::Name& prefix, uint64_t seq);
134 
135  bool
136  isUserNode(const ndn::Name& prefix) const
137  {
138  return m_prefixes.find(prefix) != m_prefixes.end();
139  }
140 
149  void
150  sendApplicationNack(const ndn::Name& name);
151 
158  void
159  onRegisterFailed(const ndn::Name& prefix, const std::string& msg) const;
160 
161  using HashNameBiMap = bm::bimap<bm::unordered_set_of<uint32_t>,
162  bm::unordered_set_of<ndn::Name, std::hash<ndn::Name>>>;
163 
167  // Threshold is used check if the differences are greater
168  // than it and whether we need to update the other side.
169  uint32_t m_threshold;
170 
171  // prefix and sequence number
172  std::map<ndn::Name, uint64_t> m_prefixes;
174 
175  ndn::Face& m_face;
176  ndn::KeyChain m_keyChain;
177  ndn::Scheduler m_scheduler;
178 
179  ndn::Name m_syncPrefix;
180  ndn::Name m_userPrefix;
181 
182  ndn::time::milliseconds m_syncReplyFreshness;
183 
185 
186  ndn::random::RandomNumberEngine& m_rng;
189 };
190 
191 } // namespace psync
192 
193 #endif // PSYNC_PRODUCER_BASE_HPP
HashNameBiMap m_biMap
const ndn::time::milliseconds SYNC_REPLY_FRESHNESS
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:82
bool isUserNode(const ndn::Name &prefix) const
Segment Publisher to publish segmented data.
bm::bimap< bm::unordered_set_of< uint32_t >, bm::unordered_set_of< ndn::Name, std::hash< ndn::Name > >> HashNameBiMap
CompressionScheme m_ibltCompression
ndn::optional< uint64_t > getSeqNo(const ndn::Name &prefix) const
Returns the current sequence number of the given prefix.
CompressionScheme
Definition: util.hpp:51
ndn::KeyChain m_keyChain
CompressionScheme m_contentCompression
#define PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED
ndn::random::RandomNumberEngine & m_rng
ndn::Scheduler m_scheduler
ndn::time::milliseconds m_syncReplyFreshness
SegmentPublisher m_segmentPublisher
Base class for PartialProducer and FullProducer.
std::map< ndn::Name, uint64_t > m_prefixes