diff --git a/src/consumer.cpp b/src/consumer.cpp index c6e37bd..ed2c7bd 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,12 @@ Consumer::handleTimeout(const Interest& interest, int nRetrials, { if (nRetrials > 0) { NDN_LOG_INFO("Timeout for: " << interest << ", retrying"); - m_face.expressInterest(interest, 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.refreshNonce(); + 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)); 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; };