blob: 01da1ddb97a81eab2a4c616ce2dd21681645ed96 [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>
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.FullSyncApp);
30
31using namespace ndn::time_literals;
32
33class Producer
34{
35public:
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++) {
Davide Pesaventoc407dee2022-07-21 23:56:05 -040056 ndn::Name prefix(userPrefix + "-" + std::to_string(i));
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050057 m_fullProducer.addUserNode(prefix);
Davide Pesaventod1437842019-03-19 14:05:21 -040058 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
59 [this, prefix] { doUpdate(prefix); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050060 }
61 }
62
63 void
64 run()
65 {
66 m_face.processEvents();
67 }
68
69private:
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 Pesaventod1437842019-03-19 14:05:21 -040079 m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
80 [this, prefix] { doUpdate(prefix); });
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050081 }
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
94private:
95 ndn::Face m_face;
Davide Pesaventod1437842019-03-19 14:05:21 -040096 ndn::Scheduler m_scheduler;
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -050097
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
107int
108main(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}