36 const uint64_t Fib::GRACE_PERIOD = 10;
42 : m_scheduler(scheduler)
43 , m_refreshTime(2 * conf.getLsaRefreshTime())
44 , m_controller(face, keyChain)
45 , m_adjacencyList(adjacencyList)
46 , m_confParameter(conf)
54 auto it = m_table.find(name);
57 if (it != m_table.end() && isNotNeighbor((it->second).getName())) {
58 for (
const auto& nexthop : (it->second).getNexthopList().getNextHops()) {
59 unregisterPrefix((it->second).getName(), nexthop.getConnectingFaceUri());
61 cancelEntryRefresh(it->second);
69 const ndn::Name& name = entry.
getName();
71 bool shouldRegister = isNotNeighbor(name);
81 hop.getRouteCostAsAdjustedInteger(),
82 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
83 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
95 unsigned int maxFaces = getNumberOfFacesForName(allHops);
98 unsigned int nFaces = 0;
101 for (
auto it = allHops.
cbegin(); it != allHops.
cend() && nFaces < maxFaces; ++it, ++nFaces) {
105 auto entryIt = m_table.find(name);
108 if (entryIt == m_table.end() && hopsToAdd.
size() != 0) {
113 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
115 m_table.emplace(name, entry);
117 entryIt = m_table.find(name);
125 if (hopsToAdd.
size() == 0) {
130 FibEntry& entry = (entryIt->second);
131 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
133 std::set<NextHop, NextHopComparator> hopsToRemove;
138 bool isUpdatable = isNotNeighbor(entry.
getName());
140 for (
const auto& hop : hopsToRemove){
142 unregisterPrefix(entry.
getName(), hop.getConnectingFaceUri());
151 entryIt = m_table.find(name);
154 if (entryIt != m_table.end() &&
155 !entryIt->second.getRefreshEventId() &&
156 isNotNeighbor(entryIt->second.getName())) {
157 scheduleEntryRefresh(entryIt->second,
169 for (
auto&& it : m_table) {
170 NLSR_LOG_DEBUG(
"Canceling Scheduled event. Name: " << it.second.getName());
171 cancelEntryRefresh(it.second);
173 for (
const auto& hop : it.second.getNexthopList().getNextHops()) {
174 unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
180 Fib::getNumberOfFacesForName(
const NexthopList& nextHopList)
182 uint32_t nNextHops =
static_cast<uint32_t
>(nextHopList.
getNextHops().size());
186 if (nMaxFaces == 0) {
190 return std::min(nNextHops, nMaxFaces);
195 Fib::isNotNeighbor(
const ndn::Name& name)
202 uint64_t faceCost,
const ndn::time::milliseconds& timeout,
203 uint64_t flags, uint8_t times)
205 uint64_t faceId = m_adjacencyList.
getFaceId(faceUri);
208 ndn::nfd::ControlParameters faceParameters;
214 .setExpirationPeriod(timeout)
215 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
217 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
218 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
219 std::bind(&Fib::onRegistrationSuccess,
this, _1, faceUri),
220 std::bind(&Fib::onRegistrationFailure,
this, _1,
221 faceParameters, faceUri, times));
224 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
229 Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& param,
230 const ndn::FaceUri& faceUri)
232 NLSR_LOG_DEBUG(
"Successful in name registration: " << param.getName() <<
233 " Face Uri: " << faceUri <<
" faceId: " << param.getFaceId());
236 if (adjacent != m_adjacencyList.
end()) {
237 adjacent->setFaceId(param.getFaceId());
243 Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
244 const ndn::nfd::ControlParameters& parameters,
245 const ndn::FaceUri& faceUri,
248 NLSR_LOG_DEBUG(
"Failed in name registration: " << response.getText() <<
249 " (code: " << response.getCode() <<
")");
250 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << +times);
254 parameters.getCost(),
255 parameters.getExpirationPeriod(),
256 parameters.getFlags(), times+1);
264 Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const std::string& faceUri)
267 auto adjacent = m_adjacencyList.
findAdjacent(ndn::FaceUri(faceUri));
268 if (adjacent != m_adjacencyList.
end()) {
269 faceId = adjacent->getFaceId();
272 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
274 ndn::nfd::ControlParameters controlParameters;
278 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
280 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
281 [] (
const ndn::nfd::ControlParameters& commandSuccessResult) {
282 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
283 " Face Id: " << commandSuccessResult.getFaceId());
285 [] (
const ndn::nfd::ControlResponse& response) {
286 NLSR_LOG_DEBUG(
"Failed in unregistering name" <<
": " << response.getText() <<
287 " (code: " << response.getCode() <<
")");
295 ndn::nfd::ControlParameters parameters;
298 .setStrategy(strategy);
300 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
301 std::bind(&Fib::onSetStrategySuccess,
this, _1),
302 std::bind(&Fib::onSetStrategyFailure,
this, _1,
307 Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
309 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
310 " for name: " << commandSuccessResult.getName());
314 Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse& response,
315 const ndn::nfd::ControlParameters& parameters,
318 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
319 " for name: " << parameters.getName());
321 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
330 " in " << m_refreshTime <<
" seconds");
333 std::bind(&Fib::refreshEntry,
this,
334 entry.
getName(), refreshCallback)));
340 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop,
this, _1));
344 Fib::cancelEntryRefresh(
const FibEntry& entry)
353 auto it = m_table.find(name);
354 if (it == m_table.end()) {
364 for (
const NextHop& hop : entry) {
366 ndn::FaceUri(hop.getConnectingFaceUri()),
367 hop.getRouteCostAsAdjustedInteger(),
368 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
369 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
378 NLSR_LOG_DEBUG(
"-------------------FIB-----------------------------");
379 for (
const auto& entry : m_table) {
void setRefreshEventId(ndn::scheduler::EventId id)
A class to house all the configuration parameters for NLSR.
bool isNeighbor(const ndn::Name &adjName) const
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
const_iterator cend() const
static const std::string MULTICAST_STRATEGY
const ndn::Name & getName() const
#define NLSR_LOG_DEBUG(x)
void setStrategy(const ndn::Name &name, const std::string &strategy, uint32_t count)
static const std::string BEST_ROUTE_V2_STRATEGY
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
ndn::scheduler::EventId getRefreshEventId() const
#define INIT_LOGGER(name)
NexthopList & getNexthopList()
void setSeqNo(int32_t fsn)
void clean()
Remove all entries from the FIB.
uint32_t getMaxFacesPerPrefix() const
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::v2::KeyChain &keyChain)
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
const std::set< NextHop, NextHopComparator > & getNextHops() const
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
const_iterator end() const
const_iterator cbegin() const
ndn::util::Signal< Fib, const ndn::Name & > onPrefixRegistrationSuccess
uint64_t getFaceId(const ndn::FaceUri &faceUri)
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
std::function< void(FibEntry &)> afterRefreshCallback
void registerPrefix(const ndn::Name &namePrefix, const ndn::FaceUri &faceUri, uint64_t faceCost, const ndn::time::milliseconds &timeout, uint64_t flags, uint8_t times)
Inform NFD of a next-hop.