Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 60e5043 | 2023-11-11 18:35:05 -0500 | [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/>. |
| 18 | **/ |
| 19 | |
| 20 | #include <PSync/partial-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.PartialSyncProducerApp); |
| 31 | |
| 32 | class PSyncPartialProducer |
| 33 | { |
| 34 | public: |
| 35 | /** |
| 36 | * @brief Initialize producer and schedule updates |
| 37 | * |
| 38 | * IBF size is set to 40 in m_producer as the expected number of update to IBF in a sync cycle |
| 39 | */ |
| 40 | PSyncPartialProducer(const ndn::Name& syncPrefix, const std::string& userPrefix, |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 41 | int numDataStreams, int maxNumPublish) |
Junxiao Shi | eecd84f | 2023-12-07 02:31:51 +0000 | [diff] [blame] | 42 | : m_producer(m_face, m_keyChain, syncPrefix, {}) |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 43 | , m_maxNumPublish(maxNumPublish) |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 44 | { |
| 45 | // Add user prefixes and schedule updates for them |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 46 | for (int i = 0; i < numDataStreams; i++) { |
Davide Pesavento | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame] | 47 | ndn::Name updateName(userPrefix + "-" + std::to_string(i)); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 48 | |
| 49 | // Add the user prefix to the producer |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 50 | m_producer.addUserNode(updateName); |
| 51 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 52 | // Each user prefix is updated at a random interval between 0 and 60 seconds |
| 53 | m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)), |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 54 | [this, updateName] { doUpdate(updateName); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | run() |
| 60 | { |
| 61 | m_face.processEvents(); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | void |
| 66 | doUpdate(const ndn::Name& updateName) |
| 67 | { |
| 68 | // Publish an update to this user prefix |
| 69 | m_producer.publishName(updateName); |
| 70 | |
| 71 | uint64_t seqNo = m_producer.getSeqNo(updateName).value(); |
| 72 | NDN_LOG_INFO("Publish: " << updateName << "/" << seqNo); |
| 73 | |
| 74 | if (seqNo < m_maxNumPublish) { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 75 | // Schedule the next update for this user prefix between 0 and 60 seconds |
| 76 | m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)), |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 77 | [this, updateName] { doUpdate(updateName); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | ndn::Face m_face; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 83 | ndn::KeyChain m_keyChain; |
Davide Pesavento | 60e5043 | 2023-11-11 18:35:05 -0500 | [diff] [blame] | 84 | ndn::Scheduler m_scheduler{m_face.getIoContext()}; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 85 | |
| 86 | psync::PartialProducer m_producer; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 87 | uint64_t m_maxNumPublish; |
| 88 | |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 89 | ndn::random::RandomNumberEngine& m_rng{ndn::random::getRandomNumberEngine()}; |
| 90 | std::uniform_int_distribution<> m_uniformRand{0, 60000}; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | int |
| 94 | main(int argc, char* argv[]) |
| 95 | { |
| 96 | if (argc != 5) { |
Davide Pesavento | 03426ef | 2022-09-23 19:49:10 -0400 | [diff] [blame] | 97 | std::cerr << "Usage: " << argv[0] << " <sync-prefix> <user-prefix> " |
| 98 | << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>\n"; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | try { |
| 103 | PSyncPartialProducer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4])); |
| 104 | producer.run(); |
| 105 | } |
| 106 | catch (const std::exception& e) { |
| 107 | NDN_LOG_ERROR(e.what()); |
| 108 | } |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 109 | } |