consumer.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_CONSUMER_HPP
21 #define PSYNC_CONSUMER_HPP
22 
23 #include "PSync/common.hpp"
26 
27 #include <ndn-cxx/face.hpp>
28 #include <ndn-cxx/util/random.hpp>
29 #include <ndn-cxx/util/scheduler.hpp>
30 #include <ndn-cxx/util/segment-fetcher.hpp>
31 #include <ndn-cxx/util/time.hpp>
32 
33 #include <map>
34 
35 namespace psync {
36 
37 using namespace ndn::time_literals;
38 
39 using ReceiveHelloCallback = std::function<void(const std::map<ndn::Name, uint64_t>&)>;
40 
41 const ndn::time::milliseconds HELLO_INTEREST_LIFETIME = 1_s;
42 const ndn::time::milliseconds SYNC_INTEREST_LIFETIME = 1_s;
43 
61 class Consumer
62 {
63 public:
76  Consumer(const ndn::Name& syncPrefix,
77  ndn::Face& face,
78  const ReceiveHelloCallback& onReceiveHelloData,
79  const UpdateCallback& onUpdate,
80  unsigned int count,
81  double false_positive,
82  ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME,
83  ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME);
84 
90  void
91  sendHelloInterest();
92 
98  void
99  sendSyncInterest();
100 
113  bool
114  addSubscription(const ndn::Name& prefix, uint64_t seqNo, bool callSyncDataCb = true);
115 
116  std::set<ndn::Name>
118  {
119  return m_subscriptionList;
120  }
121 
122  bool
123  isSubscribed(const ndn::Name& prefix) const
124  {
125  return m_subscriptionList.find(prefix) != m_subscriptionList.end();
126  }
127 
128  ndn::optional<uint64_t>
129  getSeqNo(const ndn::Name& prefix) const
130  {
131  auto it = m_prefixes.find(prefix);
132  if (it == m_prefixes.end()) {
133  return ndn::nullopt;
134  }
135  return it->second;
136  }
137 
141  void
142  stop();
143 
144 private:
157  void
158  onHelloData(const ndn::ConstBufferPtr& bufferPtr);
159 
170  void
171  onSyncData(const ndn::ConstBufferPtr& bufferPtr);
172 
174  ndn::Face& m_face;
175  ndn::Scheduler m_scheduler;
176 
177  ndn::Name m_syncPrefix;
178  ndn::Name m_helloInterestPrefix;
179  ndn::Name m_syncInterestPrefix;
180  ndn::Name m_iblt;
181  ndn::Name m_helloDataName;
182  ndn::Name m_syncDataName;
183  uint32_t m_syncDataContentType;
184 
185  ReceiveHelloCallback m_onReceiveHelloData;
186 
187  // Called when new sync update is received from producer.
188  UpdateCallback m_onUpdate;
189 
190  // Bloom filter is used to store application/user's subscription list.
191  detail::BloomFilter m_bloomFilter;
192 
193  ndn::time::milliseconds m_helloInterestLifetime;
194  ndn::time::milliseconds m_syncInterestLifetime;
195 
196  // Store sequence number for the prefix.
197  std::map<ndn::Name, uint64_t> m_prefixes;
198  std::set<ndn::Name> m_subscriptionList;
199 
200  ndn::random::RandomNumberEngine& m_rng;
201  std::uniform_int_distribution<> m_rangeUniformRandom;
202  std::shared_ptr<ndn::util::SegmentFetcher> m_helloFetcher;
203  std::shared_ptr<ndn::util::SegmentFetcher> m_syncFetcher;
204 };
205 
206 } // namespace psync
207 
208 #endif // PSYNC_CONSUMER_HPP
Consumer logic to subscribe to producer&#39;s data.
Definition: consumer.hpp:61
Definition: common.hpp:33
std::function< void(const std::map< ndn::Name, uint64_t > &)> ReceiveHelloCallback
Definition: consumer.hpp:39
std::set< ndn::Name > getSubscriptionList() const
Definition: consumer.hpp:117
bool isSubscribed(const ndn::Name &prefix) const
Definition: consumer.hpp:123
const ndn::time::milliseconds SYNC_INTEREST_LIFETIME
Definition: consumer.hpp:42
std::function< void(const std::vector< MissingDataInfo > &)> UpdateCallback
Definition: common.hpp:62
#define PSYNC_PUBLIC_WITH_TESTS_ELSE_PRIVATE
ndn::optional< uint64_t > getSeqNo(const ndn::Name &prefix) const
Definition: consumer.hpp:129
const ndn::time::milliseconds HELLO_INTEREST_LIFETIME
Definition: consumer.hpp:41