blob: a6f4ca1d4b7eee23b493d41b3ab0a25f5531b1ad [file] [log] [blame]
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoc407dee2022-07-21 23:56:05 -04003 * Copyright (c) 2014-2022, The University of Memphis
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -05004 *
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 Gawande0cf4b602019-01-18 15:58:17 -06009 * of the GNU Lesser General Public License as published by the Free Software Foundation,
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050010 * 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 Gawande0cf4b602019-01-18 15:58:17 -060014 * PURPOSE. See the GNU Lesser General Public License for more details.
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050015 *
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060016 * You should have received a copy of the GNU Lesser General Public License along with
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050017 * 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 Pesaventod1437842019-03-19 14:05:21 -040022#include <ndn-cxx/face.hpp>
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050023#include <ndn-cxx/util/logger.hpp>
24#include <ndn-cxx/util/random.hpp>
Davide Pesaventod1437842019-03-19 14:05:21 -040025#include <ndn-cxx/util/scheduler.hpp>
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050026
27#include <iostream>
28
29NDN_LOG_INIT(examples.PartialSyncProducerApp);
30
31class PSyncPartialProducer
32{
33public:
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 Pesaventoc407dee2022-07-21 23:56:05 -040050 ndn::Name updateName(userPrefix + "-" + std::to_string(i));
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050051
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 Pesaventod1437842019-03-19 14:05:21 -040057 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
58 [this, updateName] { doUpdate(updateName); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050059 }
60 }
61
62 void
63 run()
64 {
65 m_face.processEvents();
66 }
67
68private:
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 Pesaventod1437842019-03-19 14:05:21 -040080 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
81 [this, updateName] { doUpdate(updateName); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050082 }
83 }
84
85private:
86 ndn::Face m_face;
Davide Pesaventod1437842019-03-19 14:05:21 -040087 ndn::Scheduler m_scheduler;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050088
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
98int
99main(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 Pesaventod1437842019-03-19 14:05:21 -0400115}