consumer: gather constructor args into Options struct

refs #5069

Change-Id: I4eeacc2045157dd12728dab589538d6994ffcb5a
This commit is contained in:
Junxiao Shi committed 2023-11-26 02:16:21 +00:00
1 parent 60e5043d0c
commit 7639daa564
4 files changed
+66 -44

No files matched your search

+19 -13
View File
@@ -27,30 +27,36 @@ namespace psync {
NDN_LOG_INIT(psync.Consumer); NDN_LOG_INIT(psync.Consumer);
Consumer::Consumer(const ndn::Name& syncPrefix, Consumer::Consumer(ndn::Face& face, const ndn::Name& syncPrefix, const Options& opts)
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)
: m_face(face) : m_face(face)
, m_scheduler(m_face.getIoContext()) , m_scheduler(m_face.getIoContext())
, m_syncPrefix(syncPrefix) , m_syncPrefix(syncPrefix)
, m_helloInterestPrefix(ndn::Name(m_syncPrefix).append("hello")) , m_helloInterestPrefix(ndn::Name(m_syncPrefix).append("hello"))
, m_syncInterestPrefix(ndn::Name(m_syncPrefix).append("sync")) , m_syncInterestPrefix(ndn::Name(m_syncPrefix).append("sync"))
, m_syncDataContentType(ndn::tlv::ContentType_Blob) , m_syncDataContentType(ndn::tlv::ContentType_Blob)
, m_onReceiveHelloData(onReceiveHelloData) , m_onReceiveHelloData(opts.onHelloData)
, m_onUpdate(onUpdate) , m_onUpdate(opts.onUpdate)
, m_bloomFilter(count, false_positive) , m_bloomFilter(opts.bfCount, opts.bfFalsePositive)
, m_helloInterestLifetime(helloInterestLifetime) , m_helloInterestLifetime(opts.helloInterestLifetime)
, m_syncInterestLifetime(syncInterestLifetime) , m_syncInterestLifetime(opts.syncInterestLifetime)
, m_rng(ndn::random::getRandomNumberEngine()) , m_rng(ndn::random::getRandomNumberEngine())
, m_rangeUniformRandom(100, 500) , 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 bool
Consumer::addSubscription(const ndn::Name& prefix, uint64_t seqNo, bool callSyncDataCb) Consumer::addSubscription(const ndn::Name& prefix, uint64_t seqNo, bool callSyncDataCb)
{ {
+28 -11
View File
@@ -56,23 +56,40 @@ class Consumer
{ {
public: public:
/** /**
* @brief constructor * @brief Constructor options.
*
* @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
*/ */
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, Consumer(const ndn::Name& syncPrefix,
ndn::Face& face, ndn::Face& face,
const ReceiveHelloCallback& onReceiveHelloData, const ReceiveHelloCallback& onReceiveHelloData,
const UpdateCallback& onUpdate, const UpdateCallback& onUpdate,
unsigned int count, unsigned int count,
double false_positive, double falsePositive = 0.001,
ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME, ndn::time::milliseconds helloInterestLifetime = HELLO_INTEREST_LIFETIME,
ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME); ndn::time::milliseconds syncInterestLifetime = SYNC_INTEREST_LIFETIME);
+8 -8
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* -*- 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. * This file is part of PSync.
* See AUTHORS.md for complete list of PSync authors and contributors. * See AUTHORS.md for complete list of PSync authors and contributors.
@@ -32,18 +32,18 @@ class PSyncConsumer
public: public:
/** /**
* @brief Initialize consumer and start hello process * @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 syncPrefix should be the same as producer
* @param nSub number of subscriptions is used for the bloom filter (subscription list) size * @param nSub number of subscriptions is used for the bloom filter (subscription list) size
*/ */
PSyncConsumer(const ndn::Name& syncPrefix, int nSub) PSyncConsumer(const ndn::Name& syncPrefix, int nSub)
: m_nSub(nSub) : m_nSub(nSub)
, m_consumer(syncPrefix, m_face, , m_consumer(m_face, syncPrefix, [this] {
std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1), psync::Consumer::Options opts;
std::bind(&PSyncConsumer::processSyncUpdate, this, _1), opts.onHelloData = std::bind(&PSyncConsumer::afterReceiveHelloData, this, _1);
m_nSub, 0.001) 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 // This starts the consumer side by sending a hello interest to the producer
// When the producer responds with hello data, afterReceiveHelloData is called // When the producer responds with hello data, afterReceiveHelloData is called
+11 -12
View File
@@ -33,10 +33,9 @@ BOOST_AUTO_TEST_SUITE(TestConsumer)
BOOST_AUTO_TEST_CASE(AddSubscription) BOOST_AUTO_TEST_CASE(AddSubscription)
{ {
ndn::DummyClientFace face; ndn::DummyClientFace face;
Consumer consumer(Name("/psync"), face, Consumer::Options opts;
[] (const auto&) {}, opts.bfCount = 40;
[] (const auto&) {}, Consumer consumer(face, "/psync", opts);
40, 0.001);
Name subscription("test"); Name subscription("test");
@@ -48,10 +47,9 @@ BOOST_AUTO_TEST_CASE(AddSubscription)
BOOST_AUTO_TEST_CASE(RemoveSubscription) BOOST_AUTO_TEST_CASE(RemoveSubscription)
{ {
ndn::DummyClientFace face; ndn::DummyClientFace face;
Consumer consumer(Name("/psync"), face, Consumer::Options opts;
[] (const auto&) {}, opts.bfCount = 40;
[] (const auto&) {}, Consumer consumer(face, "/psync", opts);
40, 0.001);
Name subscription("test"); Name subscription("test");
consumer.addSubscription(subscription, 0); consumer.addSubscription(subscription, 0);
@@ -65,10 +63,11 @@ BOOST_AUTO_TEST_CASE(RemoveSubscription)
BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, tests::IoFixture) BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, tests::IoFixture)
{ {
ndn::DummyClientFace face(m_io); ndn::DummyClientFace face(m_io);
Consumer consumer(Name("/psync"), face, Consumer::Options opts;
[] (const auto&) {}, opts.bfCount = 40;
[] (const auto&) {}, opts.helloInterestLifetime = 4_s;
40, 0.001, 4_s, 4_s); opts.syncInterestLifetime = 4_s;
Consumer consumer(face, "/psync", opts);
consumer.sendHelloInterest(); consumer.sendHelloInterest();
advanceClocks(4_s); advanceClocks(4_s);