blob: 61513089e52b927997d5d4bf3b2bbcff8da6fab4 [file] [log] [blame]
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi7639daa2023-08-11 16:40:54 +00003 * Copyright (c) 2014-2023, The University of Memphis
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -05004 *
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
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -06009 * of the GNU Lesser General Public License as published by the Free Software Foundation,
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050010 * 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
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060014 * PURPOSE. See the GNU Lesser General Public License for more details.
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050015 *
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060016 * You should have received a copy of the GNU Lesser General Public License along with
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050017 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawandecbdc0122020-07-13 21:13:00 -070018 */
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050019
20#include <PSync/consumer.hpp>
21
Davide Pesavento03426ef2022-09-23 19:49:10 -040022#include <ndn-cxx/face.hpp>
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050023#include <ndn-cxx/util/logger.hpp>
24#include <ndn-cxx/util/random.hpp>
25
26#include <iostream>
27
28NDN_LOG_INIT(examples.PartialSyncConsumerApp);
29
30class PSyncConsumer
31{
32public:
33 /**
34 * @brief Initialize consumer and start hello process
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050035 * @param syncPrefix should be the same as producer
36 * @param nSub number of subscriptions is used for the bloom filter (subscription list) size
37 */
38 PSyncConsumer(const ndn::Name& syncPrefix, int nSub)
39 : m_nSub(nSub)
Junxiao Shi7639daa2023-08-11 16:40:54 +000040 , m_consumer(m_face, syncPrefix, [this] {
41 psync::Consumer::Options opts;
42 opts.onHelloData = std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1);
43 opts.onUpdate = std::bind(&PSyncConsumer::processSyncUpdate, this, _1);
44 opts.bfCount = m_nSub;
45 return opts;
46 } ())
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050047 {
48 // This starts the consumer side by sending a hello interest to the producer
49 // When the producer responds with hello data, afterReceiveHelloData is called
50 m_consumer.sendHelloInterest();
51 }
52
53 void
54 run()
55 {
56 m_face.processEvents();
57 }
58
59private:
60 void
Ashlesh Gawandecbdc0122020-07-13 21:13:00 -070061 afterReceiveHelloData(const std::map<ndn::Name, uint64_t>& availSubs)
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050062 {
Ashlesh Gawandecbdc0122020-07-13 21:13:00 -070063 std::vector<ndn::Name> sensors;
64 sensors.reserve(availSubs.size());
65 for (const auto& it : availSubs) {
66 sensors.insert(sensors.end(), it.first);
67 }
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050068
69 std::shuffle(sensors.begin(), sensors.end(), m_rng);
70
Ashlesh Gawandecbdc0122020-07-13 21:13:00 -070071 // Randomly subscribe to m_nSub prefixes
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050072 for (int i = 0; i < m_nSub; i++) {
Ashlesh Gawandecbdc0122020-07-13 21:13:00 -070073 ndn::Name prefix = sensors[i];
74 NDN_LOG_INFO("Subscribing to: " << prefix);
75 auto it = availSubs.find(prefix);
76 m_consumer.addSubscription(prefix, it->second);
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050077 }
78
79 // After setting the subscription list, send the sync interest
80 // The sync interest contains the subscription list
81 // When new data is received for any subscribed prefix, processSyncUpdate is called
82 m_consumer.sendSyncInterest();
83 }
84
85 void
86 processSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
87 {
88 for (const auto& update : updates) {
89 for (uint64_t i = update.lowSeq; i <= update.highSeq; i++) {
90 // Data can now be fetched using the prefix and sequence
91 NDN_LOG_INFO("Update: " << update.prefix << "/" << i);
92 }
93 }
94 }
95
96private:
97 ndn::Face m_face;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050098
Davide Pesavento03426ef2022-09-23 19:49:10 -040099 int m_nSub;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -0500100 psync::Consumer m_consumer;
Davide Pesavento03426ef2022-09-23 19:49:10 -0400101
102 ndn::random::RandomNumberEngine& m_rng{ndn::random::getRandomNumberEngine()};
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -0500103};
104
105int
106main(int argc, char* argv[])
107{
108 if (argc != 3) {
Davide Pesavento03426ef2022-09-23 19:49:10 -0400109 std::cerr << "Usage: " << argv[0] << " <sync-prefix> <number-of-subscriptions>\n";
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -0500110 return 1;
111 }
112
113 try {
114 PSyncConsumer consumer(argv[1], std::stoi(argv[2]));
115 consumer.run();
116 }
117 catch (const std::exception& e) {
118 NDN_LOG_ERROR(e.what());
119 }
120}