From 69bcaef17bc041442f60fba7cb0e11ad2705d40a Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Wed, 27 Dec 2023 02:29:59 +0000 Subject: [PATCH] full-producer: gather constructor args into Options struct refs #5069 Change-Id: I168efffe739a19273b596a7b1cee2f4ade147d75 --- PSync/full-producer.cpp | 33 +++++++++++++++++---------- PSync/full-producer.hpp | 43 +++++++++++++++++++++++++----------- PSync/partial-producer.hpp | 4 ++-- examples/full-sync.cpp | 21 +++++++++++------- tests/test-full-producer.cpp | 19 +++++++++++----- tests/test-full-sync.cpp | 6 +++-- 6 files changed, 83 insertions(+), 43 deletions(-) diff --git a/PSync/full-producer.cpp b/PSync/full-producer.cpp index fa11120..59907ee 100644 --- a/PSync/full-producer.cpp +++ b/PSync/full-producer.cpp @@ -31,6 +31,24 @@ namespace psync { NDN_LOG_INIT(psync.FullProducer); +FullProducer::FullProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + const ndn::Name& syncPrefix, + const Options& opts) + : ProducerBase(face, keyChain, opts.ibfCount, syncPrefix, opts.syncDataFreshness, + opts.ibfCompression, opts.contentCompression) + , m_syncInterestLifetime(opts.syncInterestLifetime) + , m_onUpdate(opts.onUpdate) +{ + m_registeredPrefix = m_face.setInterestFilter(ndn::InterestFilter(m_syncPrefix).allowLoopback(false), + [this] (auto&&... args) { onSyncInterest(std::forward(args)...); }, + [] (auto&&... args) { onRegisterFailed(std::forward(args)...); }); + + // Should we do this after setInterestFilter success call back + // (Currently following ChronoSync's way) + sendSyncInterest(); +} + FullProducer::FullProducer(ndn::Face& face, ndn::KeyChain& keyChain, size_t expectedNumEntries, @@ -41,20 +59,11 @@ FullProducer::FullProducer(ndn::Face& face, ndn::time::milliseconds syncReplyFreshness, CompressionScheme ibltCompression, CompressionScheme contentCompression) - : ProducerBase(face, keyChain, expectedNumEntries, syncPrefix, - syncReplyFreshness, ibltCompression, contentCompression) - , m_syncInterestLifetime(syncInterestLifetime) - , m_onUpdate(std::move(onUpdateCb)) + : FullProducer(face, keyChain, syncPrefix, + Options{std::move(onUpdateCb), static_cast(expectedNumEntries), ibltCompression, + syncInterestLifetime, syncReplyFreshness, contentCompression}) { addUserNode(userPrefix); - - m_registeredPrefix = m_face.setInterestFilter(ndn::InterestFilter(m_syncPrefix).allowLoopback(false), - [this] (auto&&... args) { onSyncInterest(std::forward(args)...); }, - [] (auto&&... args) { onRegisterFailed(std::forward(args)...); }); - - // Should we do this after setInterestFilter success call back - // (Currently following ChronoSync's way) - sendSyncInterest(); } FullProducer::~FullProducer() diff --git a/PSync/full-producer.hpp b/PSync/full-producer.hpp index 46a00e5..4644727 100644 --- a/PSync/full-producer.hpp +++ b/PSync/full-producer.hpp @@ -42,21 +42,38 @@ class FullProducer : public ProducerBase { public: /** - * @brief Constructor - * - * Registers syncPrefix in NFD and sends a sync interest. + * @brief Constructor options. + */ + struct Options + { + /// Callback to be invoked when there is new data. + UpdateCallback onUpdate = [] (const auto&) {}; + /// Expected number of entries in IBF. + uint32_t ibfCount = 80; + /// Compression scheme to use for IBF. + CompressionScheme ibfCompression = CompressionScheme::DEFAULT; + /// Lifetime of sync Interest. + ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME; + /// FreshnessPeriod of sync Data. + ndn::time::milliseconds syncDataFreshness = SYNC_REPLY_FRESHNESS; + /// Compression scheme to use for Data content. + CompressionScheme contentCompression = CompressionScheme::DEFAULT; + }; + + /** + * @brief Constructor. * - * @param face Application's face - * @param keyChain KeyChain instance to use for signing - * @param expectedNumEntries Expected number of entries in IBF - * @param syncPrefix The prefix of the sync group - * @param userPrefix The prefix of the first user in the group - * @param onUpdateCallBack The callback to be invoked when there is new data - * @param syncInterestLifetime Lifetime of the sync interest - * @param syncReplyFreshness FreshnessPeriod of sync data - * @param ibltCompression Compression scheme to use for IBF - * @param contentCompression Compression scheme to use for Data content + * @param face Application face. + * @param keyChain KeyChain instance to use for signing. + * @param syncPrefix The prefix of the sync group. + * @param opts Options. */ + FullProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + const ndn::Name& syncPrefix, + const Options& opts); + + [[deprecated]] FullProducer(ndn::Face& face, ndn::KeyChain& keyChain, size_t expectedNumEntries, diff --git a/PSync/partial-producer.hpp b/PSync/partial-producer.hpp index 78a7818..4980484 100644 --- a/PSync/partial-producer.hpp +++ b/PSync/partial-producer.hpp @@ -45,9 +45,9 @@ class PartialProducer : public ProducerBase uint32_t ibfCount = 40; /// Compression scheme to use for IBF. CompressionScheme ibfCompression = CompressionScheme::NONE; - /// FreshnessPeriod of hello data. + /// FreshnessPeriod of hello Data. ndn::time::milliseconds helloDataFreshness = HELLO_REPLY_FRESHNESS; - /// FreshnessPeriod of sync data. + /// FreshnessPeriod of sync Data. ndn::time::milliseconds syncDataFreshness = SYNC_REPLY_FRESHNESS; }; diff --git a/examples/full-sync.cpp b/examples/full-sync.cpp index 12fd909..6248336 100644 --- a/examples/full-sync.cpp +++ b/examples/full-sync.cpp @@ -35,20 +35,25 @@ class Producer { public: /** - * @brief Initialize producer and schedule updates + * @brief Initialize producer and schedule updates. * - * Set IBF size as 80 expecting 80 updates to IBF in a sync cycle - * Set syncInterestLifetime and syncReplyFreshness to 1.6 seconds - * userPrefix is the default user prefix, no updates are published on it in this example + * Set IBF size as 80 expecting 80 updates to IBF in a sync cycle. + * Set syncInterestLifetime and syncDataFreshness to 1.6 seconds. + * userPrefix is the prefix string of user node prefixes. */ Producer(const ndn::Name& syncPrefix, const std::string& userPrefix, int numDataStreams, int maxNumPublish) - : m_producer(m_face, m_keyChain, 80, syncPrefix, userPrefix, - std::bind(&Producer::processSyncUpdate, this, _1), - 1600_ms, 1600_ms) + : m_producer(m_face, m_keyChain, syncPrefix, [this] { + psync::FullProducer::Options opts; + opts.onUpdate = std::bind(&Producer::processSyncUpdate, this, _1); + opts.ibfCount = 80; + opts.syncInterestLifetime = 1600_ms; + opts.syncDataFreshness = 1600_ms; + return opts; + } ()) , m_maxNumPublish(maxNumPublish) { - // Add user prefixes and schedule updates for them in specified interval + // Add user prefixes and schedule updates for them in specified interval. for (int i = 0; i < numDataStreams; i++) { ndn::Name prefix(userPrefix + "-" + std::to_string(i)); m_producer.addUserNode(prefix); diff --git a/tests/test-full-producer.cpp b/tests/test-full-producer.cpp index ba8260c..a03d799 100644 --- a/tests/test-full-producer.cpp +++ b/tests/test-full-producer.cpp @@ -41,8 +41,10 @@ BOOST_FIXTURE_TEST_SUITE(TestFullProducer, FullProducerFixture) BOOST_AUTO_TEST_CASE(OnInterest) { - Name syncPrefix("/psync"), userNode("/testUser"); - FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr); + Name syncPrefix("/psync"); + FullProducer::Options opts; + opts.ibfCount = 40; + FullProducer node(m_face, m_keyChain, syncPrefix, opts); Name syncInterestName(syncPrefix); syncInterestName.append("malicious-IBF"); @@ -52,8 +54,11 @@ BOOST_AUTO_TEST_CASE(OnInterest) BOOST_AUTO_TEST_CASE(ConstantTimeoutForFirstSegment) { - Name syncPrefix("/psync"), userNode("/testUser"); - FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr, 8_s); + Name syncPrefix("/psync"); + FullProducer::Options opts; + opts.ibfCount = 40; + opts.syncInterestLifetime = 8_s; + FullProducer node(m_face, m_keyChain, syncPrefix, opts); advanceClocks(10_ms); m_face.sentInterests.clear(); @@ -65,8 +70,10 @@ BOOST_AUTO_TEST_CASE(ConstantTimeoutForFirstSegment) BOOST_AUTO_TEST_CASE(OnSyncDataDecodeFailure) { - Name syncPrefix("/psync"), userNode("/testUser"); - FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr); + Name syncPrefix("/psync"); + FullProducer::Options opts; + opts.ibfCount = 40; + FullProducer node(m_face, m_keyChain, syncPrefix, opts); Name syncInterestName(syncPrefix); node.m_iblt.appendToName(syncInterestName); diff --git a/tests/test-full-sync.cpp b/tests/test-full-sync.cpp index 91f95db..874f6f7 100644 --- a/tests/test-full-sync.cpp +++ b/tests/test-full-sync.cpp @@ -44,8 +44,10 @@ class FullSyncFixture : public tests::IoFixture, public tests::KeyChainFixture userPrefixes[id] = "/userPrefix" + std::to_string(id); faces[id] = std::make_unique(m_io, m_keyChain, ndn::DummyClientFace::Options{true, true}); - nodes[id] = std::make_unique(*faces[id], m_keyChain, 40, syncPrefix, userPrefixes[id], - [] (const auto&) {}); + FullProducer::Options opts; + opts.ibfCount = 40; + nodes[id] = std::make_unique(*faces[id], m_keyChain, syncPrefix, opts); + nodes[id]->addUserNode(userPrefixes[id]); } void