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)
203 const ndn::time::milliseconds& timeout,
204 uint64_t flags, uint8_t times)
206 uint64_t faceId = m_adjacencyList.
getFaceId(ndn::FaceUri(faceUri));
209 ndn::nfd::ControlParameters faceParameters;
215 .setExpirationPeriod(timeout)
216 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
218 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
219 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
220 std::bind(&Fib::onRegistrationSuccess,
this, _1,
221 "Successful in name registration",
223 std::bind(&Fib::onRegistrationFailure,
this, _1,
224 "Failed in name registration",
229 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
234 Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& commandSuccessResult,
235 const std::string& message,
const ndn::FaceUri& faceUri)
237 NLSR_LOG_DEBUG(message <<
": " << commandSuccessResult.getName() <<
238 " Face Uri: " << faceUri <<
" faceId: " << commandSuccessResult.getFaceId());
241 if (adjacent != m_adjacencyList.
end()) {
242 adjacent->setFaceId(commandSuccessResult.getFaceId());
246 m_faceMap.
update(faceUri.toString(), commandSuccessResult.getFaceId());
251 Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
252 const std::string& message,
253 const ndn::nfd::ControlParameters& parameters,
254 const ndn::FaceUri& faceUri,
257 NLSR_LOG_DEBUG(message <<
": " << response.getText() <<
" (code: " << response.getCode() <<
")");
258 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << times);
262 parameters.getCost(),
263 parameters.getExpirationPeriod(),
264 parameters.getFlags(), times+1);
272 Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const std::string& faceUri)
274 uint32_t faceId = m_faceMap.
getFaceId(faceUri);
275 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
277 ndn::nfd::ControlParameters controlParameters;
281 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
282 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
283 std::bind(&Fib::onUnregistrationSuccess,
this, _1,
284 "Successful in unregistering name"),
285 std::bind(&Fib::onUnregistrationFailure,
287 "Failed in unregistering name"));
292 Fib::onUnregistrationSuccess(
const ndn::nfd::ControlParameters& commandSuccessResult,
293 const std::string& message)
295 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
296 " Face Id: " << commandSuccessResult.getFaceId());
300 Fib::onUnregistrationFailure(
const ndn::nfd::ControlResponse& response,
301 const std::string& message)
303 NLSR_LOG_DEBUG(message <<
": " << response.getText() <<
" (code: " << response.getCode() <<
")");
309 ndn::nfd::ControlParameters parameters;
312 .setStrategy(strategy);
314 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
315 std::bind(&Fib::onSetStrategySuccess,
this, _1),
316 std::bind(&Fib::onSetStrategyFailure,
this, _1,
321 Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
323 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
324 " for name: " << commandSuccessResult.getName());
328 Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse& response,
329 const ndn::nfd::ControlParameters& parameters,
332 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
333 " for name: " << parameters.getName());
335 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
344 " in " << m_refreshTime <<
" seconds");
347 std::bind(&Fib::refreshEntry,
this,
348 entry.
getName(), refreshCallback)));
354 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop,
this, _1));
358 Fib::cancelEntryRefresh(
const FibEntry& entry)
367 auto it = m_table.find(name);
368 if (it == m_table.end()) {
378 for (
const NextHop& hop : entry) {
380 ndn::FaceUri(hop.getConnectingFaceUri()),
381 hop.getRouteCostAsAdjustedInteger(),
382 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
383 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
392 NLSR_LOG_DEBUG(
"-------------------FIB-----------------------------");
393 for (
const auto& entry : m_table) {
void setRefreshEventId(ndn::scheduler::EventId id)
A class to house all the configuration parameters for NLSR.
const std::set< NextHop, NextHopComparator > & getNextHops() const
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
static const std::string MULTICAST_STRATEGY
const_iterator cend() const
bool isNeighbor(const ndn::Name &adjName) 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
const_iterator cbegin() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
uint32_t getMaxFacesPerPrefix() const
NexthopList & getNexthopList()
void setSeqNo(int32_t fsn)
ndn::scheduler::EventId getRefreshEventId() const
void clean()
Remove all entries from the FIB.
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::v2::KeyChain &keyChain)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const ndn::Name & getName() const
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
uint64_t getFaceId(const ndn::FaceUri &faceUri)
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
void update(const std::string &faceUri, uint32_t faceId)
std::function< void(FibEntry &)> afterRefreshCallback
const_iterator end() const
uint32_t getFaceId(const std::string &faceUri)
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.