35 const time::milliseconds Readvertise::RETRY_DELAY_MIN = 50_s;
36 const time::milliseconds Readvertise::RETRY_DELAY_MAX = 3600_s;
38 static time::milliseconds
41 std::uniform_int_distribution<uint64_t> dist(-5, 5);
42 time::milliseconds newTime = baseTimer + time::milliseconds(dist(
getGlobalRng()));
43 return std::max(newTime, 0_ms);
47 unique_ptr<ReadvertisePolicy> policy,
48 unique_ptr<ReadvertiseDestination> destination)
49 : m_scheduler(scheduler)
50 , m_policy(std::move(policy))
51 , m_destination(std::move(destination))
53 m_addRouteConn = rib.
afterAddRoute.connect([
this] (
const auto& r) { this->afterAddRoute(r); });
54 m_removeRouteConn = rib.
beforeRemoveRoute.connect([
this] (
const auto& r) { this->beforeRemoveRoute(r); });
56 m_destination->afterAvailabilityChange.connect([
this] (
bool isAvailable) {
58 this->afterDestinationAvailable();
61 this->afterDestinationUnavailable();
67 Readvertise::afterAddRoute(
const RibRouteRef& ribRoute)
69 optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute);
72 ',' << ribRoute.
route->origin <<
") not-readvertising");
78 std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix);
80 if (!isNew && rrIt->signer != action->signer) {
82 ',' << ribRoute.
route->origin <<
") readvertising-as " << action->prefix <<
83 " old-signer " << rrIt->signer <<
" new-signer " << action->signer);
85 rrIt->signer = action->signer;
88 std::tie(indexIt, isNew) = m_routeToRr.emplace(ribRoute, rrIt);
91 if (rrIt->nRibRoutes++ > 0) {
93 ',' << ribRoute.
route->origin <<
") already-readvertised-as " << action->prefix);
98 ',' << ribRoute.
route->origin <<
") readvertising-as " << action->prefix <<
99 " signer " << action->signer);
100 rrIt->retryDelay = RETRY_DELAY_MIN;
101 this->advertise(rrIt);
105 Readvertise::beforeRemoveRoute(
const RibRouteRef& ribRoute)
107 auto indexIt = m_routeToRr.find(ribRoute);
108 if (indexIt == m_routeToRr.end()) {
109 NFD_LOG_DEBUG(
"remove-route " << ribRoute.entry->getName() <<
'(' << ribRoute.route->faceId <<
110 ',' << ribRoute.route->origin <<
") not-readvertised");
114 auto rrIt = indexIt->second;
115 m_routeToRr.erase(indexIt);
117 if (--rrIt->nRibRoutes > 0) {
118 NFD_LOG_DEBUG(
"remove-route " << ribRoute.entry->getName() <<
'(' << ribRoute.route->faceId <<
119 ',' << ribRoute.route->origin <<
") needed-by " << rrIt->nRibRoutes);
123 rrIt->retryDelay = RETRY_DELAY_MIN;
124 this->withdraw(rrIt);
128 Readvertise::afterDestinationAvailable()
130 for (
auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) {
131 rrIt->retryDelay = RETRY_DELAY_MIN;
132 this->advertise(rrIt);
137 Readvertise::afterDestinationUnavailable()
139 for (
auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) {
140 if (rrIt->nRibRoutes > 0) {
141 rrIt->retryEvt.cancel();
145 rrIt = m_rrs.erase(rrIt);
153 BOOST_ASSERT(rrIt->nRibRoutes > 0);
155 if (!m_destination->isAvailable()) {
156 NFD_LOG_DEBUG(
"advertise " << rrIt->prefix <<
" destination-unavailable");
160 m_destination->advertise(*rrIt,
163 rrIt->retryDelay = RETRY_DELAY_MIN;
165 [=] { advertise(rrIt); });
167 [=] (
const std::string& msg) {
168 NFD_LOG_DEBUG(
"advertise " << rrIt->prefix <<
" failure " << msg);
169 rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
171 [=] { advertise(rrIt); });
178 BOOST_ASSERT(rrIt->nRibRoutes == 0);
180 if (!m_destination->isAvailable()) {
181 NFD_LOG_DEBUG(
"withdraw " << rrIt->prefix <<
" destination-unavailable");
186 m_destination->withdraw(*rrIt,
191 [=] (
const std::string& msg) {
192 NFD_LOG_DEBUG(
"withdraw " << rrIt->prefix <<
" failure " << msg);
193 rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
195 [=] { withdraw(rrIt); });
Readvertise(Rib &rib, ndn::util::Scheduler &scheduler, unique_ptr< ReadvertisePolicy > policy, unique_ptr< ReadvertiseDestination > destination)
Declares the global pseudorandom number generator (PRNG) for NFD.
represents the Routing Information Base
ndn::util::signal::Signal< Rib, RibRouteRef > afterAddRoute
signals after a Route is added
RibEntry::const_iterator route
EventId scheduleEvent(time::nanoseconds after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
Table::const_iterator iterator
Copyright (c) 2011-2015 Regents of the University of California.
shared_ptr< RibEntry > entry
readvertise a subset of routes to a destination according to a policy
static time::milliseconds randomizeTimer(time::milliseconds baseTimer)
std::mt19937 & getGlobalRng()
#define NFD_LOG_INIT(name)
ndn::util::signal::Signal< Rib, RibRouteRef > beforeRemoveRoute
signals before a route is removed