From fe99a7831c31b8cc08689fb0b609c0c0d10d84c2 Mon Sep 17 00:00:00 2001 From: matianxing1992 Date: Wed, 15 May 2024 12:54:27 -0500 Subject: [PATCH 1/3] Fix the problem of duplicate nonce; Add APIs to set m_maxRetries and m_defaultTimeout --- src/consumer.cpp | 26 +++++++++++++++++++++++--- src/consumer.hpp | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/consumer.cpp b/src/consumer.cpp index c6e37bd..6bc58ca 100644 --- a/src/consumer.cpp +++ b/src/consumer.cpp @@ -26,6 +26,8 @@ #include #include +#include + namespace ndn { namespace nacabe { @@ -102,6 +104,7 @@ Consumer::consume(const Name& dataName, // Application data can be fetched long after they have been published, // so we should not set the MustBeFresh flag. interest.setCanBePrefix(true); + interest.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout)); consume(interest, consumptionCb, errorCallback); } @@ -153,10 +156,22 @@ Consumer::consume(const Interest& dataInterest, m_face.expressInterest(dataInterest, dataCallback, std::bind(&Consumer::handleNack, this, _1, _2, errorCallback, nackMessage), - std::bind(&Consumer::handleTimeout, this, _1, 3, + std::bind(&Consumer::handleTimeout, this, _1, m_maxRetries, dataCallback, errorCallback, nackMessage, timeoutMessage)); } +void +Consumer::setMaxRetries(int maxRetries) +{ + m_maxRetries = maxRetries; +} + +void +Consumer::setDefaultTimeout(int defaultTimeout) +{ + m_defaultTimeout = defaultTimeout; +} + void Consumer::decryptContent(const Name& dataObjName, const Block& content, @@ -176,6 +191,7 @@ Consumer::decryptContent(const Name& dataObjName, Name ckName(content.get(tlv::Name)); NDN_LOG_INFO("CK Name is " << ckName); Interest ckInterest(ckName); + ckInterest.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout)); ckInterest.setCanBePrefix(true); std::string nackMessage = "Nack for " + ckName.toUri() + " content key fetch with reason "; @@ -216,7 +232,7 @@ Consumer::decryptContent(const Name& dataObjName, m_face.expressInterest(ckInterest, dataCallback, std::bind(&Consumer::handleNack, this, _1, _2, errorCallback, nackMessage), - std::bind(&Consumer::handleTimeout, this, _1, 3, + std::bind(&Consumer::handleTimeout, this, _1, m_maxRetries, dataCallback, errorCallback, nackMessage, timeoutMessage)); } @@ -270,7 +286,11 @@ Consumer::handleTimeout(const Interest& interest, int nRetrials, { if (nRetrials > 0) { NDN_LOG_INFO("Timeout for: " << interest << ", retrying"); - m_face.expressInterest(interest, dataCallback, + auto interestNew = const_cast(interest); + int factor = (int) std::pow(2, m_maxRetries+1-nRetrials); + interestNew.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout*factor)); + interestNew.setNonce(std::nullopt); + m_face.expressInterest(interestNew, dataCallback, std::bind(&Consumer::handleNack, this, _1, _2, errorCallback, nackMessage), std::bind(&Consumer::handleTimeout, this, _1, nRetrials - 1, dataCallback, errorCallback, nackMessage, timeoutMessage)); diff --git a/src/consumer.hpp b/src/consumer.hpp index fdc95ee..6f31d0c 100644 --- a/src/consumer.hpp +++ b/src/consumer.hpp @@ -86,6 +86,20 @@ class Consumer const ConsumptionCallback& consumptionCb, const ErrorCallback& errorCallback); + /** + * @brief Set the maximum number of retries for fetching data packets. + * @param maxRetries The maximum number of retries. + */ + void + setMaxRetries(int maxRetries); + + /** + * @brief Set the default timeout for fetching data packets. + * @param defaultTimeout The default timeout in milliseconds. + */ + void + setDefaultTimeout(int defaultTimeout); + private: void decryptContent(const Name& dataObjName, @@ -119,6 +133,9 @@ class Consumer TrustConfig m_trustConfig; algo::PrivateKey m_keyCache; + int m_maxRetries = 3; + int m_defaultTimeout = 200; + PUBLIC_WITH_TESTS_ELSE_PRIVATE: ParamFetcher m_paramFetcher; }; From bf4d176c7ca00040a73daa3ebcc7fd4b3a65b74f Mon Sep 17 00:00:00 2001 From: matianxing1992 Date: Fri, 17 May 2024 12:06:33 -0500 Subject: [PATCH 2/3] Fix the issues in the PR --- src/consumer.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/consumer.cpp b/src/consumer.cpp index 6bc58ca..62847a4 100644 --- a/src/consumer.cpp +++ b/src/consumer.cpp @@ -286,11 +286,12 @@ Consumer::handleTimeout(const Interest& interest, int nRetrials, { if (nRetrials > 0) { NDN_LOG_INFO("Timeout for: " << interest << ", retrying"); - auto interestNew = const_cast(interest); - int factor = (int) std::pow(2, m_maxRetries+1-nRetrials); - interestNew.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout*factor)); - interestNew.setNonce(std::nullopt); - m_face.expressInterest(interestNew, dataCallback, + Interest interestRetry(interest); + int factor = static_cast(std::pow(2, m_maxRetries + 1 - nRetrials)); + interestRetry.setCanBePrefix(true); + interestRetry.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout*factor)); + interestRetry.setNonce(std::nullopt); + m_face.expressInterest(interestRetry, dataCallback, std::bind(&Consumer::handleNack, this, _1, _2, errorCallback, nackMessage), std::bind(&Consumer::handleTimeout, this, _1, nRetrials - 1, dataCallback, errorCallback, nackMessage, timeoutMessage)); From 6dd6be3e8ca33668214cdce6f3f04cd7348e949d Mon Sep 17 00:00:00 2001 From: matianxing1992 Date: Fri, 17 May 2024 12:18:43 -0500 Subject: [PATCH 3/3] Use refreshNonce() instead --- src/consumer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/consumer.cpp b/src/consumer.cpp index 62847a4..ed2c7bd 100644 --- a/src/consumer.cpp +++ b/src/consumer.cpp @@ -290,7 +290,7 @@ Consumer::handleTimeout(const Interest& interest, int nRetrials, int factor = static_cast(std::pow(2, m_maxRetries + 1 - nRetrials)); interestRetry.setCanBePrefix(true); interestRetry.setInterestLifetime(ndn::time::milliseconds(m_defaultTimeout*factor)); - interestRetry.setNonce(std::nullopt); + interestRetry.refreshNonce(); m_face.expressInterest(interestRetry, dataCallback, std::bind(&Consumer::handleNack, this, _1, _2, errorCallback, nackMessage), std::bind(&Consumer::handleTimeout, this, _1, nRetrials - 1,