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/partial-producer.hpp> |
| 21 | |
| 22 | #include <ndn-cxx/util/logger.hpp> |
| 23 | #include <ndn-cxx/util/random.hpp> |
| 24 | |
| 25 | #include <iostream> |
| 26 | |
| 27 | NDN_LOG_INIT(examples.PartialSyncProducerApp); |
| 28 | |
| 29 | class PSyncPartialProducer |
| 30 | { |
| 31 | public: |
| 32 | /** |
| 33 | * @brief Initialize producer and schedule updates |
| 34 | * |
| 35 | * IBF size is set to 40 in m_producer as the expected number of update to IBF in a sync cycle |
| 36 | */ |
| 37 | PSyncPartialProducer(const ndn::Name& syncPrefix, const std::string& userPrefix, |
| 38 | int nDataStreams, int maxNumPublish) |
| 39 | : m_scheduler(m_face.getIoService()) |
| 40 | , m_producer(40, m_face, syncPrefix, userPrefix + "-0") |
| 41 | , m_nDataStreams(nDataStreams) |
| 42 | , m_maxNumPublish(maxNumPublish) |
| 43 | , m_rng(ndn::random::getRandomNumberEngine()) |
| 44 | , m_rangeUniformRandom(0, 60000) |
| 45 | { |
| 46 | // Add user prefixes and schedule updates for them |
| 47 | for (int i = 0; i < m_nDataStreams; i++) { |
| 48 | ndn::Name updateName(userPrefix + "-" + ndn::to_string(i)); |
| 49 | |
| 50 | // Add the user prefix to the producer |
| 51 | // Note that this does not add the already added userPrefix-0 in the constructor |
| 52 | m_producer.addUserNode(updateName); |
| 53 | |
| 54 | // Each user prefix is updated at random interval between 0 and 60 second |
| 55 | m_scheduler.scheduleEvent(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 56 | [this, updateName] { |
| 57 | doUpdate(updateName); |
| 58 | }); |
| 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 |
| 80 | m_scheduler.scheduleEvent(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 81 | [this, updateName] { |
| 82 | doUpdate(updateName); |
| 83 | }); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | ndn::Face m_face; |
| 89 | ndn::util::Scheduler m_scheduler; |
| 90 | |
| 91 | psync::PartialProducer m_producer; |
| 92 | |
| 93 | int m_nDataStreams; |
| 94 | uint64_t m_maxNumPublish; |
| 95 | |
| 96 | ndn::random::RandomNumberEngine& m_rng; |
| 97 | std::uniform_int_distribution<int> m_rangeUniformRandom; |
| 98 | }; |
| 99 | |
| 100 | int |
| 101 | main(int argc, char* argv[]) |
| 102 | { |
| 103 | if (argc != 5) { |
| 104 | std::cout << "usage: " << argv[0] << " <sync-prefix> <user-prefix> " |
| 105 | << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>" |
| 106 | << std::endl; |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | try { |
| 111 | PSyncPartialProducer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4])); |
| 112 | producer.run(); |
| 113 | } |
| 114 | catch (const std::exception& e) { |
| 115 | NDN_LOG_ERROR(e.what()); |
| 116 | } |
| 117 | } |