37 time::milliseconds interestLifetime)
41 , m_lastSequenceNo(
std::numeric_limits<uint64_t>::max())
42 , m_lastNackSequenceNo(
std::numeric_limits<uint64_t>::max())
44 , m_scheduler(face.getIoService())
45 , m_nackEvent(m_scheduler)
46 , m_interestLifetime(interestLifetime)
59 this->sendInitialInterest();
69 if (m_lastInterestId != 0)
75 NotificationSubscriberBase::sendInitialInterest()
77 if (this->shouldStop())
80 auto interest = make_shared<Interest>(m_prefix);
81 interest->setMustBeFresh(
true);
82 interest->setChildSelector(1);
86 bind(&NotificationSubscriberBase::afterReceiveData,
this, _2),
87 bind(&NotificationSubscriberBase::afterReceiveNack,
this, _2),
88 bind(&NotificationSubscriberBase::afterTimeout,
this));
92 NotificationSubscriberBase::sendNextInterest()
94 if (this->shouldStop())
97 BOOST_ASSERT(m_lastSequenceNo != std::numeric_limits<uint64_t>::max());
99 Name nextName = m_prefix;
102 auto interest = make_shared<Interest>(nextName);
106 bind(&NotificationSubscriberBase::afterReceiveData,
this, _2),
107 bind(&NotificationSubscriberBase::afterReceiveNack,
this, _2),
108 bind(&NotificationSubscriberBase::afterTimeout,
this));
112 NotificationSubscriberBase::shouldStop()
116 if (!this->hasSubscriber() &&
onNack.isEmpty()) {
124 NotificationSubscriberBase::afterReceiveData(
const Data& data)
126 if (this->shouldStop())
134 this->sendInitialInterest();
138 if (!this->decodeAndDeliver(data)) {
140 this->sendInitialInterest();
144 this->sendNextInterest();
148 NotificationSubscriberBase::afterReceiveNack(
const lp::Nack& nack)
150 if (this->shouldStop())
155 time::milliseconds delay = exponentialBackoff(nack);
156 m_nackEvent = m_scheduler.
scheduleEvent(delay, [
this] {this->sendInitialInterest();});
160 NotificationSubscriberBase::afterTimeout()
162 if (this->shouldStop())
167 this->sendInitialInterest();
171 NotificationSubscriberBase::exponentialBackoff(
lp::Nack nack)
173 uint64_t nackSequenceNo;
182 if (m_lastNackSequenceNo == nackSequenceNo) {
189 m_lastNackSequenceNo = nackSequenceNo;
191 return time::milliseconds(static_cast<time::milliseconds::rep>(std::pow(2, m_attempts) * 100 +
void start()
start or resume receiving notifications
const Name & getName() const
Copyright (c) 2013-2017 Regents of the University of California.
virtual ~NotificationSubscriberBase()
time::milliseconds getInterestLifetime() const
const Interest & getInterest() const
EventId scheduleEvent(time::nanoseconds after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
represents a Network Nack
uint32_t generateWord32()
Generate a non-cryptographically-secure random integer in the range [0, 2^32)
signal::Signal< NotificationSubscriberBase, lp::Nack > onNack
fires when a NACK is received
signal::Signal< NotificationSubscriberBase > onTimeout
fires when no Notification is received within .getInterestLifetime period
Provide a communication channel with local or remote NDN forwarder.
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Represents an absolute name.
const Name & getName() const
Get name.
signal::Signal< NotificationSubscriberBase, Data > onDecodeError
fires when a Data packet in the Notification Stream cannot be decoded as Notification ...
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Represents a Data packet.
const Component & get(ssize_t i) const
Get the component at the given index.
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
represents an error in TLV encoding or decoding
NotificationSubscriberBase(Face &face, const Name &prefix, time::milliseconds interestLifetime)
construct a NotificationSubscriber
void stop()
stop receiving notifications