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-2022, 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/util/random.hpp>
31 #include <ndn-cxx/util/scheduler.hpp>
32 
33 #include <boost/bimap/bimap.hpp>
34 #include <boost/bimap/unordered_set_of.hpp>
35 
36 #include <map>
37 
38 namespace psync {
39 
40 namespace bm = boost::bimaps;
41 
48 {
49 public:
50  class Error : public std::runtime_error
51  {
52  public:
53  using std::runtime_error::runtime_error;
54  };
55 
69  ProducerBase(ndn::Face& face,
70  ndn::KeyChain& keyChain,
71  size_t expectedNumEntries,
72  const ndn::Name& syncPrefix,
73  const ndn::Name& userPrefix,
74  ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS,
76  CompressionScheme contentCompression = CompressionScheme::NONE);
77 
78 public:
84  std::optional<uint64_t>
85  getSeqNo(const ndn::Name& prefix) const
86  {
87  auto it = m_prefixes.find(prefix);
88  if (it == m_prefixes.end()) {
89  return std::nullopt;
90  }
91  return it->second;
92  }
93 
105  bool
106  addUserNode(const ndn::Name& prefix);
107 
115  void
116  removeUserNode(const ndn::Name& prefix);
117 
130  void
131  updateSeqNo(const ndn::Name& prefix, uint64_t seq);
132 
133  bool
134  isUserNode(const ndn::Name& prefix) const
135  {
136  return m_prefixes.find(prefix) != m_prefixes.end();
137  }
138 
147  void
148  sendApplicationNack(const ndn::Name& name);
149 
153  [[noreturn]] static void
154  onRegisterFailed(const ndn::Name& prefix, const std::string& msg);
155 
157  ndn::Face& m_face;
158  ndn::KeyChain& m_keyChain;
159  ndn::Scheduler m_scheduler;
160  ndn::random::RandomNumberEngine& m_rng;
161 
163 
164  // prefix and sequence number
165  std::map<ndn::Name, uint64_t> m_prefixes;
166 
167  using HashNameBiMap = bm::bimap<bm::unordered_set_of<uint32_t>,
168  bm::unordered_set_of<ndn::Name, std::hash<ndn::Name>>>;
170 
172 
173  const size_t m_expectedNumEntries;
174  // Threshold is used check if the differences are greater
175  // than it and whether we need to update the other side.
176  const size_t m_threshold;
177  const ndn::Name m_syncPrefix;
178  const ndn::Name m_userPrefix;
179  const ndn::time::milliseconds m_syncReplyFreshness;
182 };
183 
184 } // namespace psync
185 
186 #endif // PSYNC_PRODUCER_BASE_HPP
#define PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Base class for PartialProducer and FullProducer.
const CompressionScheme m_contentCompression
void sendApplicationNack(const ndn::Name &name)
Sends a data packet with content type nack.
const ndn::Name m_syncPrefix
std::optional< uint64_t > getSeqNo(const ndn::Name &prefix) const
Returns the current sequence number of the given prefix.
const ndn::time::milliseconds m_syncReplyFreshness
void removeUserNode(const ndn::Name &prefix)
Remove the user node from synchronization.
const size_t m_expectedNumEntries
bool addUserNode(const ndn::Name &prefix)
Adds a user node for synchronization.
std::map< ndn::Name, uint64_t > m_prefixes
const size_t m_threshold
ProducerBase(ndn::Face &face, ndn::KeyChain &keyChain, size_t expectedNumEntries, const ndn::Name &syncPrefix, const ndn::Name &userPrefix, ndn::time::milliseconds syncReplyFreshness=SYNC_REPLY_FRESHNESS, CompressionScheme ibltCompression=CompressionScheme::NONE, CompressionScheme contentCompression=CompressionScheme::NONE)
Constructor.
HashNameBiMap m_biMap
const ndn::Name m_userPrefix
ndn::Scheduler m_scheduler
static void onRegisterFailed(const ndn::Name &prefix, const std::string &msg)
Logs a message and throws if setting an interest filter fails.
bool isUserNode(const ndn::Name &prefix) const
const CompressionScheme m_ibltCompression
void updateSeqNo(const ndn::Name &prefix, uint64_t seq)
Update m_prefixes and IBF with the given prefix and seq.
bm::bimap< bm::unordered_set_of< uint32_t >, bm::unordered_set_of< ndn::Name, std::hash< ndn::Name > >> HashNameBiMap
SegmentPublisher m_segmentPublisher
ndn::KeyChain & m_keyChain
ndn::random::RandomNumberEngine & m_rng
Helper class to publish segmented data.
Invertible Bloom Lookup Table (Invertible Bloom Filter)
Definition: iblt.hpp:81
Definition: common.hpp:34
CompressionScheme
Definition: common.hpp:43
constexpr ndn::time::milliseconds SYNC_REPLY_FRESHNESS
Definition: common.hpp:41