consumer: gather constructor args into Options struct
refs #5069
Change-Id: I4eeacc2045157dd12728dab589538d6994ffcb5a
diff --git a/PSync/consumer.cpp b/PSync/consumer.cpp
index c598e82..82408a9 100644
--- a/PSync/consumer.cpp
+++ b/PSync/consumer.cpp
@@ -27,30 +27,36 @@
NDN_LOG_INIT(psync.Consumer);
-Consumer::Consumer(const ndn::Name& syncPrefix,
- ndn::Face& face,
- const ReceiveHelloCallback& onReceiveHelloData,
- const UpdateCallback& onUpdate,
- unsigned int count,
- double false_positive = 0.001,
- ndn::time::milliseconds helloInterestLifetime,
- ndn::time::milliseconds syncInterestLifetime)
+Consumer::Consumer(ndn::Face& face, const ndn::Name& syncPrefix, const Options& opts)
: m_face(face)
, m_scheduler(m_face.getIoContext())
, m_syncPrefix(syncPrefix)
, m_helloInterestPrefix(ndn::Name(m_syncPrefix).append("hello"))
, m_syncInterestPrefix(ndn::Name(m_syncPrefix).append("sync"))
, m_syncDataContentType(ndn::tlv::ContentType_Blob)
- , m_onReceiveHelloData(onReceiveHelloData)
- , m_onUpdate(onUpdate)
- , m_bloomFilter(count, false_positive)
- , m_helloInterestLifetime(helloInterestLifetime)
- , m_syncInterestLifetime(syncInterestLifetime)
+ , m_onReceiveHelloData(opts.onHelloData)
+ , m_onUpdate(opts.onUpdate)
+ , m_bloomFilter(opts.bfCount, opts.bfFalsePositive)
+ , m_helloInterestLifetime(opts.helloInterestLifetime)
+ , m_syncInterestLifetime(opts.syncInterestLifetime)
, m_rng(ndn::random::getRandomNumberEngine())
, m_rangeUniformRandom(100, 500)
{
}
+Consumer::Consumer(const ndn::Name& syncPrefix,
+ ndn::Face& face,
+ const ReceiveHelloCallback& onReceiveHelloData,
+ const UpdateCallback& onUpdate,
+ unsigned int count,
+ double falsePositive,
+ ndn::time::milliseconds helloInterestLifetime,
+ ndn::time::milliseconds syncInterestLifetime)
+ : Consumer(face, syncPrefix,
+ Options{onReceiveHelloData, onUpdate, count, falsePositive, helloInterestLifetime, syncInterestLifetime})
+{
+}
+
bool
Consumer::addSubscription(const ndn::Name& prefix, uint64_t seqNo, bool callSyncDataCb)
{
diff --git a/PSync/consumer.hpp b/PSync/consumer.hpp
index 28e2709..ed5174b 100644
--- a/PSync/consumer.hpp
+++ b/PSync/consumer.hpp
@@ -56,23 +56,40 @@
{
public:
/**
- * @brief constructor
- *
- * @param syncPrefix syncPrefix to send hello/sync interests to producer
- * @param face application's face
- * @param onReceiveHelloData call back to give hello data back to application
- * @param onUpdate call back to give sync data back to application
- * @param count bloom filter number of expected elements (subscriptions) in bloom filter
- * @param false_positive bloom filter false positive probability
- * @param helloInterestLifetime lifetime of hello interest
- * @param syncInterestLifetime lifetime of sync interest
+ * @brief Constructor options.
*/
+ struct Options
+ {
+ /// Callback to give hello data back to application.
+ ReceiveHelloCallback onHelloData = [] (const auto&) {};
+ /// Callback to give sync data back to application.
+ UpdateCallback onUpdate = [] (const auto&) {};
+ /// Number of expected elements (subscriptions) in Bloom filter.
+ unsigned int bfCount = 80;
+ /// Bloom filter false positive probability.
+ double bfFalsePositive = 0.001;
+ /// Lifetime of hello Interest.
+ ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME;
+ /// Lifetime of sync Interest.
+ ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME;
+ };
+
+ /**
+ * @brief Constructor.
+ *
+ * @param face Application face.
+ * @param syncPrefix Prefix to send hello and sync Interests to producer.
+ * @param opts Options.
+ */
+ Consumer(ndn::Face& face, const ndn::Name& syncPrefix, const Options& opts);
+
+ [[deprecated]]
Consumer(const ndn::Name& syncPrefix,
ndn::Face& face,
const ReceiveHelloCallback& onReceiveHelloData,
const UpdateCallback& onUpdate,
unsigned int count,
- double false_positive,
+ double falsePositive = 0.001,
ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME,
ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME);
diff --git a/examples/consumer.cpp b/examples/consumer.cpp
index f787c97..6151308 100644
--- a/examples/consumer.cpp
+++ b/examples/consumer.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.
@@ -32,18 +32,18 @@
public:
/**
* @brief Initialize consumer and start hello process
- *
- * 0.001 is the false positive probability of the bloom filter
- *
* @param syncPrefix should be the same as producer
* @param nSub number of subscriptions is used for the bloom filter (subscription list) size
*/
PSyncConsumer(const ndn::Name& syncPrefix, int nSub)
: m_nSub(nSub)
- , m_consumer(syncPrefix, m_face,
- std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1),
- std::bind(&PSyncConsumer::processSyncUpdate, this, _1),
- m_nSub, 0.001)
+ , m_consumer(m_face, syncPrefix, [this] {
+ psync::Consumer::Options opts;
+ opts.onHelloData = std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1);
+ opts.onUpdate = std::bind(&PSyncConsumer::processSyncUpdate, this, _1);
+ opts.bfCount = m_nSub;
+ return opts;
+ } ())
{
// This starts the consumer side by sending a hello interest to the producer
// When the producer responds with hello data, afterReceiveHelloData is called
diff --git a/tests/test-consumer.cpp b/tests/test-consumer.cpp
index 5f60a7f..d3b8181 100644
--- a/tests/test-consumer.cpp
+++ b/tests/test-consumer.cpp
@@ -33,10 +33,9 @@
BOOST_AUTO_TEST_CASE(AddSubscription)
{
ndn::DummyClientFace face;
- Consumer consumer(Name("/psync"), face,
- [] (const auto&) {},
- [] (const auto&) {},
- 40, 0.001);
+ Consumer::Options opts;
+ opts.bfCount = 40;
+ Consumer consumer(face, "/psync", opts);
Name subscription("test");
@@ -48,10 +47,9 @@
BOOST_AUTO_TEST_CASE(RemoveSubscription)
{
ndn::DummyClientFace face;
- Consumer consumer(Name("/psync"), face,
- [] (const auto&) {},
- [] (const auto&) {},
- 40, 0.001);
+ Consumer::Options opts;
+ opts.bfCount = 40;
+ Consumer consumer(face, "/psync", opts);
Name subscription("test");
consumer.addSubscription(subscription, 0);
@@ -65,10 +63,11 @@
BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, tests::IoFixture)
{
ndn::DummyClientFace face(m_io);
- Consumer consumer(Name("/psync"), face,
- [] (const auto&) {},
- [] (const auto&) {},
- 40, 0.001, 4_s, 4_s);
+ Consumer::Options opts;
+ opts.bfCount = 40;
+ opts.helloInterestLifetime = 4_s;
+ opts.syncInterestLifetime = 4_s;
+ Consumer consumer(face, "/psync", opts);
consumer.sendHelloInterest();
advanceClocks(4_s);