akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame] | 1 | #include<list> |
| 2 | #include "nlsr_fe.hpp" |
| 3 | #include "nlsr_fib.hpp" |
| 4 | #include "nlsr_nhl.hpp" |
| 5 | |
| 6 | using namespace std; |
| 7 | |
| 8 | static bool |
| 9 | fibEntryNameCompare(FibEntry& fe, string name) |
| 10 | { |
| 11 | return fe.getName() == name ; |
| 12 | } |
| 13 | |
| 14 | |
| 15 | |
| 16 | void |
| 17 | Fib::removeFromFib(string name) |
| 18 | { |
| 19 | std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(), |
| 20 | fibTable.end(), bind(&fibEntryNameCompare, _1, name)); |
| 21 | if( it != fibTable.end() ) |
| 22 | { |
| 23 | for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin(); |
| 24 | nhit != (*it).getNhl().getNextHopList().begin(); nhit++) |
| 25 | { |
| 26 | //remove entry from NDN-FIB |
| 27 | } |
| 28 | fibTable.erase(it); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | If NHL is equal for current FIB and NPT then to change |
| 34 | Otherwise |
| 35 | Add the first Nexthop to FIB |
| 36 | remove all old nexthop from FIB |
| 37 | And add all other Nexthop to FIB |
| 38 | */ |
| 39 | |
| 40 | void |
| 41 | Fib::updateFib(string name, Nhl& nextHopList, int maxFacesPerPrefix) |
| 42 | { |
| 43 | int startFace=0; |
| 44 | int endFace=getNumberOfFacesForName(nextHopList,maxFacesPerPrefix); |
| 45 | std::list<FibEntry >::iterator it = std::find_if( fibTable.begin(), |
| 46 | fibTable.end(), bind(&fibEntryNameCompare, _1, name)); |
| 47 | if( it != fibTable.end() ) |
| 48 | { |
| 49 | if ( !(*it).isEqualNextHops(nextHopList) ) |
| 50 | { |
| 51 | std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin(); |
| 52 | (*it).getNhl().addNextHop((*nhit)); |
| 53 | removeFibEntryHop((*it).getNhl(),(*nhit).getConnectingFace()); |
| 54 | startFace++; |
| 55 | nhit++; |
| 56 | for( int i=startFace;i< endFace;nhit++,i++) |
| 57 | { |
| 58 | (*it).getNhl().addNextHop((*nhit)); |
| 59 | } |
| 60 | } |
akmhoque | 4768f89 | 2014-02-08 23:58:07 -0600 | [diff] [blame^] | 61 | (*it).getNhl().sortNhl(); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame] | 62 | } |
| 63 | else |
| 64 | { |
| 65 | FibEntry newEntry(name); |
| 66 | for(std::list<NextHop>::iterator nhit=nextHopList.getNextHopList().begin(); |
| 67 | nhit!=nextHopList.getNextHopList().end();++nhit) |
| 68 | { |
| 69 | newEntry.getNhl().addNextHop((*nhit)); |
| 70 | } |
akmhoque | 4768f89 | 2014-02-08 23:58:07 -0600 | [diff] [blame^] | 71 | newEntry.getNhl().sortNhl(); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame] | 72 | fibTable.push_back(newEntry); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void Fib::cleanFib() |
| 77 | { |
| 78 | for( std::list<FibEntry >::iterator it=fibTable.begin(); it != fibTable.end(); |
| 79 | ++it) |
| 80 | { |
| 81 | for(std::list<NextHop>::iterator nhit=(*it).getNhl().getNextHopList().begin(); |
| 82 | nhit != (*it).getNhl().getNextHopList().begin(); nhit++) |
| 83 | { |
| 84 | //remove entry from NDN-FIB |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if ( fibTable.size() > 0 ) |
| 89 | { |
| 90 | fibTable.clear(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void |
| 96 | Fib::removeFibEntryHop(Nhl& nl, int doNotRemoveHop) |
| 97 | { |
| 98 | for( std::list<NextHop >::iterator it=nl.getNextHopList().begin(); |
| 99 | it != nl.getNextHopList().end(); ++it) |
| 100 | { |
| 101 | if ( (*it).getConnectingFace() != doNotRemoveHop ) |
| 102 | { |
| 103 | nl.getNextHopList().erase(it); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | int |
| 110 | Fib::getNumberOfFacesForName(Nhl& nextHopList, int maxFacesPerPrefix) |
| 111 | { |
| 112 | int endFace=0; |
| 113 | if((maxFacesPerPrefix == 0) || (nextHopList.getNhlSize() <= maxFacesPerPrefix)) |
| 114 | { |
| 115 | return nextHopList.getNhlSize(); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | return maxFacesPerPrefix; |
| 120 | } |
| 121 | |
| 122 | return endFace; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Fib::printFib() |
| 127 | { |
| 128 | cout<<"-------------------FIB-----------------------------"<<endl; |
| 129 | for(std::list<FibEntry>::iterator it = fibTable.begin(); it!=fibTable.end(); |
| 130 | ++it) |
| 131 | { |
akmhoque | 4768f89 | 2014-02-08 23:58:07 -0600 | [diff] [blame^] | 132 | //(*it).getNhl().sortNhl(); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame] | 133 | cout<<(*it); |
| 134 | } |
| 135 | } |