Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame^] | 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 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include <PSync/consumer.hpp> |
| 21 | |
| 22 | #include <ndn-cxx/name.hpp> |
| 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 |
| 35 | * |
| 36 | * 0.001 is the false positive probability of the bloom filter |
| 37 | * |
| 38 | * @param syncPrefix should be the same as producer |
| 39 | * @param nSub number of subscriptions is used for the bloom filter (subscription list) size |
| 40 | */ |
| 41 | PSyncConsumer(const ndn::Name& syncPrefix, int nSub) |
| 42 | : m_nSub(nSub) |
| 43 | , m_consumer(syncPrefix, m_face, |
| 44 | std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1), |
| 45 | std::bind(&PSyncConsumer::processSyncUpdate, this, _1), |
| 46 | m_nSub, 0.001) |
| 47 | , m_rng(ndn::random::getRandomNumberEngine()) |
| 48 | { |
| 49 | // This starts the consumer side by sending a hello interest to the producer |
| 50 | // When the producer responds with hello data, afterReceiveHelloData is called |
| 51 | m_consumer.sendHelloInterest(); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | run() |
| 56 | { |
| 57 | m_face.processEvents(); |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | void |
| 62 | afterReceiveHelloData(const std::vector<ndn::Name>& availSubs) |
| 63 | { |
| 64 | // Randomly subscribe to m_nSub prefixes |
| 65 | std::vector<ndn::Name> sensors = availSubs; |
| 66 | |
| 67 | std::shuffle(sensors.begin(), sensors.end(), m_rng); |
| 68 | |
| 69 | for (int i = 0; i < m_nSub; i++) { |
| 70 | NDN_LOG_INFO("Subscribing to: " << sensors[i]); |
| 71 | m_consumer.addSubscription(sensors[i]); |
| 72 | } |
| 73 | |
| 74 | // After setting the subscription list, send the sync interest |
| 75 | // The sync interest contains the subscription list |
| 76 | // When new data is received for any subscribed prefix, processSyncUpdate is called |
| 77 | m_consumer.sendSyncInterest(); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | processSyncUpdate(const std::vector<psync::MissingDataInfo>& updates) |
| 82 | { |
| 83 | for (const auto& update : updates) { |
| 84 | for (uint64_t i = update.lowSeq; i <= update.highSeq; i++) { |
| 85 | // Data can now be fetched using the prefix and sequence |
| 86 | NDN_LOG_INFO("Update: " << update.prefix << "/" << i); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | ndn::Face m_face; |
| 93 | int m_nSub; |
| 94 | |
| 95 | psync::Consumer m_consumer; |
| 96 | ndn::random::RandomNumberEngine& m_rng; |
| 97 | }; |
| 98 | |
| 99 | int |
| 100 | main(int argc, char* argv[]) |
| 101 | { |
| 102 | if (argc != 3) { |
| 103 | std::cout << "usage: " << argv[0] << " " |
| 104 | << "<sync-prefix> <number-of-subscriptions>" << std::endl; |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | PSyncConsumer consumer(argv[1], std::stoi(argv[2])); |
| 110 | consumer.run(); |
| 111 | } |
| 112 | catch (const std::exception& e) { |
| 113 | NDN_LOG_ERROR(e.what()); |
| 114 | } |
| 115 | } |