blob: fc823c1a6850f0ab24ad2ff4053f760081a811c0 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -05002#include <cmath>
akmhoquec8a10f72014-04-25 18:42:55 -05003
4#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05005#include "nexthop-list.hpp"
akmhoquefdbddb12014-05-02 18:35:19 -05006#include "fib.hpp"
7
akmhoquec8a10f72014-04-25 18:42:55 -05008
akmhoque53353462014-04-22 08:43:45 -05009
10
11namespace nlsr {
12
13using namespace std;
14using namespace ndn;
15
16static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050017fibEntryNameCompare(const FibEntry& fibEntry, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050018{
akmhoquefdbddb12014-05-02 18:35:19 -050019 return fibEntry.getName() == name ;
akmhoque53353462014-04-22 08:43:45 -050020}
21
22void
akmhoque31d1d4b2014-05-05 22:08:14 -050023Fib::cancelScheduledExpiringEvent(EventId eid)
akmhoque53353462014-04-22 08:43:45 -050024{
akmhoque31d1d4b2014-05-05 22:08:14 -050025 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050026}
27
28
29ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050030Fib::scheduleEntryRefreshing(const ndn::Name& name, int32_t feSeqNum,
akmhoquefdbddb12014-05-02 18:35:19 -050031 int32_t refreshTime)
akmhoque53353462014-04-22 08:43:45 -050032{
akmhoquefdbddb12014-05-02 18:35:19 -050033 std::cout << "Fib::scheduleEntryRefreshing Called" << std::endl;
34 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050035 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
36 ndn::bind(&Fib::refreshEntry, this,
37 name, feSeqNum));
akmhoque53353462014-04-22 08:43:45 -050038}
39
40void
akmhoque31d1d4b2014-05-05 22:08:14 -050041Fib::refreshEntry(const ndn::Name& name, int32_t feSeqNum)
akmhoque53353462014-04-22 08:43:45 -050042{
akmhoquefdbddb12014-05-02 18:35:19 -050043 std::cout << "Fib::refreshEntry Called" << std::endl;
44 std::cout << "Name: " << name << " Seq Num: " << feSeqNum << std::endl;
45 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
46 m_table.end(),
47 bind(&fibEntryNameCompare, _1, name));
48 if (it != m_table.end())
49 {
50 std::cout << "Entry found with Seq Num: " << feSeqNum << std::endl;
51 if (it->getSeqNo() == feSeqNum)
52 {
53 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();
56 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
akmhoquefdbddb12014-05-02 18:35:19 -050057 {
58 // add entry to NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -050059 registerPrefixInNfd(it->getName(), nhit->getConnectingFace(),
60 std::ceil(nhit->getRouteCost()));
akmhoquefdbddb12014-05-02 18:35:19 -050061 }
akmhoquefdbddb12014-05-02 18:35:19 -050062 // increase sequence number and schedule refresh again
63 it->setSeqNo(feSeqNum + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -050064 it->setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
65 it->getSeqNo(),
akmhoquefdbddb12014-05-02 18:35:19 -050066 m_refreshTime));
akmhoquefdbddb12014-05-02 18:35:19 -050067 }
68 }
akmhoque53353462014-04-22 08:43:45 -050069}
70
71void
akmhoque31d1d4b2014-05-05 22:08:14 -050072Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050073{
74 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquefdbddb12014-05-02 18:35:19 -050075 m_table.end(),
akmhoquec8a10f72014-04-25 18:42:55 -050076 bind(&fibEntryNameCompare, _1, name));
akmhoque53353462014-04-22 08:43:45 -050077 if (it != m_table.end())
78 {
79 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -050080 (*it).getNexthopList().getNextHops().begin();
81 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
akmhoque53353462014-04-22 08:43:45 -050082 {
83 //remove entry from NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -050084 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName()))
akmhoquefdbddb12014-05-02 18:35:19 -050085 {
86 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
87 }
88 else
89 {
akmhoque31d1d4b2014-05-05 22:08:14 -050090 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
91 nhit->getConnectingFace())
akmhoquefdbddb12014-05-02 18:35:19 -050092 {
93 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
94 }
95 }
akmhoque53353462014-04-22 08:43:45 -050096 }
akmhoquefdbddb12014-05-02 18:35:19 -050097 std::cout << "Cancellling Scheduled event" << std::endl;
98 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050099 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500100 m_table.erase(it);
101 }
102}
103
104
105void
akmhoque31d1d4b2014-05-05 22:08:14 -0500106Fib::update(const ndn::Name& name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500107{
108 std::cout << "Fib::updateFib Called" << std::endl;
109 int startFace = 0;
110 int endFace = getNumberOfFacesForName(nextHopList,
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 m_nlsr.getConfParameter().getMaxFacesPerPrefix());
akmhoque53353462014-04-22 08:43:45 -0500112 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
113 m_table.end(),
114 bind(&fibEntryNameCompare, _1, name));
115 if (it == m_table.end())
116 {
117 if (nextHopList.getSize() > 0)
118 {
119 nextHopList.sort();
120 FibEntry newEntry(name);
akmhoquefdbddb12014-05-02 18:35:19 -0500121 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500122 std::list<NextHop>::iterator nhit = nhl.begin();
123 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
124 {
akmhoquefdbddb12014-05-02 18:35:19 -0500125 newEntry.getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500126 //Add entry to NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 registerPrefixInNfd(name, nhit->getConnectingFace(),
128 std::ceil(nhit->getRouteCost()));
akmhoque53353462014-04-22 08:43:45 -0500129 }
akmhoquefdbddb12014-05-02 18:35:19 -0500130 newEntry.getNexthopList().sort();
akmhoque53353462014-04-22 08:43:45 -0500131 newEntry.setTimeToRefresh(m_refreshTime);
132 newEntry.setSeqNo(1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 newEntry.setExpiringEventId(scheduleEntryRefreshing(name , 1, m_refreshTime));
akmhoque53353462014-04-22 08:43:45 -0500134 m_table.push_back(newEntry);
135 }
136 }
137 else
138 {
139 std::cout << "Old FIB Entry" << std::endl;
140 if (nextHopList.getSize() > 0)
141 {
142 nextHopList.sort();
143 if (!it->isEqualNextHops(nextHopList))
144 {
akmhoquefdbddb12014-05-02 18:35:19 -0500145 std::list<NextHop> nhl = nextHopList.getNextHops();
akmhoque53353462014-04-22 08:43:45 -0500146 std::list<NextHop>::iterator nhit = nhl.begin();
147 // Add first Entry to NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 registerPrefixInNfd(name, nhit->getConnectingFace(),
149 std::ceil(nhit->getRouteCost()));
150 removeHop(it->getNexthopList(), nhit->getConnectingFace(), name);
akmhoquefdbddb12014-05-02 18:35:19 -0500151 it->getNexthopList().reset();
152 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500153 ++startFace;
154 ++nhit;
155 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
156 {
akmhoquefdbddb12014-05-02 18:35:19 -0500157 it->getNexthopList().addNextHop((*nhit));
akmhoque53353462014-04-22 08:43:45 -0500158 //Add Entry to NDN_FIB
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 registerPrefixInNfd(name, nhit->getConnectingFace(),
160 std::ceil(nhit->getRouteCost()));
akmhoque53353462014-04-22 08:43:45 -0500161 }
162 }
163 it->setTimeToRefresh(m_refreshTime);
akmhoquefdbddb12014-05-02 18:35:19 -0500164 std::cout << "Cancellling Scheduled event" << std::endl;
165 std::cout << "Name: " << name << "Seq num: " << it->getSeqNo() << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 cancelScheduledExpiringEvent(it->getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500167 it->setSeqNo(it->getSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500168 (*it).setExpiringEventId(scheduleEntryRefreshing(it->getName() ,
akmhoque53353462014-04-22 08:43:45 -0500169 it->getSeqNo(), m_refreshTime));
170 }
171 else
172 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500173 remove(name);
akmhoque53353462014-04-22 08:43:45 -0500174 }
175 }
176}
177
178
179
180void
akmhoque31d1d4b2014-05-05 22:08:14 -0500181Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500182{
183 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
184 ++it)
185 {
akmhoquefdbddb12014-05-02 18:35:19 -0500186 std::cout << "Cancellling Scheduled event" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500187 std::cout << "Name: " << it->getName() << "Seq num: " << it->getSeqNo() <<
188 std::endl;
189 cancelScheduledExpiringEvent((*it).getExpiringEventId());
akmhoque53353462014-04-22 08:43:45 -0500190 for (std::list<NextHop>::iterator nhit =
akmhoquefdbddb12014-05-02 18:35:19 -0500191 (*it).getNexthopList().getNextHops().begin();
192 nhit != (*it).getNexthopList().getNextHops().end(); nhit++)
akmhoque53353462014-04-22 08:43:45 -0500193 {
akmhoque53353462014-04-22 08:43:45 -0500194 //Remove entry from NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 if (!m_nlsr.getAdjacencyList().isNeighbor(it->getName()))
akmhoquefdbddb12014-05-02 18:35:19 -0500196 {
197 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
198 }
199 else
200 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500201 if (m_nlsr.getAdjacencyList().getAdjacent(it->getName()).getConnectingFace() !=
202 nhit->getConnectingFace())
akmhoquefdbddb12014-05-02 18:35:19 -0500203 {
204 unregisterPrefixFromNfd(it->getName(), nhit->getConnectingFace());
205 }
206 }
akmhoque53353462014-04-22 08:43:45 -0500207 }
208 }
209 if (m_table.size() > 0)
210 {
211 m_table.clear();
212 }
213}
214
215int
akmhoque31d1d4b2014-05-05 22:08:14 -0500216Fib::getNumberOfFacesForName(NexthopList& nextHopList,
217 uint32_t maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500218{
219 int endFace = 0;
220 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix))
221 {
222 return nextHopList.getSize();
223 }
224 else
225 {
226 return maxFacesPerPrefix;
227 }
228 return endFace;
229}
230
231void
akmhoque31d1d4b2014-05-05 22:08:14 -0500232Fib::removeHop(NexthopList& nl, uint32_t doNotRemoveHopFaceId,
233 const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500234{
akmhoquefdbddb12014-05-02 18:35:19 -0500235 for (std::list<NextHop>::iterator it = nl.getNextHops().begin();
236 it != nl.getNextHops().end(); ++it)
akmhoque53353462014-04-22 08:43:45 -0500237 {
238 if (it->getConnectingFace() != doNotRemoveHopFaceId)
239 {
240 //Remove FIB Entry from NDN-FIB
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 if (!m_nlsr.getAdjacencyList().isNeighbor(name))
akmhoquefdbddb12014-05-02 18:35:19 -0500242 {
243 unregisterPrefixFromNfd(name, it->getConnectingFace());
244 }
245 else
246 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500247 if (m_nlsr.getAdjacencyList().getAdjacent(name).getConnectingFace() !=
248 it->getConnectingFace())
akmhoquefdbddb12014-05-02 18:35:19 -0500249 {
250 unregisterPrefixFromNfd(name, it->getConnectingFace());
251 }
252 }
akmhoque53353462014-04-22 08:43:45 -0500253 }
254 }
255}
256
257void
akmhoque31d1d4b2014-05-05 22:08:14 -0500258Fib::registerPrefixInNfd(const ndn::Name& namePrefix, uint64_t faceId,
259 uint64_t faceCost)
akmhoquefdbddb12014-05-02 18:35:19 -0500260{
261 ndn::nfd::ControlParameters controlParameters;
262 controlParameters
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 .setName(namePrefix)
264 .setCost(faceCost)
265 .setFaceId(faceId)
266 .setExpirationPeriod(ndn::time::milliseconds(m_refreshTime * 1000))
267 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500268 m_controller.start<ndn::nfd::RibRegisterCommand>(controlParameters,
269 ndn::bind(&Fib::onSuccess, this, _1,
270 "Successful in name registration"),
271 ndn::bind(&Fib::onFailure, this, _1, _2,
272 "Failed in name registration"));
273}
akmhoque31d1d4b2014-05-05 22:08:14 -0500274
akmhoquefdbddb12014-05-02 18:35:19 -0500275void
akmhoque31d1d4b2014-05-05 22:08:14 -0500276Fib::unregisterPrefixFromNfd(const ndn::Name& namePrefix, uint64_t faceId)
akmhoquefdbddb12014-05-02 18:35:19 -0500277{
278 ndn::nfd::ControlParameters controlParameters;
279 controlParameters
akmhoque31d1d4b2014-05-05 22:08:14 -0500280 .setName(namePrefix)
281 .setFaceId(faceId)
282 .setOrigin(128);
akmhoquefdbddb12014-05-02 18:35:19 -0500283 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
284 ndn::bind(&Fib::onSuccess, this, _1,
285 "Successful in unregistering name"),
286 ndn::bind(&Fib::onFailure, this, _1, _2,
287 "Failed in unregistering name"));
288}
289
290void
akmhoque31d1d4b2014-05-05 22:08:14 -0500291Fib::onSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
292 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500293{
294 std::cout << message << ": " << commandSuccessResult << std::endl;
295}
296
297void
akmhoque31d1d4b2014-05-05 22:08:14 -0500298Fib::onFailure(uint32_t code, const std::string& error,
299 const std::string& message)
akmhoquefdbddb12014-05-02 18:35:19 -0500300{
301 std::cout << message << ": " << error << " (code: " << code << ")";
302}
303
304
305void
akmhoque53353462014-04-22 08:43:45 -0500306Fib::print()
307{
308 cout << "-------------------FIB-----------------------------" << endl;
309 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
310 ++it)
311 {
312 cout << (*it);
313 }
314}
315
316} //namespace nlsr