blob: a68d4d66c440a81e91c5b5ee3da5394d78b3e597 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -05002#include <cmath>
akmhoque157b0a42014-05-13 00:26:37 -05003#include <ndn-cxx/common.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -05004
5#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05006#include "nexthop-list.hpp"
akmhoque157b0a42014-05-13 00:26:37 -05007#include "face-map.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -05008#include "fib.hpp"
9
akmhoquec8a10f72014-04-25 18:42:55 -050010
akmhoque53353462014-04-22 08:43:45 -050011
12
13namespace nlsr {
14
15using namespace std;
16using namespace ndn;
17
18static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050019fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050020{
akmhoquefdbddb12014-05-02 18:35:19 -050021 return fibEntry.getName() == name ;
akmhoque53353462014-04-22 08:43:45 -050022}
23
24void
akmhoque31d1d4b2014-05-05 22:08:14 -050025Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050026{
akmhoque31d1d4b2014-05-05 22:08:14 -050027 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050028}
29
30
31ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050032Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum,
akmhoquefdbddb12014-05-02 18:35:19 -050033 int32_t refreshTime)
akmhoque53353462014-04-22 08:43:45 -050034{
akmhoquefdbddb12014-05-02 18:35:19 -050035 std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl;
36 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050037 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
38 ndn::bind(&Fib::refreshEntry, this,
39 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050040}
41
42void
akmhoque31d1d4b2014-05-05 22:08:14 -050043Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050044{
akmhoquefdbddb12014-05-02 18:35:19 -050045 std::cout << "Fib::refreshEntry Called" << std::endl;
46 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
47 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
48 m_table.end(),
49 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050050 if (it != m_table.end()) {
akmhoquefdbddb12014-05-02 18:35:19 -050051 std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050052 if (it->getSeqNo() == feSeqNum) {
akmhoquefdbddb12014-05-02 18:35:19 -050053 std::cout << "Refreshing the FIB entry" << std::endl;
54 for (std::list<NextHop>::iterator nhit =
akmhoque31d1d4b2014-05-05 22:08:14 -050055 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050056 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoquefdbddb12014-05-02 18:35:19 -050057 // add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -050058 registerPrefix(it->getName(), nhit->getConnectingFaceUri(),
59 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -050060 }
akmhoquefdbddb12014-05-02 18:35:19 -050061 // increase sequence number and schedule refresh again
62 it->setSeqNo(feSeqNum + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -050063 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
64 it->getSeqNo(),
akmhoquefdbddb12014-05-02 18:35:19 -050065 m_refreshTime));
akmhoquefdbddb12014-05-02 18:35:19 -050066 }
67 }
akmhoque53353462014-04-22 08:43:45 -050068}
69
70void
akmhoque31d1d4b2014-05-05 22:08:14 -050071Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050072{
73 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050074 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050075 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -050076 if (it != m_table.end()) {
akmhoque53353462014-04-22 08:43:45 -050077 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050078 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -050079 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -050080 //remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -050081 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
82 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050083 }
84 else
85 {
akmhoque157b0a42014-05-13 00:26:37 -050086 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
87 nhit->getConnectingFaceUri()) {
88 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -050089 }
90 }
akmhoque53353462014-04-22 08:43:45 -050091 }
akmhoquefdbddb12014-05-02 18:35:19 -050092 std::cout << "Cancellling Scheduled event" << std::endl;
93 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050094 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -050095 m_table.erase(it);
96 }
97}
98
99
100void
akmhoque31d1d4b2014-05-05 22:08:14 -0500101Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500102{
103 std::cout << "Fib::updateFib Called" << std::endl;
104 int startFace = 0;
105 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500107 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
108 m_table.end(),
109 bind(&fibEntryNameCompare, _1, name));
akmhoque157b0a42014-05-13 00:26:37 -0500110 if (it == m_table.end()) {
111 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500112 nextHopList.sort();
113 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500114 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500115 std::list<NextHop>::iterator nhit = nhl.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500116 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500117 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500118 //Add entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500119 registerPrefix(name, nhit->getConnectingFaceUri(),
120 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500121 }
akmhoquefdbddb12014-05-02 18:35:19 -0500122 newEntry.getNexthopList().sort();
akmhoque53353462014-04-22 08:43:45 -0500123 newEntry.setTimeToRefresh(m_refreshTime);
124 newEntry.setSeqNo(1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, m_refreshTime));
akmhoque53353462014-04-22 08:43:45 -0500126 m_table.push_back(newEntry);
127 }
128 }
akmhoque157b0a42014-05-13 00:26:37 -0500129 else {
akmhoque53353462014-04-22 08:43:45 -0500130 std::cout << "Old FIB Entry" << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500131 if (nextHopList.getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500132 nextHopList.sort();
akmhoque157b0a42014-05-13 00:26:37 -0500133 if (!it->isEqualNextHops(nextHopList)) {
akmhoquefdbddb12014-05-02 18:35:19 -0500134 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500135 std::list<NextHop>::iterator nhit = nhl.begin();
136 // Add first Entry to NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500137 registerPrefix(name, nhit->getConnectingFaceUri(),
138 std::ceil(nhit->getRouteCost()), m_refreshTime);
139 removeHop(it->getNexthopList(), nhit->getConnectingFaceUri(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500140 it->getNexthopList().reset();
141 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500142 ++startFace;
143 ++nhit;
akmhoque157b0a42014-05-13 00:26:37 -0500144 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++) {
akmhoquefdbddb12014-05-02 18:35:19 -0500145 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500146 //Add Entry to NDN_FIB
akmhoque157b0a42014-05-13 00:26:37 -0500147 registerPrefix(name, nhit->getConnectingFaceUri(),
148 std::ceil(nhit->getRouteCost()), m_refreshTime);
akmhoque53353462014-04-22 08:43:45 -0500149 }
150 }
151 it->setTimeToRefresh(m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -0500152 std::cout << "Cancellling Scheduled event" << std::endl;
153 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500154 cancelScheduledExpiringEvent(it->getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500155 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500156 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoque53353462014-04-22 08:43:45 -0500157 it->getSeqNo(), m_refreshTime));
158 }
akmhoque157b0a42014-05-13 00:26:37 -0500159 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500161 }
162 }
163}
164
165
166
167void
akmhoque31d1d4b2014-05-05 22:08:14 -0500168Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500169{
170 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500171 ++it) {
akmhoquefdbddb12014-05-02 18:35:19 -0500172 std::cout << "Cancellling Scheduled event" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() <<
174 std::endl;
175 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500176 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500177 (*it).getNexthopList().getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500178 nhit != (*it).getNexthopList().getNextHops().end(); nhit++) {
akmhoque53353462014-04-22 08:43:45 -0500179 //Remove entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500180 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName())) {
181 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500182 }
akmhoque157b0a42014-05-13 00:26:37 -0500183 else {
184 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFaceUri() !=
185 nhit->getConnectingFaceUri()) {
186 unregisterPrefix(it->getName(), nhit->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500187 }
188 }
akmhoque53353462014-04-22 08:43:45 -0500189 }
190 }
akmhoque157b0a42014-05-13 00:26:37 -0500191 if (m_table.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500192 m_table.clear();
193 }
194}
195
196int
akmhoque31d1d4b2014-05-05 22:08:14 -0500197Fib::getNumberOfFacesForName(NexthopList& nextHopList,
198 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500199{
200 int endFace = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500201 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix)) {
akmhoque53353462014-04-22 08:43:45 -0500202 return nextHopList.getSize();
203 }
akmhoque157b0a42014-05-13 00:26:37 -0500204 else {
akmhoque53353462014-04-22 08:43:45 -0500205 return maxFacesPerPrefix;
206 }
207 return endFace;
208}
209
210void
akmhoque157b0a42014-05-13 00:26:37 -0500211Fib::removeHop(NexthopList& nl, const std::string& doNotRemoveHopFaceUri,
akmhoque31d1d4b2014-05-05 22:08:14 -0500212 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500213{
akmhoquefdbddb12014-05-02 18:35:19 -0500214 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500215 it != nl.getNextHops().end(); ++it) {
216 if (it->getConnectingFaceUri() != doNotRemoveHopFaceUri) {
akmhoque53353462014-04-22 08:43:45 -0500217 //Remove FIB Entry from NDN-FIB
akmhoque157b0a42014-05-13 00:26:37 -0500218 if (!m_nlsr.getAdjacencyList().isNeighbor(name)) {
219 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500220 }
akmhoque157b0a42014-05-13 00:26:37 -0500221 else {
222 if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFaceUri() !=
223 it->getConnectingFaceUri()) {
224 unregisterPrefix(name, it->getConnectingFaceUri());
akmhoquefdbddb12014-05-02 18:35:19 -0500225 }
226 }
akmhoque53353462014-04-22 08:43:45 -0500227 }
228 }
229}
230
231void
akmhoque157b0a42014-05-13 00:26:37 -0500232Fib::registerPrefix(const ndn::Name& namePrefix, const std::string& faceUri,
233 uint64_t faceCost, uint64_t timeout)
234{
235 ndn::nfd::ControlParameters faceParameters;
236 faceParameters
237 .setUri(faceUri);
238
239 m_controller.start<ndn::nfd::FaceCreateCommand>(faceParameters,
240 ndn::bind(&Fib::registerPrefixInNfd, this,_1,
241 namePrefix, faceCost, timeout),
242 ndn::bind(&Fib::onFailure, this, _1, _2,
243 "Failed in name registration"));
244
245}
246
247void
248Fib::registerPrefixInNfd(const ndn::nfd::ControlParameters& faceCreateResult,
249 const ndn::Name& namePrefix, uint64_t faceCost, uint64_t timeout)
akmhoquefdbddb12014-05-02 18:35:19 -0500250{
251 ndn::nfd::ControlParameters controlParameters;
252 controlParameters
akmhoque157b0a42014-05-13 00:26:37 -0500253 .setName(namePrefix)
254 .setFaceId(faceCreateResult.getFaceId())
255 .setCost(faceCost)
256 .setExpirationPeriod(ndn::time::milliseconds(timeout * 1000))
257 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500258 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
akmhoque157b0a42014-05-13 00:26:37 -0500259 ndn::bind(&Fib::onRegistration, this, _1,
260 "Successful in name registration",
261 faceCreateResult.getUri()),
akmhoquefdbddb12014-05-02 18:35:19 -0500262 ndn::bind(&Fib::onFailure, this, _1, _2,
263 "Failed in name registration"));
264}
akmhoque31d1d4b2014-05-05 22:08:14 -0500265
akmhoquefdbddb12014-05-02 18:35:19 -0500266void
akmhoque157b0a42014-05-13 00:26:37 -0500267Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500268{
akmhoque157b0a42014-05-13 00:26:37 -0500269 uint32_t faceId = m_faceMap.getFaceId(faceUri);
270 if (faceId > 0) {
271 ndn::nfd::ControlParameters controlParameters;
272 controlParameters
273 .setName(namePrefix)
274 .setFaceId(faceId)
275 .setOrigin(128);
276 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
akmhoquefdbddb12014-05-02 18:35:19 -0500277 ndn::bind(&Fib::onSuccess, this, _1,
278 "Successful in unregistering name"),
279 ndn::bind(&Fib::onFailure, this, _1, _2,
280 "Failed in unregistering name"));
akmhoque157b0a42014-05-13 00:26:37 -0500281 }
akmhoquefdbddb12014-05-02 18:35:19 -0500282}
283
284void
akmhoque157b0a42014-05-13 00:26:37 -0500285Fib::setStrategy(const ndn::Name& name, const std::string& strategy)
286{
287 ndn::nfd::ControlParameters parameters;
288 parameters
289 .setName(name)
290 .setStrategy(strategy);
291
292 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
293 bind(&Fib::onSuccess, this, _1,
294 "Successfully set strategy choice"),
295 bind(&Fib::onFailure, this, _1, _2,
296 "Failed to set strategy choice"));
297}
298
299void
300Fib::onRegistration(const ndn::nfd::ControlParameters& commandSuccessResult,
301 const std::string& message, const std::string& faceUri)
302{
303 //std::cout << message << ": " << commandSuccessResult << std::endl;
304 m_faceMap.update(faceUri, commandSuccessResult.getFaceId());
305 m_faceMap.print();
306}
307
308
309void
akmhoque31d1d4b2014-05-05 22:08:14 -0500310Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
311 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500312{
akmhoque157b0a42014-05-13 00:26:37 -0500313 //std::cout << message << ": " << commandSuccessResult << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500314}
315
316void
akmhoque31d1d4b2014-05-05 22:08:14 -0500317Fib::onFailure(uint32_t code, const std::string& error,
318 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500319{
320 std::cout << message << ": " << error << " (code: " << code << ")";
321}
322
323
324void
akmhoque53353462014-04-22 08:43:45 -0500325Fib::print()
326{
327 cout << "-------------------FIB-----------------------------" << endl;
328 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500329 ++it) {
akmhoque53353462014-04-22 08:43:45 -0500330 cout << (*it);
331 }
332}
333
334} //namespace nlsr