30 #include <ndn-cxx/util/random.hpp>
39 static time::milliseconds
42 std::uniform_int_distribution<> dist(-5, 5);
43 auto newTime = baseTimer + time::milliseconds(dist(ndn::random::getRandomNumberEngine()));
44 return std::max(newTime, 0_ms);
48 unique_ptr<ReadvertisePolicy> policy,
49 unique_ptr<ReadvertiseDestination> destination)
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 std::optional<ReadvertiseAction> action = m_policy->handleNewRoute(ribRoute);
72 ',' << ribRoute.
route->origin <<
") not-readvertising");
76 auto [rrIt, isNewRr] = m_rrs.emplace(action->prefix);
77 if (!isNewRr && rrIt->signer != action->signer) {
79 ',' << ribRoute.
route->origin <<
") readvertising-as " << action->prefix <<
80 " old-signer " << rrIt->signer <<
" new-signer " << action->signer);
82 rrIt->signer = action->signer;
84 bool isNewInMap = m_routeToRr.try_emplace(ribRoute, rrIt).second;
85 BOOST_VERIFY(isNewInMap);
87 if (rrIt->nRibRoutes++ > 0) {
89 ',' << ribRoute.
route->origin <<
") already-readvertised-as " << action->prefix);
94 ',' << ribRoute.
route->origin <<
") readvertising-as " << action->prefix <<
95 " signer " << action->signer);
97 this->advertise(rrIt);
101 Readvertise::beforeRemoveRoute(
const RibRouteRef& ribRoute)
103 auto indexIt = m_routeToRr.find(ribRoute);
104 if (indexIt == m_routeToRr.end()) {
105 NFD_LOG_DEBUG(
"remove-route " << ribRoute.entry->getName() <<
'(' << ribRoute.route->faceId <<
106 ',' << ribRoute.route->origin <<
") not-readvertised");
110 auto rrIt = indexIt->second;
111 m_routeToRr.erase(indexIt);
113 if (--rrIt->nRibRoutes > 0) {
114 NFD_LOG_DEBUG(
"remove-route " << ribRoute.entry->getName() <<
'(' << ribRoute.route->faceId <<
115 ',' << ribRoute.route->origin <<
") needed-by " << rrIt->nRibRoutes);
120 this->withdraw(rrIt);
124 Readvertise::afterDestinationAvailable()
126 for (
auto rrIt = m_rrs.begin(); rrIt != m_rrs.end(); ++rrIt) {
128 this->advertise(rrIt);
133 Readvertise::afterDestinationUnavailable()
135 for (
auto rrIt = m_rrs.begin(); rrIt != m_rrs.end();) {
136 if (rrIt->nRibRoutes > 0) {
137 rrIt->retryEvt.cancel();
141 rrIt = m_rrs.erase(rrIt);
147 Readvertise::advertise(ReadvertisedRouteContainer::iterator rrIt)
149 BOOST_ASSERT(rrIt->nRibRoutes > 0);
151 if (!m_destination->isAvailable()) {
152 NFD_LOG_DEBUG(
"advertise " << rrIt->prefix <<
" destination-unavailable");
156 m_destination->advertise(*rrIt,
161 [=] { advertise(rrIt); });
163 [=] (
const std::string& msg) {
164 NFD_LOG_DEBUG(
"advertise " << rrIt->prefix <<
" failure " << msg);
167 [=] { advertise(rrIt); });
172 Readvertise::withdraw(ReadvertisedRouteContainer::iterator rrIt)
174 BOOST_ASSERT(rrIt->nRibRoutes == 0);
176 if (!m_destination->isAvailable()) {
177 NFD_LOG_DEBUG(
"withdraw " << rrIt->prefix <<
" destination-unavailable");
182 m_destination->withdraw(*rrIt,
187 [=] (
const std::string& msg) {
188 NFD_LOG_DEBUG(
"withdraw " << rrIt->prefix <<
" failure " << msg);
191 [=] { withdraw(rrIt); });
Readvertise(Rib &rib, unique_ptr< ReadvertisePolicy > policy, unique_ptr< ReadvertiseDestination > destination)
Represents the Routing Information Base.
signal::Signal< Rib, RibRouteRef > afterAddRoute
Signals after a Route is added.
signal::Signal< Rib, RibRouteRef > beforeRemoveRoute
Signals before a route is removed.
#define NFD_LOG_INIT(name)
constexpr time::milliseconds RETRY_DELAY_MAX
static time::milliseconds randomizeTimer(time::milliseconds baseTimer)
constexpr time::milliseconds RETRY_DELAY_MIN
ndn::Scheduler & getScheduler()
Returns the global Scheduler instance for the calling thread.
shared_ptr< RibEntry > entry
RibEntry::const_iterator route