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 | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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> |
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> |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 25 | #include <ndn-cxx/util/scheduler.hpp> |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 26 | |
| 27 | #include <iostream> |
| 28 | |
| 29 | NDN_LOG_INIT(examples.PartialSyncProducerApp); |
| 30 | |
| 31 | class PSyncPartialProducer |
| 32 | { |
| 33 | public: |
| 34 | /** |
| 35 | * @brief Initialize producer and schedule updates |
| 36 | * |
| 37 | * IBF size is set to 40 in m_producer as the expected number of update to IBF in a sync cycle |
| 38 | */ |
| 39 | PSyncPartialProducer(const ndn::Name& syncPrefix, const std::string& userPrefix, |
| 40 | int nDataStreams, int maxNumPublish) |
| 41 | : m_scheduler(m_face.getIoService()) |
| 42 | , m_producer(40, m_face, syncPrefix, userPrefix + "-0") |
| 43 | , m_nDataStreams(nDataStreams) |
| 44 | , m_maxNumPublish(maxNumPublish) |
| 45 | , m_rng(ndn::random::getRandomNumberEngine()) |
| 46 | , m_rangeUniformRandom(0, 60000) |
| 47 | { |
| 48 | // Add user prefixes and schedule updates for them |
| 49 | for (int i = 0; i < m_nDataStreams; i++) { |
Davide Pesavento | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame] | 50 | ndn::Name updateName(userPrefix + "-" + std::to_string(i)); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 51 | |
| 52 | // Add the user prefix to the producer |
| 53 | // Note that this does not add the already added userPrefix-0 in the constructor |
| 54 | m_producer.addUserNode(updateName); |
| 55 | |
| 56 | // Each user prefix is updated at random interval between 0 and 60 second |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 57 | m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 58 | [this, updateName] { doUpdate(updateName); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | run() |
| 64 | { |
| 65 | m_face.processEvents(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | void |
| 70 | doUpdate(const ndn::Name& updateName) |
| 71 | { |
| 72 | // Publish an update to this user prefix |
| 73 | m_producer.publishName(updateName); |
| 74 | |
| 75 | uint64_t seqNo = m_producer.getSeqNo(updateName).value(); |
| 76 | NDN_LOG_INFO("Publish: " << updateName << "/" << seqNo); |
| 77 | |
| 78 | if (seqNo < m_maxNumPublish) { |
| 79 | // Schedule the next update for this user prefix b/w 0 and 60 seconds |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 80 | m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 81 | [this, updateName] { doUpdate(updateName); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | ndn::Face m_face; |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 87 | ndn::Scheduler m_scheduler; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 88 | |
| 89 | psync::PartialProducer m_producer; |
| 90 | |
| 91 | int m_nDataStreams; |
| 92 | uint64_t m_maxNumPublish; |
| 93 | |
| 94 | ndn::random::RandomNumberEngine& m_rng; |
| 95 | std::uniform_int_distribution<int> m_rangeUniformRandom; |
| 96 | }; |
| 97 | |
| 98 | int |
| 99 | main(int argc, char* argv[]) |
| 100 | { |
| 101 | if (argc != 5) { |
| 102 | std::cout << "usage: " << argv[0] << " <sync-prefix> <user-prefix> " |
| 103 | << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>" |
| 104 | << std::endl; |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | PSyncPartialProducer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4])); |
| 110 | producer.run(); |
| 111 | } |
| 112 | catch (const std::exception& e) { |
| 113 | NDN_LOG_ERROR(e.what()); |
| 114 | } |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 115 | } |