Skip to content

Commit

Permalink
Merge pull request #35 from matianxing1992/master
Browse files Browse the repository at this point in the history
Fix the problem of duplicate nonce; Add APIs to set m_maxRetries and m_defaultTimeout
  • Loading branch information
tianyuan129 authored May 17, 2024
2 parents 5e9e853 + 6dd6be3 commit 235bc2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <ndn-cxx/security/verification-helpers.hpp>
#include <ndn-cxx/util/segment-fetcher.hpp>

#include <cmath>

namespace ndn {
namespace nacabe {

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -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 ";
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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<int>(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));
Expand Down
17 changes: 17 additions & 0 deletions src/consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 235bc2b

Please sign in to comment.