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 
23 #include "PSync/common.hpp"
25 #include "PSync/detail/iblt.hpp"
27 
28 #include <ndn-cxx/face.hpp>
29 #include <ndn-cxx/security/key-chain.hpp>
30 #include <ndn-cxx/security/validator-config.hpp>
31 #include <ndn-cxx/util/random.hpp>
32 #include <ndn-cxx/util/scheduler.hpp>
33 #include <ndn-cxx/util/time.hpp>
34 
35 #include <boost/bimap/bimap.hpp>
36 #include <boost/bimap/unordered_set_of.hpp>
37 
38 #include <map>
39 
40 namespace psync {
41 
42 namespace bm = boost::bimaps;
43 using namespace ndn::time_literals;
44 
45 const ndn::time::milliseconds SYNC_REPLY_FRESHNESS = 1_s;
46 
53 {
54 public:
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::NONE);
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 
155  void
156  onRegisterFailed(const ndn::Name& prefix, const std::string& msg) const;
157 
161  // Threshold is used check if the differences are greater
162  // than it and whether we need to update the other side.
163  uint32_t m_threshold;
164 
165  // prefix and sequence number
166  std::map<ndn::Name, uint64_t> m_prefixes;
167 
168  using HashNameBiMap = bm::bimap<bm::unordered_set_of<uint32_t>,
169  bm::unordered_set_of<ndn::Name, std::hash<ndn::Name>>>;
171 
172  ndn::Face& m_face;
173  ndn::KeyChain m_keyChain;
174  ndn::Scheduler m_scheduler;
175 
176  ndn::Name m_syncPrefix;
177  ndn::Name m_userPrefix;
178 
179  ndn::time::milliseconds m_syncReplyFreshness;
180 
182 
183  ndn::random::RandomNumberEngine& m_rng;
186 };
187 
188 } // namespace psync
189 
190 #endif // PSYNC_PRODUCER_BASE_HPP
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:81
Definition: common.hpp:33
HashNameBiMap m_biMap
const ndn::time::milliseconds SYNC_REPLY_FRESHNESS
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: common.hpp:35
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