blob: 56adaf0aef95a49d6d37fc08e62c42ccbb52483d [file] [log] [blame]
Ashlesh Gawande4c0a7472018-08-08 12:20:33 -05001/* -*- 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
9 * of the GNU General Public License as published by the Free Software Foundation,
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
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
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
22#include <ndn-cxx/util/logger.hpp>
23#include <ndn-cxx/util/random.hpp>
24
25#include <iostream>
26
27NDN_LOG_INIT(examples.FullSyncApp);
28
29using namespace ndn::time_literals;
30
31class Producer
32{
33public:
34 /**
35 * @brief Initialize producer and schedule updates
36 *
37 * Set IBF size as 80 expecting 80 updates to IBF in a sync cycle
38 * Set syncInterestLifetime and syncReplyFreshness to 1.6 seconds
39 * userPrefix is the default user prefix, no updates are published on it in this example
40 */
41 Producer(const ndn::Name& syncPrefix, const std::string& userPrefix,
42 int numDataStreams, int maxNumPublish)
43 : m_scheduler(m_face.getIoService())
44 , m_fullProducer(80, m_face, syncPrefix, userPrefix,
45 std::bind(&Producer::processSyncUpdate, this, _1),
46 1600_ms, 1600_ms)
47 , m_numDataStreams(numDataStreams)
48 , m_maxNumPublish(maxNumPublish)
49 , m_rng(ndn::random::getRandomNumberEngine())
50 , m_rangeUniformRandom(0, 60000)
51 {
52 // Add user prefixes and schedule updates for them in specified interval
53 for (int i = 0; i < m_numDataStreams; i++) {
54 ndn::Name prefix(userPrefix + "-" + ndn::to_string(i));
55
56 m_fullProducer.addUserNode(prefix);
57
58 m_scheduler.scheduleEvent(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
59 [this, prefix] {
60 doUpdate(prefix);
61 });
62 }
63 }
64
65 void
66 run()
67 {
68 m_face.processEvents();
69 }
70
71private:
72 void
73 doUpdate(const ndn::Name& prefix)
74 {
75 m_fullProducer.publishName(prefix);
76
77 uint64_t seqNo = m_fullProducer.getSeqNo(prefix).value();
78 NDN_LOG_INFO("Publish: " << prefix << "/" << seqNo);
79
80 if (seqNo < m_maxNumPublish) {
81 m_scheduler.scheduleEvent(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)),
82 [this, prefix] {
83 doUpdate(prefix);
84 });
85 }
86 }
87
88 void
89 processSyncUpdate(const std::vector<psync::MissingDataInfo>& updates)
90 {
91 for (const auto& update : updates) {
92 for (uint64_t i = update.lowSeq; i <= update.highSeq; i++) {
93 NDN_LOG_INFO("Update " << update.prefix << "/" << i);
94 }
95 }
96 }
97
98private:
99 ndn::Face m_face;
100 ndn::util::Scheduler m_scheduler;
101
102 psync::FullProducer m_fullProducer;
103
104 int m_numDataStreams;
105 uint64_t m_maxNumPublish;
106
107 ndn::random::RandomNumberEngine& m_rng;
108 std::uniform_int_distribution<> m_rangeUniformRandom;
109};
110
111int
112main(int argc, char* argv[])
113{
114 if (argc != 5) {
115 std::cout << "usage: " << argv[0] << " <syncPrefix> <user-prefix> "
116 << "<number-of-user-prefixes> <max-number-of-updates-per-user-prefix>"
117 << std::endl;
118 return 1;
119 }
120
121 try {
122 Producer producer(argv[1], argv[2], std::stoi(argv[3]), std::stoi(argv[4]));
123 producer.run();
124 }
125 catch (const std::exception& e) {
126 NDN_LOG_ERROR(e.what());
127 }
128}