blob: 05ca84206b1060ab47ea91c5cbc746ce00e6596b [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>
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040023#include <ndn-cxx/security/key-chain.hpp>
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050024#include <ndn-cxx/util/logger.hpp>
25#include <ndn-cxx/util/random.hpp>
Davide Pesaventod1437842019-03-19 14:05:21 -040026#include <ndn-cxx/util/scheduler.hpp>
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050027
28#include <iostream>
29
30NDN_LOG_INIT(examples.PartialSyncProducerApp);
31
32class PSyncPartialProducer
33{
34public:
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 Pesaventoc45a4ea2022-09-19 02:10:53 -040041 int numDataStreams, int maxNumPublish)
42 : m_producer(m_face, m_keyChain, 40, syncPrefix, userPrefix + "-0")
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050043 , m_maxNumPublish(maxNumPublish)
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050044 {
45 // Add user prefixes and schedule updates for them
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040046 for (int i = 0; i < numDataStreams; i++) {
Davide Pesaventoc407dee2022-07-21 23:56:05 -040047 ndn::Name updateName(userPrefix + "-" + std::to_string(i));
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050048
49 // Add the user prefix to the producer
50 // Note that this does not add the already added userPrefix-0 in the constructor
51 m_producer.addUserNode(updateName);
52
Davide Pesavento03426ef2022-09-23 19:49:10 -040053 // Each user prefix is updated at a random interval between 0 and 60 seconds
54 m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)),
Davide Pesaventod1437842019-03-19 14:05:21 -040055 [this, updateName] { doUpdate(updateName); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050056 }
57 }
58
59 void
60 run()
61 {
62 m_face.processEvents();
63 }
64
65private:
66 void
67 doUpdate(const ndn::Name& updateName)
68 {
69 // Publish an update to this user prefix
70 m_producer.publishName(updateName);
71
72 uint64_t seqNo = m_producer.getSeqNo(updateName).value();
73 NDN_LOG_INFO("Publish: " << updateName << "/" << seqNo);
74
75 if (seqNo < m_maxNumPublish) {
Davide Pesavento03426ef2022-09-23 19:49:10 -040076 // Schedule the next update for this user prefix between 0 and 60 seconds
77 m_scheduler.schedule(ndn::time::milliseconds(m_uniformRand(m_rng)),
Davide Pesaventod1437842019-03-19 14:05:21 -040078 [this, updateName] { doUpdate(updateName); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050079 }
80 }
81
82private:
83 ndn::Face m_face;
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040084 ndn::KeyChain m_keyChain;
85 ndn::Scheduler m_scheduler{m_face.getIoService()};
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050086
87 psync::PartialProducer m_producer;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050088 uint64_t m_maxNumPublish;
89
Davide Pesavento03426ef2022-09-23 19:49:10 -040090 ndn::random::RandomNumberEngine& m_rng{ndn::random::getRandomNumberEngine()};
91 std::uniform_int_distribution<> m_uniformRand{0, 60000};
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050092};
93
94int
95main(int argc, char* argv[])
96{
97 if (argc != 5) {
Davide Pesavento03426ef2022-09-23 19:49:10 -040098 std::cerr << "Usage: " << argv[0] << " <sync-prefix> <user-prefix> "
99 << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>\n";
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -0500100 return 1;
101 }
102
103 try {
104 PSyncPartialProducer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4]));
105 producer.run();
106 }
107 catch (const std::exception& e) {
108 NDN_LOG_ERROR(e.what());
109 }
Davide Pesaventod1437842019-03-19 14:05:21 -0400110}