Skip to content

Commit

Permalink
face: PacketHandler::OutgoingPendingInterest
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jan 15, 2021
1 parent e8de47c commit 2dd5e95
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 74 deletions.
23 changes: 9 additions & 14 deletions src/ndnph/app/ndncert/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,9 @@ class Client : public PacketHandler
: m_client(client)
{}

bool operator()(State state, int deadline = 4000)
bool operator()(State state)
{
m_client->m_state = state;
m_client->m_deadline = ndnph::port::Clock::add(ndnph::port::Clock::now(), deadline);
m_set = true;
return true;
}
Expand All @@ -331,14 +330,13 @@ class Client : public PacketHandler

explicit Client(const Options& opts)
: PacketHandler(opts.face)
, m_pending(this)
, m_profile(opts.profile)
, m_challenges(opts.challenges)
, m_pvt(opts.pvt)
, m_cb(opts.cb)
, m_cbCtx(opts.ctx)
{
ndnph::port::RandomSource::generate(reinterpret_cast<uint8_t*>(&m_lastPitToken),
sizeof(m_lastPitToken));
sendNewRequest(opts.pub);
}

Expand All @@ -356,7 +354,7 @@ class Client : public PacketHandler
case State::WaitNewResponse:
case State::WaitChallengeResponse:
case State::WaitIssuedCert: {
if (ndnph::port::Clock::isBefore(m_deadline, ndnph::port::Clock::now())) {
if (m_pending.expired()) {
m_state = State::Failure;
}
break;
Expand All @@ -377,7 +375,7 @@ class Client : public PacketHandler

bool processData(Data data) final
{
if (getCurrentPacketInfo()->pitToken != m_lastPitToken) {
if (!m_pending.matchPitToken()) {
return false;
}

Expand Down Expand Up @@ -417,8 +415,7 @@ class Client : public PacketHandler
return;
}

send(m_newRequest.toInterest(region, m_profile, m_signingPolicy, m_pvt),
WithPitToken(++m_lastPitToken)) &&
m_pending.send(m_newRequest.toInterest(region, m_profile, m_signingPolicy, m_pvt)) &&
gotoState(State::WaitNewResponse);
}

Expand Down Expand Up @@ -469,9 +466,8 @@ class Client : public PacketHandler
{
StaticRegion<2048> region;
GotoState gotoState(this);
send(m_challengeRequest.toInterest(region, m_profile, m_newResponse.requestId, m_sessionKey,
m_signingPolicy, m_pvt),
WithPitToken(++m_lastPitToken)) &&
m_pending.send(m_challengeRequest.toInterest(region, m_profile, m_newResponse.requestId,
m_sessionKey, m_signingPolicy, m_pvt)) &&
gotoState(State::WaitChallengeResponse);
}

Expand Down Expand Up @@ -506,7 +502,7 @@ class Client : public PacketHandler
return;
}
interest.setName(m_challengeResponse.issuedCertName);
send(interest, WithPitToken(++m_lastPitToken)) && gotoState(State::WaitIssuedCert);
m_pending.send(interest) && gotoState(State::WaitIssuedCert);
}

bool handleIssuedCert(Data data)
Expand All @@ -525,9 +521,8 @@ class Client : public PacketHandler
}

private:
OutgoingPendingInterest m_pending;
State m_state = State::SendNewRequest;
port::Clock::Time m_deadline;
uint64_t m_lastPitToken = 0;

const CaProfile& m_profile;
ChallengeList m_challenges;
Expand Down
95 changes: 95 additions & 0 deletions src/ndnph/face/packet-handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,101 @@ class PacketHandler
return pi != nullptr && send(std::forward<Arg>(arg)..., *pi);
}

/** @brief Helper to keep track an outgoing pending Interest. */
class OutgoingPendingInterest
{
public:
OutgoingPendingInterest(PacketHandler* ph)
: m_ph(*ph)
{
port::RandomSource::generate(reinterpret_cast<uint8_t*>(&m_pitToken), sizeof(m_pitToken));
m_expire = port::Clock::now();
}

/**
* @brief Send an Interest.
* @tparam Packet @c Interest or its parameterized / signed variant.
* @param interest the Interest.
* @param timeout timeout in milliseconds. Default is InterestLifetime.
* @param arg other arguments to @c PacketHandler::send() .
*/
template<typename Packet, typename... Arg>
bool send(const Packet& interest, int timeout, Arg&&... arg)
{
m_expire = ndnph::port::Clock::add(ndnph::port::Clock::now(), timeout);
return m_ph.send(interest, WithPitToken(++m_pitToken), std::forward<Arg>(arg)...);
}

template<typename Packet>
bool send(const Packet& interest)
{
return send(interest, interest.getLifetime());
}

template<typename Packet, typename ArgFirst, typename... Arg,
typename = typename std::enable_if<!std::is_integral<ArgFirst>::value>::type>
bool send(const Packet& interest, ArgFirst&& arg1, Arg&&... arg)
{
return send(interest, interest.getLifetime(), std::forward<ArgFirst>(arg1),
std::forward<Arg>(arg)...);
}

/**
* @brief Compare PIT token of current incoming packet against last outgoing Interest.
* @pre one of processInterest, processData, or processNack is executing.
*
* Comparing PIT token alone is unreliable because PIT token is not guaranteed to be unique.
* If the application has saved a copy of the outgoing Interest or its name, it's
* recommended to use @c match() instead.
*/
bool matchPitToken() const
{
auto pi = m_ph.getCurrentPacketInfo();
return pi != nullptr && pi->pitToken == m_pitToken;
}

/**
* @brief Check Interest-Data match.
* @pre processData is executing.
* @param data incoming Data.
* @param interest saved outgoing Interest.
*/
bool match(const Data& data, const Interest& interest) const
{
return matchPitToken() && interest.match(data);
}

/**
* @brief Check Interest-Data match.
* @pre processData is executing.
* @param data incoming Data.
* @param name saved outgoing Interest name.
* @param canBePrefix CanBePrefix flag on the Interest.
*/
bool match(const Data& data, const Name& name, bool canBePrefix = true) const
{
StaticRegion<512> region;
auto interest = region.create<Interest>();
if (!interest) {
return false;
}
interest.setName(name);
interest.setCanBePrefix(canBePrefix);
return match(data, interest);
}

/** @brief Determine if the pending Interest has expired / timed out. */
bool expired() const
{
return ndnph::port::Clock::isBefore(m_expire, ndnph::port::Clock::now());
}

private:
PacketHandler& m_ph;
uint64_t m_pitToken = 0;
port::Clock::Time m_expire;
};

private:
/** @brief Override to be invoked periodically. */
virtual void loop() {}
Expand Down
60 changes: 30 additions & 30 deletions src/ndnph/packet/interest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ class InterestRefBase : public RefRegion<InterestObj>
public:
using RefRegion::RefRegion;

const Name& getName() const
{
return obj->name;
}

bool getCanBePrefix() const
{
return obj->canBePrefix;
}

bool getMustBeFresh() const
{
return obj->mustBeFresh;
}

uint32_t getNonce() const
{
return obj->nonce;
}

uint16_t getLifetime() const
{
return obj->lifetime;
}

uint8_t getHopLimit() const
{
return obj->hopLimit;
}

protected:
~InterestRefBase() = default;

Expand Down Expand Up @@ -258,61 +288,31 @@ class Interest : public detail::InterestRefBase
public:
using InterestRefBase::InterestRefBase;

const Name& getName() const
{
return obj->name;
}

void setName(const Name& v)
{
obj->name = v;
}

bool getCanBePrefix() const
{
return obj->canBePrefix;
}

void setCanBePrefix(bool v)
{
obj->canBePrefix = v;
}

bool getMustBeFresh() const
{
return obj->mustBeFresh;
}

void setMustBeFresh(bool v)
{
obj->mustBeFresh = v;
}

uint32_t getNonce() const
{
return obj->nonce;
}

void setNonce(uint32_t v)
{
obj->nonce = v;
}

uint16_t getLifetime() const
{
return obj->lifetime;
}

void setLifetime(uint16_t v)
{
obj->lifetime = v;
}

uint8_t getHopLimit() const
{
return obj->hopLimit;
}

void setHopLimit(uint8_t v)
{
obj->hopLimit = v;
Expand Down
Loading

0 comments on commit 2dd5e95

Please sign in to comment.