blob: 1d7582baa794f0c40479078a4081b5dbb769e91b [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
akmhoquec8a10f72014-04-25 18:42:55 -05002
3#include "nlsr.hpp"
akmhoque53353462014-04-22 08:43:45 -05004#include "fib-entry.hpp"
5#include "fib.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05006#include "nexthop-list.hpp"
7
akmhoque53353462014-04-22 08:43:45 -05008
9
10namespace nlsr {
11
12using namespace std;
13using namespace ndn;
14
15static bool
16fibEntryNameCompare(FibEntry& fe, string name)
17{
18 return fe.getName() == name ;
19}
20
21void
22Fib::cancelScheduledExpiringEvent(Nlsr& pnlsr, EventId eid)
23{
24 pnlsr.getScheduler().cancelEvent(eid);
25}
26
27
28ndn::EventId
29Fib::scheduleEntryRefreshing(Nlsr& pnlsr, string name, int feSeqNum,
30 int refreshTime)
31{
32 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(refreshTime),
33 ndn::bind(&Fib::refreshEntry, this, name, feSeqNum));
34}
35
36void
37Fib::refreshEntry(string name, int feSeqNum)
38{
39}
40
41void
42Fib::remove(Nlsr& pnlsr, string name)
43{
44 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
akmhoquec8a10f72014-04-25 18:42:55 -050045 m_table.end(),
46 bind(&fibEntryNameCompare, _1, name));
akmhoque53353462014-04-22 08:43:45 -050047 if (it != m_table.end())
48 {
49 for (std::list<NextHop>::iterator nhit =
50 (*it).getNhl().getNextHopList().begin();
51 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
52 {
53 //remove entry from NDN-FIB
54 }
55 cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId());
56 m_table.erase(it);
57 }
58}
59
60
61void
akmhoquec8a10f72014-04-25 18:42:55 -050062Fib::update(Nlsr& pnlsr, string name, NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -050063{
64 std::cout << "Fib::updateFib Called" << std::endl;
65 int startFace = 0;
66 int endFace = getNumberOfFacesForName(nextHopList,
67 pnlsr.getConfParameter().getMaxFacesPerPrefix());
68 std::list<FibEntry>::iterator it = std::find_if(m_table.begin(),
69 m_table.end(),
70 bind(&fibEntryNameCompare, _1, name));
71 if (it == m_table.end())
72 {
73 if (nextHopList.getSize() > 0)
74 {
75 nextHopList.sort();
76 FibEntry newEntry(name);
77 std::list<NextHop> nhl = nextHopList.getNextHopList();
78 std::list<NextHop>::iterator nhit = nhl.begin();
79 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
80 {
81 newEntry.getNhl().addNextHop((*nhit));
82 //Add entry to NDN-FIB
83 }
84 newEntry.getNhl().sort();
85 newEntry.setTimeToRefresh(m_refreshTime);
86 newEntry.setSeqNo(1);
87 newEntry.setExpiringEventId(scheduleEntryRefreshing(pnlsr,
88 name , 1, m_refreshTime));
89 m_table.push_back(newEntry);
90 }
91 }
92 else
93 {
94 std::cout << "Old FIB Entry" << std::endl;
95 if (nextHopList.getSize() > 0)
96 {
97 nextHopList.sort();
98 if (!it->isEqualNextHops(nextHopList))
99 {
100 std::list<NextHop> nhl = nextHopList.getNextHopList();
101 std::list<NextHop>::iterator nhit = nhl.begin();
102 // Add first Entry to NDN-FIB
103 removeHop(pnlsr, it->getNhl(), nhit->getConnectingFace());
104 it->getNhl().reset();
105 it->getNhl().addNextHop((*nhit));
106 ++startFace;
107 ++nhit;
108 for (int i = startFace; i < endFace && nhit != nhl.end(); ++nhit, i++)
109 {
110 it->getNhl().addNextHop((*nhit));
111 //Add Entry to NDN_FIB
112 }
113 }
114 it->setTimeToRefresh(m_refreshTime);
115 cancelScheduledExpiringEvent(pnlsr, it->getExpiringEventId());
116 it->setSeqNo(it->getSeqNo() + 1);
117 (*it).setExpiringEventId(scheduleEntryRefreshing(pnlsr,
118 it->getName() ,
119 it->getSeqNo(), m_refreshTime));
120 }
121 else
122 {
123 remove(pnlsr, name);
124 }
125 }
126}
127
128
129
130void
131Fib::clean(Nlsr& pnlsr)
132{
133 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
134 ++it)
135 {
136 for (std::list<NextHop>::iterator nhit =
137 (*it).getNhl().getNextHopList().begin();
138 nhit != (*it).getNhl().getNextHopList().begin(); nhit++)
139 {
140 cancelScheduledExpiringEvent(pnlsr, (*it).getExpiringEventId());
141 //Remove entry from NDN-FIB
142 }
143 }
144 if (m_table.size() > 0)
145 {
146 m_table.clear();
147 }
148}
149
150int
akmhoquec8a10f72014-04-25 18:42:55 -0500151Fib::getNumberOfFacesForName(NexthopList& nextHopList, int maxFacesPerPrefix)
akmhoque53353462014-04-22 08:43:45 -0500152{
153 int endFace = 0;
154 if ((maxFacesPerPrefix == 0) || (nextHopList.getSize() <= maxFacesPerPrefix))
155 {
156 return nextHopList.getSize();
157 }
158 else
159 {
160 return maxFacesPerPrefix;
161 }
162 return endFace;
163}
164
165void
akmhoquec8a10f72014-04-25 18:42:55 -0500166Fib::removeHop(Nlsr& pnlsr, NexthopList& nl, int doNotRemoveHopFaceId)
akmhoque53353462014-04-22 08:43:45 -0500167{
168 for (std::list<NextHop>::iterator it = nl.getNextHopList().begin();
169 it != nl.getNextHopList().end(); ++it)
170 {
171 if (it->getConnectingFace() != doNotRemoveHopFaceId)
172 {
173 //Remove FIB Entry from NDN-FIB
174 }
175 }
176}
177
178void
179Fib::print()
180{
181 cout << "-------------------FIB-----------------------------" << endl;
182 for (std::list<FibEntry>::iterator it = m_table.begin(); it != m_table.end();
183 ++it)
184 {
185 cout << (*it);
186 }
187}
188
189} //namespace nlsr