37 : m_scheduler(scheduler)
38 , m_refreshTime(2 * conf.getLsaRefreshTime())
39 , m_controller(face, keyChain)
40 , m_adjacencyList(adjacencyList)
41 , m_confParameter(conf)
49 auto it = m_table.find(name);
52 if (it != m_table.end() && isNotNeighbor((it->second).name)) {
53 for (
const auto& nexthop : (it->second).nexthopSet) {
54 unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
63 const ndn::Name& name = entry.
name;
65 bool shouldRegister = isNotNeighbor(name);
67 for (
const auto& hop : hopsToAdd)
76 hop.getRouteCostAsAdjustedInteger(),
77 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
78 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
90 unsigned int maxFaces = getNumberOfFacesForName(allHops);
93 unsigned int nFaces = 0;
96 for (
auto it = allHops.
cbegin(); it != allHops.
cend() && nFaces < maxFaces; ++it, ++nFaces) {
100 auto entryIt = m_table.find(name);
103 if (entryIt == m_table.end() && hopsToAdd.
size() != 0) {
108 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
110 entryIt = m_table.try_emplace(name, std::move(entry)).first;
118 if (hopsToAdd.
size() == 0) {
124 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
126 std::set<NextHop, NextHopUriSortedComparator> hopsToRemove;
129 std::inserter(hopsToRemove, hopsToRemove.begin()),
132 bool isUpdatable = isNotNeighbor(entry.
name);
134 for (
const auto& hop : hopsToRemove){
136 unregisterPrefix(entry.
name, hop.getConnectingFaceUri());
144 entryIt = m_table.find(name);
147 if (entryIt != m_table.end() &&
148 !entryIt->second.refreshEventId &&
149 isNotNeighbor(entryIt->second.name)) {
150 scheduleEntryRefresh(entryIt->second, [
this] (
FibEntry& entry) { scheduleLoop(entry); });
158 for (
const auto& it : m_table) {
159 for (
const auto& hop : it.second.nexthopSet) {
160 unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
166 Fib::getNumberOfFacesForName(
const NexthopList& nextHopList)
168 uint32_t nNextHops =
static_cast<uint32_t
>(nextHopList.
getNextHops().size());
172 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
176 Fib::isNotNeighbor(
const ndn::Name& name)
183 uint64_t faceCost,
const ndn::time::milliseconds& timeout,
184 uint64_t flags, uint8_t times)
186 uint64_t faceId = m_adjacencyList.
getFaceId(faceUri);
189 ndn::nfd::ControlParameters faceParameters;
195 .setExpirationPeriod(timeout)
196 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
198 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
199 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
200 std::bind(&Fib::onRegistrationSuccess,
this, _1, faceUri),
201 std::bind(&Fib::onRegistrationFailure,
this, _1,
202 faceParameters, faceUri, times));
205 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
210 Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& param,
211 const ndn::FaceUri& faceUri)
213 NLSR_LOG_DEBUG(
"Successful in name registration: " << param.getName() <<
214 " Face Uri: " << faceUri <<
" faceId: " << param.getFaceId());
217 if (adjacent != m_adjacencyList.
end()) {
218 adjacent->setFaceId(param.getFaceId());
224 Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
225 const ndn::nfd::ControlParameters& parameters,
226 const ndn::FaceUri& faceUri,
229 NLSR_LOG_DEBUG(
"Failed in name registration: " << response.getText() <<
230 " (code: " << response.getCode() <<
")");
231 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << +times);
235 parameters.getCost(),
236 parameters.getExpirationPeriod(),
237 parameters.getFlags(), times+1);
245 Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const ndn::FaceUri& faceUri)
249 if (adjacent != m_adjacencyList.
end()) {
250 faceId = adjacent->getFaceId();
253 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
255 ndn::nfd::ControlParameters controlParameters;
259 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
261 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
262 [] (
const ndn::nfd::ControlParameters& commandSuccessResult) {
263 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
264 " Face Id: " << commandSuccessResult.getFaceId());
266 [] (
const ndn::nfd::ControlResponse& response) {
267 NLSR_LOG_DEBUG(
"Failed in unregistering name" <<
": " << response.getText() <<
268 " (code: " << response.getCode() <<
")");
276 ndn::nfd::ControlParameters parameters;
279 .setStrategy(strategy);
281 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
282 std::bind(&Fib::onSetStrategySuccess,
this, _1),
283 std::bind(&Fib::onSetStrategyFailure,
this, _1,
288 Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
290 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
291 " for name: " << commandSuccessResult.getName());
295 Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse& response,
296 const ndn::nfd::ControlParameters& parameters,
299 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
300 " for name: " << parameters.getName());
302 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
310 " Seq Num: " << entry.seqNo <<
311 " in " << m_refreshTime <<
" seconds");
313 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
314 std::bind(&Fib::refreshEntry,
this,
315 entry.name, refreshCallback));
319 Fib::scheduleLoop(FibEntry& entry)
321 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop,
this, _1));
327 auto it = m_table.find(name);
328 if (it == m_table.end()) {
332 FibEntry& entry = it->second;
333 NLSR_LOG_DEBUG(
"Refreshing " << entry.name <<
" Seq Num: " << entry.seqNo);
337 for (
const NextHop& hop : entry.nexthopSet) {
339 ndn::FaceUri(hop.getConnectingFaceUri()),
340 hop.getRouteCostAsAdjustedInteger(),
341 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
342 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
351 NLSR_LOG_DEBUG(
"-------------------FIB-----------------------------");
352 for (
const auto& entry : m_table) {
uint64_t getFaceId(const ndn::FaceUri &faceUri)
const_iterator end() const
bool isNeighbor(const ndn::Name &adjName) const
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
A class to house all the configuration parameters for NLSR.
uint32_t getMaxFacesPerPrefix() const
void setStrategy(const ndn::Name &name, const ndn::Name &strategy, uint32_t count)
void clean()
Remove all entries from the FIB.
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::KeyChain &keyChain)
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.
ndn::signal::Signal< Fib, ndn::Name > onPrefixRegistrationSuccess
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
const_iterator cend() const
const_iterator cbegin() const
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
const std::set< NextHop, T > & getNextHops() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
#define INIT_LOGGER(name)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::function< void(FibEntry &)> AfterRefreshCallback
NextHopsUriSortedSet nexthopSet