blob: 3ed98145d7fac5874b9d12add1db216388d9dfa0 [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/full-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.FullSyncApp);
31
32using namespace ndn::time_literals;
33
34class Producer
35{
36public:
37 /**
38 * @brief Initialize producer and schedule updates
39 *
40 * Set IBF size as 80 expecting 80 updates to IBF in a sync cycle
41 * Set syncInterestLifetime and syncReplyFreshness to 1.6 seconds
42 * userPrefix is the default user prefix, no updates are published on it in this example
43 */
44 Producer(const ndn::Name& syncPrefix, const std::string& userPrefix,
45 int numDataStreams, int maxNumPublish)
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040046 : m_fullProducer(m_face, m_keyChain, 80, syncPrefix, userPrefix,
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050047 std::bind(&Producer::processSyncUpdate, this, _1),
48 1600_ms, 1600_ms)
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050049 , m_maxNumPublish(maxNumPublish)
50 , m_rng(ndn::random::getRandomNumberEngine())
51 , m_rangeUniformRandom(0, 60000)
52 {
53 // Add user prefixes and schedule updates for them in specified interval
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040054 for (int i = 0; i < numDataStreams; i++) {
Davide Pesaventoc407dee2022-07-21 23:56:05 -040055 ndn::Name prefix(userPrefix + "-" + std::to_string(i));
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050056 m_fullProducer.addUserNode(prefix);
Davide Pesaventod1437842019-03-19 14:05:21 -040057 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
58 [this, prefix] { doUpdate(prefix); });
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& prefix)
71 {
72 m_fullProducer.publishName(prefix);
73
74 uint64_t seqNo = m_fullProducer.getSeqNo(prefix).value();
75 NDN_LOG_INFO("Publish: " << prefix << "/" << seqNo);
76
77 if (seqNo < m_maxNumPublish) {
Davide Pesaventod1437842019-03-19 14:05:21 -040078 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
79 [this, prefix] { doUpdate(prefix); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050080 }
81 }
82
83 void
84 processSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
85 {
86 for (const auto& update : updates) {
87 for (uint64_t i = update.lowSeq; i <= update.highSeq; i++) {
88 NDN_LOG_INFO("Update " << update.prefix << "/" << i);
89 }
90 }
91 }
92
93private:
94 ndn::Face m_face;
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040095 ndn::KeyChain m_keyChain;
96 ndn::Scheduler m_scheduler{m_face.getIoService()};
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050097
98 psync::FullProducer m_fullProducer;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050099 uint64_t m_maxNumPublish;
100
101 ndn::random::RandomNumberEngine& m_rng;
102 std::uniform_int_distribution<> m_rangeUniformRandom;
103};
104
105int
106main(int argc, char* argv[])
107{
108 if (argc != 5) {
109 std::cout << "usage: " << argv[0] << " <syncPrefix> <user-prefix> "
110 << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>"
111 << std::endl;
112 return 1;
113 }
114
115 try {
116 Producer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4]));
117 producer.run();
118 }
119 catch (const std::exception& e) {
120 NDN_LOG_ERROR(e.what());
121 }
122}