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-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_CONSUMER_HPP
21 #define PSYNC_CONSUMER_HPP
22 
24 #include "PSync/detail/util.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 #include <vector>
35 
36 namespace psync {
37 
38 using namespace ndn::literals::time_literals;
39 
40 typedef std::function<void(const std::vector<ndn::Name>&)> ReceiveHelloCallback;
41 typedef std::function<void(const std::vector<MissingDataInfo>&)> UpdateCallback;
42 
43 const ndn::time::milliseconds HELLO_INTEREST_LIFETIME = 1_s;
44 const ndn::time::milliseconds SYNC_INTEREST_LIFETIME = 1_s;
45 
63 class Consumer
64 {
65 public:
78  Consumer(const ndn::Name& syncPrefix,
79  ndn::Face& face,
80  const ReceiveHelloCallback& onReceiveHelloData,
81  const UpdateCallback& onUpdate,
82  unsigned int count,
83  double false_positive,
84  ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME,
85  ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME);
86 
92  void
93  sendHelloInterest();
94 
100  void
101  sendSyncInterest();
102 
109  bool
110  addSubscription(const ndn::Name& prefix);
111 
112  std::set<ndn::Name>
114  {
115  return m_subscriptionList;
116  }
117 
118  bool
119  isSubscribed(const ndn::Name& prefix) const
120  {
121  return m_subscriptionList.find(prefix) != m_subscriptionList.end();
122  }
123 
124  ndn::optional<uint64_t>
125  getSeqNo(const ndn::Name& prefix) const
126  {
127  auto it = m_prefixes.find(prefix);
128  if (it == m_prefixes.end()) {
129  return ndn::nullopt;
130  }
131  return it->second;
132  }
133 
137  void
138  stop();
139 
140 private:
153  void
154  onHelloData(const ndn::ConstBufferPtr& bufferPtr);
155 
166  void
167  onSyncData(const ndn::ConstBufferPtr& bufferPtr);
168 
170  ndn::Face& m_face;
171  ndn::Scheduler m_scheduler;
172 
173  ndn::Name m_syncPrefix;
174  ndn::Name m_helloInterestPrefix;
175  ndn::Name m_syncInterestPrefix;
176  ndn::Name m_iblt;
177  ndn::Name m_helloDataName;
178  ndn::Name m_syncDataName;
179  uint32_t m_syncDataContentType;
180 
181  ReceiveHelloCallback m_onReceiveHelloData;
182 
183  // Called when new sync update is received from producer.
184  UpdateCallback m_onUpdate;
185 
186  // Bloom filter is used to store application/user's subscription list.
187  BloomFilter m_bloomFilter;
188 
189  ndn::time::milliseconds m_helloInterestLifetime;
190  ndn::time::milliseconds m_syncInterestLifetime;
191 
192  // Store sequence number for the prefix.
193  std::map<ndn::Name, uint64_t> m_prefixes;
194  std::set<ndn::Name> m_subscriptionList;
195 
196  ndn::random::RandomNumberEngine& m_rng;
197  std::uniform_int_distribution<> m_rangeUniformRandom;
198  std::shared_ptr<ndn::util::SegmentFetcher> m_helloFetcher;
199  std::shared_ptr<ndn::util::SegmentFetcher> m_syncFetcher;
200 };
201 
202 } // namespace psync
203 
204 #endif // PSYNC_CONSUMER_HPP
Consumer logic to subscribe to producer&#39;s data.
Definition: consumer.hpp:63
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE
ndn::optional< uint64_t > getSeqNo(const ndn::Name &prefix) const
Definition: consumer.hpp:125
const ndn::time::milliseconds SYNC_INTEREST_LIFETIME
Definition: consumer.hpp:44
std::set< ndn::Name > getSubscriptionList() const
Definition: consumer.hpp:113
const ndn::time::milliseconds HELLO_INTEREST_LIFETIME
Definition: consumer.hpp:43
std::function< void(const std::vector< MissingDataInfo > &)> UpdateCallback
Definition: consumer.hpp:41
bool isSubscribed(const ndn::Name &prefix) const
Definition: consumer.hpp:119
std::function< void(const std::vector< ndn::Name > &)> ReceiveHelloCallback
Definition: consumer.hpp:40