Skip to content

Commit

Permalink
partial-producer: gather constructor args into Options struct
Browse files Browse the repository at this point in the history
refs #5069

Change-Id: I9ac66d8c42be4fcfebbd718eeeb50a9009d75c54
  • Loading branch information
yoursunny committed Dec 11, 2023
1 parent 123d44b commit eecd84f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 45 deletions.
3 changes: 2 additions & 1 deletion PSync/consumer.cpp
Expand Up @@ -53,7 +53,8 @@ Consumer::Consumer(const ndn::Name& syncPrefix,
ndn::time::milliseconds helloInterestLifetime,
ndn::time::milliseconds syncInterestLifetime)
: Consumer(face, syncPrefix,
Options{onReceiveHelloData, onUpdate, count, falsePositive, helloInterestLifetime, syncInterestLifetime})
Options{onReceiveHelloData, onUpdate, static_cast<uint32_t>(count), falsePositive,
helloInterestLifetime, syncInterestLifetime})
{
}

Expand Down
2 changes: 1 addition & 1 deletion PSync/consumer.hpp
Expand Up @@ -65,7 +65,7 @@ class Consumer
/// Callback to give sync data back to application.
UpdateCallback onUpdate = [] (const auto&) {};
/// Number of expected elements (subscriptions) in Bloom filter.
unsigned int bfCount = 80;
uint32_t bfCount = 80;
/// Bloom filter false positive probability.
double bfFalsePositive = 0.001;
/// Lifetime of hello Interest.
Expand Down
4 changes: 3 additions & 1 deletion PSync/full-producer.cpp
Expand Up @@ -41,11 +41,13 @@ FullProducer::FullProducer(ndn::Face& face,
ndn::time::milliseconds syncReplyFreshness,
CompressionScheme ibltCompression,
CompressionScheme contentCompression)
: ProducerBase(face, keyChain, expectedNumEntries, syncPrefix, userPrefix,
: ProducerBase(face, keyChain, expectedNumEntries, syncPrefix,
syncReplyFreshness, ibltCompression, contentCompression)
, m_syncInterestLifetime(syncInterestLifetime)
, m_onUpdate(std::move(onUpdateCb))
{
addUserNode(userPrefix);

m_registeredPrefix = m_face.setInterestFilter(ndn::InterestFilter(m_syncPrefix).allowLoopback(false),
[this] (auto&&... args) { onSyncInterest(std::forward<decltype(args)>(args)...); },
[] (auto&&... args) { onRegisterFailed(std::forward<decltype(args)>(args)...); });
Expand Down
29 changes: 20 additions & 9 deletions PSync/partial-producer.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis
* Copyright (c) 2014-2023, The University of Memphis
*
* This file is part of PSync.
* See AUTHORS.md for complete list of PSync authors and contributors.
Expand Down Expand Up @@ -33,15 +33,11 @@ const ndn::name::Component SYNC{"sync"};

PartialProducer::PartialProducer(ndn::Face& face,
ndn::KeyChain& keyChain,
size_t expectedNumEntries,
const ndn::Name& syncPrefix,
const ndn::Name& userPrefix,
ndn::time::milliseconds helloReplyFreshness,
ndn::time::milliseconds syncReplyFreshness,
CompressionScheme ibltCompression)
: ProducerBase(face, keyChain, expectedNumEntries, syncPrefix, userPrefix,
syncReplyFreshness, ibltCompression, CompressionScheme::NONE)
, m_helloReplyFreshness(helloReplyFreshness)
const Options& opts)
: ProducerBase(face, keyChain, opts.ibfCount, syncPrefix, opts.syncDataFreshness,
opts.ibfCompression, CompressionScheme::NONE)
, m_helloReplyFreshness(opts.helloDataFreshness)
{
m_registeredPrefix = m_face.registerPrefix(m_syncPrefix,
[this] (const auto&) {
Expand All @@ -53,6 +49,21 @@ PartialProducer::PartialProducer(ndn::Face& face,
[] (auto&&... args) { onRegisterFailed(std::forward<decltype(args)>(args)...); });
}

PartialProducer::PartialProducer(ndn::Face& face,
ndn::KeyChain& keyChain,
size_t expectedNumEntries,
const ndn::Name& syncPrefix,
const ndn::Name& userPrefix,
ndn::time::milliseconds helloReplyFreshness,
ndn::time::milliseconds syncReplyFreshness,
CompressionScheme ibltCompression)
: PartialProducer(face, keyChain, syncPrefix,
Options{static_cast<uint32_t>(expectedNumEntries), ibltCompression,
helloReplyFreshness, syncReplyFreshness})
{
addUserNode(userPrefix);
}

void
PartialProducer::publishName(const ndn::Name& prefix, std::optional<uint64_t> seq)
{
Expand Down
40 changes: 27 additions & 13 deletions PSync/partial-producer.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis
* Copyright (c) 2014-2023, The University of Memphis
*
* This file is part of PSync.
* See AUTHORS.md for complete list of PSync authors and contributors.
Expand Down Expand Up @@ -37,20 +37,34 @@ class PartialProducer : public ProducerBase
{
public:
/**
* @brief Constructor
*
* Registers syncPrefix in NFD and sets internal filters for
* "sync" and "hello" under syncPrefix.
* @brief Constructor options.
*/
struct Options
{
/// Expected number of entries in IBF.
uint32_t ibfCount = 40;
/// Compression scheme to use for IBF.
CompressionScheme ibfCompression = CompressionScheme::NONE;
/// FreshnessPeriod of hello data.
ndn::time::milliseconds helloDataFreshness = HELLO_REPLY_FRESHNESS;
/// FreshnessPeriod of sync data.
ndn::time::milliseconds syncDataFreshness = SYNC_REPLY_FRESHNESS;
};

/**
* @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 helloReplyFreshness FreshnessPeriod of hello data
* @param syncReplyFreshness FreshnessPeriod of sync data
* @param ibltCompression Compression scheme to use for IBF
* @param face Application face.
* @param keyChain KeyChain instance to use for signing.
* @param syncPrefix The prefix of the sync group.
* @param opts Options.
*/
PartialProducer(ndn::Face& face,
ndn::KeyChain& keyChain,
const ndn::Name& syncPrefix,
const Options& opts);

[[deprecated]]
PartialProducer(ndn::Face& face,
ndn::KeyChain& keyChain,
size_t expectedNumEntries,
Expand Down
3 changes: 0 additions & 3 deletions PSync/producer-base.cpp
Expand Up @@ -31,7 +31,6 @@ ProducerBase::ProducerBase(ndn::Face& face,
ndn::KeyChain& keyChain,
size_t expectedNumEntries,
const ndn::Name& syncPrefix,
const ndn::Name& userPrefix,
ndn::time::milliseconds syncReplyFreshness,
CompressionScheme ibltCompression,
CompressionScheme contentCompression)
Expand All @@ -44,12 +43,10 @@ ProducerBase::ProducerBase(ndn::Face& face,
, m_expectedNumEntries(expectedNumEntries)
, m_threshold(expectedNumEntries / 2)
, m_syncPrefix(syncPrefix)
, m_userPrefix(userPrefix)
, m_syncReplyFreshness(syncReplyFreshness)
, m_ibltCompression(ibltCompression)
, m_contentCompression(contentCompression)
{
addUserNode(userPrefix);
}

bool
Expand Down
5 changes: 1 addition & 4 deletions PSync/producer-base.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, The University of Memphis
* Copyright (c) 2014-2023, The University of Memphis
*
* This file is part of PSync.
* See AUTHORS.md for complete list of PSync authors and contributors.
Expand Down Expand Up @@ -61,7 +61,6 @@ class ProducerBase
* @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 syncReplyFreshness FreshnessPeriod of sync data
* @param ibltCompression Compression scheme to use for IBF
* @param contentCompression Compression scheme to use for Data content
Expand All @@ -70,7 +69,6 @@ class ProducerBase
ndn::KeyChain& keyChain,
size_t expectedNumEntries,
const ndn::Name& syncPrefix,
const ndn::Name& userPrefix,
ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS,
CompressionScheme ibltCompression = CompressionScheme::NONE,
CompressionScheme contentCompression = CompressionScheme::NONE);
Expand Down Expand Up @@ -175,7 +173,6 @@ class ProducerBase
// than it and whether we need to update the other side.
const size_t m_threshold;
const ndn::Name m_syncPrefix;
const ndn::Name m_userPrefix;
const ndn::time::milliseconds m_syncReplyFreshness;
const CompressionScheme m_ibltCompression;
const CompressionScheme m_contentCompression;
Expand Down
3 changes: 1 addition & 2 deletions examples/producer.cpp
Expand Up @@ -39,15 +39,14 @@ class PSyncPartialProducer
*/
PSyncPartialProducer(const ndn::Name& syncPrefix, const std::string& userPrefix,
int numDataStreams, int maxNumPublish)
: m_producer(m_face, m_keyChain, 40, syncPrefix, userPrefix + "-0")
: m_producer(m_face, m_keyChain, syncPrefix, {})
, m_maxNumPublish(maxNumPublish)
{
// Add user prefixes and schedule updates for them
for (int i = 0; i < numDataStreams; i++) {
ndn::Name updateName(userPrefix + "-" + std::to_string(i));

// Add the user prefix to the producer
// Note that this does not add the already added userPrefix-0 in the constructor
m_producer.addUserNode(updateName);

// Each user prefix is updated at a random interval between 0 and 60 seconds
Expand Down
12 changes: 8 additions & 4 deletions tests/test-partial-producer.cpp
Expand Up @@ -41,7 +41,8 @@ BOOST_FIXTURE_TEST_SUITE(TestPartialProducer, PartialProducerFixture)
BOOST_AUTO_TEST_CASE(RegisterPrefix)
{
Name syncPrefix("/psync"), userNode("/testUser");
PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
producer.addUserNode(userNode);

m_face.processEvents(-1_ms);

Expand All @@ -55,7 +56,8 @@ BOOST_AUTO_TEST_CASE(RegisterPrefix)
BOOST_AUTO_TEST_CASE(PublishName)
{
Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
producer.addUserNode(userNode);

BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
producer.publishName(userNode);
Expand All @@ -74,7 +76,8 @@ BOOST_AUTO_TEST_CASE(PublishName)
BOOST_AUTO_TEST_CASE(SameSyncInterest)
{
Name syncPrefix("/psync"), userNode("/testUser");
PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
producer.addUserNode(userNode);

Name syncInterestName(syncPrefix);
syncInterestName.append("sync");
Expand Down Expand Up @@ -110,7 +113,8 @@ BOOST_AUTO_TEST_CASE(SameSyncInterest)
BOOST_AUTO_TEST_CASE(OnSyncInterest)
{
Name syncPrefix("/psync"), userNode("/testUser");
PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
producer.addUserNode(userNode);

// Sync interest with no bloom filter attached
Name syncInterestName(syncPrefix);
Expand Down
9 changes: 4 additions & 5 deletions tests/test-partial-sync.cpp
Expand Up @@ -37,7 +37,7 @@ class PartialSyncFixture : public tests::IoFixture, public tests::KeyChainFixtur
protected:
PartialSyncFixture()
{
producer = std::make_unique<PartialProducer>(face, m_keyChain, 40, syncPrefix, userPrefix);
producer = std::make_unique<PartialProducer>(face, m_keyChain, syncPrefix, PartialProducer::Options{});
addUserNodes("testUser", 10);
}

Expand Down Expand Up @@ -111,8 +111,7 @@ class PartialSyncFixture : public tests::IoFixture, public tests::KeyChainFixtur
void
addUserNodes(const std::string& prefix, int numOfUserNodes)
{
// zeroth is added through constructor
for (int i = 1; i < numOfUserNodes; i++) {
for (int i = 0; i < numOfUserNodes; i++) {
producer->addUserNode(prefix + "-" + std::to_string(i));
}
}
Expand Down Expand Up @@ -291,8 +290,8 @@ BOOST_AUTO_TEST_CASE(ReplicatedProducer)
face.unlink();

ndn::DummyClientFace face2(m_io, m_keyChain, {true, true});
PartialProducer replicatedProducer(face2, m_keyChain, 40, syncPrefix, userPrefix);
for (int i = 1; i < 10; i++) {
PartialProducer replicatedProducer(face2, m_keyChain, syncPrefix, {});
for (int i = 0; i < 10; i++) {
replicatedProducer.addUserNode("testUser-" + std::to_string(i));
}
advanceClocks(ndn::time::milliseconds(10));
Expand Down
6 changes: 4 additions & 2 deletions tests/test-producer-base.cpp
Expand Up @@ -39,7 +39,8 @@ BOOST_FIXTURE_TEST_SUITE(TestProducerBase, ProducerBaseFixture)
BOOST_AUTO_TEST_CASE(Basic)
{
Name userNode("/testUser");
ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), userNode);
ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"));
producerBase.addUserNode(userNode);

// Hash table size should be 40 + 40/2 = 60 (which is perfectly divisible by N_HASH = 3)
BOOST_CHECK_EQUAL(producerBase.m_iblt.getHashTable().size(), 60);
Expand All @@ -66,7 +67,8 @@ BOOST_AUTO_TEST_CASE(Basic)

BOOST_AUTO_TEST_CASE(ApplicationNack)
{
ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), Name("/testUser"));
ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"));
producerBase.addUserNode("/testUser");

BOOST_CHECK_EQUAL(m_face.sentData.size(), 0);
producerBase.sendApplicationNack(Name("test"));
Expand Down

0 comments on commit eecd84f

Please sign in to comment.