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 |
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/full-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.FullSyncApp); |
| 30 | |
| 31 | using namespace ndn::time_literals; |
| 32 | |
| 33 | class Producer |
| 34 | { |
| 35 | public: |
| 36 | /** |
| 37 | * @brief Initialize producer and schedule updates |
| 38 | * |
| 39 | * Set IBF size as 80 expecting 80 updates to IBF in a sync cycle |
| 40 | * Set syncInterestLifetime and syncReplyFreshness to 1.6 seconds |
| 41 | * userPrefix is the default user prefix, no updates are published on it in this example |
| 42 | */ |
| 43 | Producer(const ndn::Name& syncPrefix, const std::string& userPrefix, |
| 44 | int numDataStreams, int maxNumPublish) |
| 45 | : m_scheduler(m_face.getIoService()) |
| 46 | , m_fullProducer(80, m_face, syncPrefix, userPrefix, |
| 47 | std::bind(&Producer::processSyncUpdate, this, _1), |
| 48 | 1600_ms, 1600_ms) |
| 49 | , m_numDataStreams(numDataStreams) |
| 50 | , m_maxNumPublish(maxNumPublish) |
| 51 | , m_rng(ndn::random::getRandomNumberEngine()) |
| 52 | , m_rangeUniformRandom(0, 60000) |
| 53 | { |
| 54 | // Add user prefixes and schedule updates for them in specified interval |
| 55 | for (int i = 0; i < m_numDataStreams; i++) { |
| 56 | ndn::Name prefix(userPrefix + "-" + ndn::to_string(i)); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 57 | m_fullProducer.addUserNode(prefix); |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 58 | m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 59 | [this, prefix] { doUpdate(prefix); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | run() |
| 65 | { |
| 66 | m_face.processEvents(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | void |
| 71 | doUpdate(const ndn::Name& prefix) |
| 72 | { |
| 73 | m_fullProducer.publishName(prefix); |
| 74 | |
| 75 | uint64_t seqNo = m_fullProducer.getSeqNo(prefix).value(); |
| 76 | NDN_LOG_INFO("Publish: " << prefix << "/" << seqNo); |
| 77 | |
| 78 | if (seqNo < m_maxNumPublish) { |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 79 | m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), |
| 80 | [this, prefix] { doUpdate(prefix); }); |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | processSyncUpdate(const std::vector<psync::MissingDataInfo>& updates) |
| 86 | { |
| 87 | for (const auto& update : updates) { |
| 88 | for (uint64_t i = update.lowSeq; i <= update.highSeq; i++) { |
| 89 | NDN_LOG_INFO("Update " << update.prefix << "/" << i); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | ndn::Face m_face; |
Davide Pesavento | d143784 | 2019-03-19 14:05:21 -0400 | [diff] [blame] | 96 | ndn::Scheduler m_scheduler; |
Ashlesh Gawande | 4c0a747 | 2018-08-08 12:20:33 -0500 | [diff] [blame] | 97 | |
| 98 | psync::FullProducer m_fullProducer; |
| 99 | |
| 100 | int m_numDataStreams; |
| 101 | uint64_t m_maxNumPublish; |
| 102 | |
| 103 | ndn::random::RandomNumberEngine& m_rng; |
| 104 | std::uniform_int_distribution<> m_rangeUniformRandom; |
| 105 | }; |
| 106 | |
| 107 | int |
| 108 | main(int argc, char* argv[]) |
| 109 | { |
| 110 | if (argc != 5) { |
| 111 | std::cout << "usage: " << argv[0] << " <syncPrefix> <user-prefix> " |
| 112 | << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>" |
| 113 | << std::endl; |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | try { |
| 118 | Producer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4])); |
| 119 | producer.run(); |
| 120 | } |
| 121 | catch (const std::exception& e) { |
| 122 | NDN_LOG_ERROR(e.what()); |
| 123 | } |
| 124 | } |