37 #include <boost/range/adaptor/reversed.hpp> 45 const time::milliseconds SelfLearningStrategy::ROUTE_RENEW_LIFETIME(600_s);
52 BOOST_THROW_EXCEPTION(std::invalid_argument(
"SelfLearningStrategy does not accept parameters"));
55 BOOST_THROW_EXCEPTION(std::invalid_argument(
56 "SelfLearningStrategy does not support version " +
to_string(*parsed.
version)));
64 static Name strategyName(
"/localhost/nfd/strategy/self-learning/%FD%01");
70 const shared_ptr<pit::Entry>& pitEntry)
76 auto inRecordInfo = pitEntry->getInRecord(inFace)->insertStrategyInfo<
InRecordInfo>().first;
78 inRecordInfo->isNonDiscoveryInterest =
true;
79 if (nexthops.empty()) {
80 NFD_LOG_DEBUG(
"NACK non-discovery Interest=" << interest <<
" from=" << inFace.
getId() <<
" noNextHop");
82 nackHeader.
setReason(lp::NackReason::NO_ROUTE);
83 this->
sendNack(pitEntry, inFace, nackHeader);
87 multicastInterest(interest, inFace, pitEntry, nexthops);
91 inRecordInfo->isNonDiscoveryInterest =
false;
92 if (nexthops.empty()) {
93 broadcastInterest(interest, inFace, pitEntry);
97 multicastInterest(interest, inFace, pitEntry, nexthops);
104 const Face& inFace,
const Data& data)
108 if (!needPrefixAnn(pitEntry)) {
112 asyncProcessData(pitEntry, inFace, data);
117 if (paTag !=
nullptr) {
118 addRoute(pitEntry, inFace, data, *paTag->get().getPrefixAnn());
128 const shared_ptr<pit::Entry>& pitEntry)
131 if (nack.
getReason() == lp::NackReason::NO_ROUTE) {
140 SelfLearningStrategy::broadcastInterest(
const Interest& interest,
const Face& inFace,
141 const shared_ptr<pit::Entry>& pitEntry)
143 for (
auto& outFace : this->
getFaceTable() | boost::adaptors::reversed) {
149 pitEntry->getOutRecord(outFace)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest =
false;
150 NFD_LOG_DEBUG(
"send discovery Interest=" << interest <<
" from=" 151 << inFace.
getId() <<
" to=" << outFace.getId());
156 SelfLearningStrategy::multicastInterest(
const Interest& interest,
const Face& inFace,
157 const shared_ptr<pit::Entry>& pitEntry,
160 for (
const auto& nexthop : nexthops) {
161 Face& outFace = nexthop.getFace();
167 pitEntry->getOutRecord(outFace)->insertStrategyInfo<OutRecordInfo>().first->isNonDiscoveryInterest =
true;
168 NFD_LOG_DEBUG(
"send non-discovery Interest=" << interest <<
" from=" 169 << inFace.getId() <<
" to=" << outFace.getId());
174 SelfLearningStrategy::asyncProcessData(
const shared_ptr<pit::Entry>& pitEntry,
const Face& inFace,
const Data& data)
181 runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data,
this] {
183 [pitEntryWeak, inFaceId, data,
this] (optional<ndn::PrefixAnnouncement> paOpt) {
186 auto pitEntry = pitEntryWeak.lock();
187 auto inFace = this->
getFace(inFaceId);
188 if (pitEntry && inFace) {
189 NFD_LOG_DEBUG(
"found PrefixAnnouncement=" << pa.getAnnouncedName());
204 SelfLearningStrategy::needPrefixAnn(
const shared_ptr<pit::Entry>& pitEntry)
206 bool hasDiscoveryInterest =
false;
207 bool directToConsumer =
true;
210 for (
const auto& inRecord : pitEntry->getInRecords()) {
211 if (inRecord.getExpiry() > now) {
212 InRecordInfo* inRecordInfo = inRecord.getStrategyInfo<InRecordInfo>();
213 if (inRecordInfo && !inRecordInfo->isNonDiscoveryInterest) {
214 hasDiscoveryInterest =
true;
217 directToConsumer =
false;
221 return hasDiscoveryInterest && !directToConsumer;
225 SelfLearningStrategy::addRoute(
const shared_ptr<pit::Entry>& pitEntry,
const Face& inFace,
228 runOnRibIoService([pitEntryWeak = weak_ptr<pit::Entry>{pitEntry}, inFaceId = inFace.getId(), data, pa] {
231 NFD_LOG_DEBUG(
"Add route via PrefixAnnouncement with result=" << res);
237 SelfLearningStrategy::renewRoute(
const Name&
name,
FaceId inFaceId, time::milliseconds maxLifetime)
void setTag(shared_ptr< T > tag) const
set a tag item
std::vector< NextHop > NextHopList
static ParsedInstanceName parseInstanceName(const Name &input)
parse a strategy instance name
shared_ptr< T > getTag() const
get a tag item
SelfLearningStrategy(Forwarder &forwarder, const Name &name=getStrategyName())
generalization of a network interface
PartialName parameters
parameter components
static time_point now() noexcept
void sendNack(const shared_ptr< pit::Entry > &pitEntry, const Face &outFace, const lp::NackHeader &header)
send Nack to outFace
void runOnRibIoService(const std::function< void()> &f)
run a function on the RIB io_service instance
void sendNacks(const shared_ptr< pit::Entry > &pitEntry, const lp::NackHeader &header, std::initializer_list< const Face * > exceptFaces=std::initializer_list< const Face * >())
send Nack to every face that has an in-record, except those in exceptFaces
Face * getFace(FaceId id) const
StrategyInfo on pit::OutRecord.
void setInstanceName(const Name &name)
set strategy instance name
Represents an Interest packet.
const NackHeader & getHeader() const
void runOnMainIoService(const std::function< void()> &f)
run a function on the main io_service instance
void afterReceiveData(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data) override
trigger after Data is received
Represents a collection of nexthops.
void afterReceiveNack(const Face &inFace, const lp::Nack &nack, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Nack is received
A prefix announcement object that represents an application's intent of registering a prefix toward i...
represents a Network Nack
NackReason getReason() const
provides a tag type for simple types
Copyright (c) 2011-2015 Regents of the University of California.
NFD_REGISTER_STRATEGY(AccessStrategy)
bool isNonDiscoveryInterest
void rejectPendingInterest(const shared_ptr< pit::Entry > &pitEntry)
schedule the PIT entry for immediate deletion
void slAnnounce(const ndn::PrefixAnnouncement &pa, uint64_t faceId, time::milliseconds maxLifetime, const SlAnnounceCallback &cb)
Insert a route by prefix announcement from self-learning strategy.
Represents an absolute name.
RibManager & getRibManager()
represents a forwarding strategy
represents a zero-length TLV-VALUE
static Service & get()
Get a reference to the only instance of this class.
optional< uint64_t > version
whether strategyName contains a version component
This file contains common algorithms used by forwarding strategies.
const fib::Entry & lookupFib(const pit::Entry &pitEntry) const
performs a FIB lookup, considering Link object if present
void slFindAnn(const Name &name, const SlFindAnnCallback &cb) const
Retrieve an outgoing prefix announcement for self-learning strategy.
const FaceTable & getFaceTable() const
StrategyInfo on pit::InRecord.
static Name makeInstanceName(const Name &input, const Name &strategyName)
construct a strategy instance name
void setExpiryTimer(const shared_ptr< pit::Entry > &pitEntry, time::milliseconds duration)
Schedule the PIT entry to be erased after duration.
void sendDataToAll(const shared_ptr< pit::Entry > &pitEntry, const Face &inFace, const Data &data)
send data to all matched and qualified faces
std::string to_string(const V &v)
const NextHopList & getNextHops() const
uint64_t FaceId
identifies a face
static const Name & getStrategyName()
#define NFD_LOG_INIT(name)
void slRenew(const Name &name, uint64_t faceId, time::milliseconds maxLifetime, const SlAnnounceCallback &cb)
Renew a route created by prefix announcement from self-learning strategy.
Represents a Data packet.
void afterReceiveInterest(const Face &inFace, const Interest &interest, const shared_ptr< pit::Entry > &pitEntry) override
trigger after Interest is received
void sendInterest(const shared_ptr< pit::Entry > &pitEntry, Face &outFace, const Interest &interest)
send Interest to outFace
bool wouldViolateScope(const Face &inFace, const Interest &interest, const Face &outFace)
determine whether forwarding the Interest in pitEntry to outFace would violate scope
const Interest & getInterest() const
const Name & getName() const