Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | 7639daa | 2023-08-11 16:40:54 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, The University of Memphis |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 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 |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 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 |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | cbdc012 | 2020-07-13 21:13:00 -0700 | [diff] [blame] | 18 | */ |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 19 | |
| 20 | #include <PSync/consumer.hpp> |
| 21 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 22 | #include <ndn-cxx/face.hpp> |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/util/logger.hpp> |
| 24 | #include <ndn-cxx/util/random.hpp> |
| 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | NDN_LOG_INIT(examples.PartialSyncConsumerApp); |
| 29 | |
| 30 | class PSyncConsumer |
| 31 | { |
| 32 | public: |
| 33 | /** |
| 34 | * @brief Initialize consumer and start hello process |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 35 | * @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 Shi | 7639daa | 2023-08-11 16:40:54 +0000 | [diff] [blame] | 40 | , 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 Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 47 | { |
| 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 | |
| 59 | private: |
| 60 | void |
Ashlesh Gawande | cbdc012 | 2020-07-13 21:13:00 -0700 | [diff] [blame] | 61 | afterReceiveHelloData(const std::map<ndn::Name, uint64_t>& availSubs) |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 62 | { |
Ashlesh Gawande | cbdc012 | 2020-07-13 21:13:00 -0700 | [diff] [blame] | 63 | 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 Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 68 | |
| 69 | std::shuffle(sensors.begin(), sensors.end(), m_rng); |
| 70 | |
Ashlesh Gawande | cbdc012 | 2020-07-13 21:13:00 -0700 | [diff] [blame] | 71 | // Randomly subscribe to m_nSub prefixes |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 72 | for (int i = 0; i < m_nSub; i++) { |
Ashlesh Gawande | cbdc012 | 2020-07-13 21:13:00 -0700 | [diff] [blame] | 73 | 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 Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 77 | } |
| 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 | |
| 96 | private: |
| 97 | ndn::Face m_face; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 98 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 99 | int m_nSub; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 100 | psync::Consumer m_consumer; |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 101 | |
| 102 | ndn::random::RandomNumberEngine& m_rng{ndn::random::getRandomNumberEngine()}; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | int |
| 106 | main(int argc, char* argv[]) |
| 107 | { |
| 108 | if (argc != 3) { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 109 | std::cerr << "Usage: " << argv[0] << " <sync-prefix> <number-of-subscriptions>\n"; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 110 | 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 | } |