Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Ashlesh Gawande | d83af52 | 2023-07-14 14:37:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | d83af52 | 2023-07-14 14:37:18 -0500 | [diff] [blame] | 18 | */ |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 19 | |
| 20 | #include <PSync/full-producer.hpp> |
| 21 | |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 22 | #include <ndn-cxx/face.hpp> |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 23 | #include <ndn-cxx/security/key-chain.hpp> |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/util/logger.hpp> |
| 25 | #include <ndn-cxx/util/random.hpp> |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 26 | #include <ndn-cxx/util/scheduler.hpp> |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 27 | |
| 28 | #include <iostream> |
| 29 | |
| 30 | NDN_LOG_INIT(examples.FullSyncApp); |
| 31 | |
| 32 | using namespace ndn::time_literals; |
| 33 | |
| 34 | class Producer |
| 35 | { |
| 36 | public: |
| 37 | /** |
Junxiao Shi | 69bcaef | 2023-12-27 02:29:59 +0000 | [diff] [blame] | 38 | * @brief Initialize producer and schedule updates. |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 39 | * |
Ashlesh Gawande | d83af52 | 2023-07-14 14:37:18 -0500 | [diff] [blame] | 40 | * Use default IBF size of 6 as we're expecting 6 updates to IBF in a sync cycle. |
Junxiao Shi | 69bcaef | 2023-12-27 02:29:59 +0000 | [diff] [blame] | 41 | * Set syncInterestLifetime and syncDataFreshness to 1.6 seconds. |
| 42 | * userPrefix is the prefix string of user node prefixes. |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 43 | */ |
| 44 | Producer(const ndn::Name& syncPrefix, const std::string& userPrefix, |
| 45 | int numDataStreams, int maxNumPublish) |
Junxiao Shi | 69bcaef | 2023-12-27 02:29:59 +0000 | [diff] [blame] | 46 | : m_producer(m_face, m_keyChain, syncPrefix, [this] { |
| 47 | psync::FullProducer::Options opts; |
| 48 | opts.onUpdate = std::bind(&Producer::processSyncUpdate, this, _1); |
Junxiao Shi | 69bcaef | 2023-12-27 02:29:59 +0000 | [diff] [blame] | 49 | opts.syncInterestLifetime = 1600_ms; |
| 50 | opts.syncDataFreshness = 1600_ms; |
| 51 | return opts; |
| 52 | } ()) |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 53 | , m_maxNumPublish(maxNumPublish) |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 54 | { |
Junxiao Shi | 69bcaef | 2023-12-27 02:29:59 +0000 | [diff] [blame] | 55 | // Add user prefixes and schedule updates for them in specified interval. |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 56 | for (int i = 0; i < numDataStreams; i++) { |
Davide Pesavento | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame] | 57 | ndn::Name prefix(userPrefix + "-" + std::to_string(i)); |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 58 | m_producer.addUserNode(prefix); |
| 59 | m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)), |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 60 | [this, prefix] { doUpdate(prefix); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | run() |
| 66 | { |
| 67 | m_face.processEvents(); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | void |
| 72 | doUpdate(const ndn::Name& prefix) |
| 73 | { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 74 | m_producer.publishName(prefix); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 75 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 76 | uint64_t seqNo = m_producer.getSeqNo(prefix).value(); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 77 | NDN_LOG_INFO("Publish: " << prefix << "/" << seqNo); |
| 78 | |
| 79 | if (seqNo < m_maxNumPublish) { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 80 | m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)), |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 81 | [this, prefix] { doUpdate(prefix); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 82 | } |
| 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 | NDN_LOG_INFO("Update " << update.prefix << "/" << i); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | ndn::Face m_face; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 97 | ndn::KeyChain m_keyChain; |
Davide Pesavento | 60e5043 | 2023-11-11 18:35:05 -0500 | [diff] [blame] | 98 | ndn::Scheduler m_scheduler{m_face.getIoContext()}; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 99 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 100 | psync::FullProducer m_producer; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 101 | uint64_t m_maxNumPublish; |
| 102 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 103 | ndn::random::RandomNumberEngine& m_rng{ndn::random::getRandomNumberEngine()}; |
| 104 | std::uniform_int_distribution<> m_uniformRand{0, 60000}; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | int |
| 108 | main(int argc, char* argv[]) |
| 109 | { |
| 110 | if (argc != 5) { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 111 | std::cerr << "Usage: " << argv[0] << " <sync-prefix> <user-prefix> " |
| 112 | << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>\n"; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | Producer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4])); |
| 118 | producer.run(); |
| 119 | } |
| 120 | catch (const std::exception& e) { |
| 121 | NDN_LOG_ERROR(e.what()); |
| 122 | } |
| 123 | } |