Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem of duplicate nonce; Add APIs to set m_maxRetries and m_defaultTimeout #35

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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