akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame^] | 1 | #include<string> |
| 2 | #include "nlsr_lsdb.hpp" |
| 3 | #include "nlsr.hpp" |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | bool |
| 12 | Lsdb::doesLsaExist(string key, int lsType) |
| 13 | { |
| 14 | if ( lsType == 1) |
| 15 | { |
| 16 | return doesNameLsaExist(key); |
| 17 | } |
| 18 | else if ( lsType == 2) |
| 19 | { |
| 20 | return doesAdjLsaExist(key); |
| 21 | } |
| 22 | else if ( lsType == 3) |
| 23 | { |
| 24 | return doesCorLsaExist(key); |
| 25 | } |
| 26 | |
| 27 | return false; |
| 28 | |
| 29 | } |
| 30 | |
| 31 | static bool |
| 32 | nameLsaCompare(NameLsa& nlsa1, NameLsa& nlsa2){ |
| 33 | return nlsa1.getLsaKey()==nlsa1.getLsaKey(); |
| 34 | } |
| 35 | |
| 36 | static bool |
| 37 | nameLsaCompareByKey(NameLsa& nlsa1, string& key){ |
| 38 | return nlsa1.getLsaKey()==key; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | bool |
| 43 | Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr) |
| 44 | { |
| 45 | NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 46 | , 1 |
| 47 | , pnlsr.getNameLsaSeq()+1 |
| 48 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 49 | , pnlsr.getNpl() ); |
| 50 | pnlsr.setNameLsaSeq(pnlsr.getNameLsaSeq()+1); |
| 51 | cout<<nameLsa; |
| 52 | return installNameLsa(nameLsa); |
| 53 | |
| 54 | } |
| 55 | |
| 56 | NameLsa& |
| 57 | Lsdb::getNameLsa(string key) |
| 58 | { |
| 59 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 60 | nameLsdb.end(), |
| 61 | bind(nameLsaCompareByKey, _1, key)); |
| 62 | |
| 63 | if( it != nameLsdb.end()){ |
| 64 | return (*it); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | bool |
| 71 | Lsdb::installNameLsa(NameLsa &nlsa) |
| 72 | { |
| 73 | bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey()); |
| 74 | if ( !doesLsaExist_ ) |
| 75 | { |
| 76 | // add name LSA |
| 77 | addNameLsa(nlsa); |
| 78 | // update NPT and FIB |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | // check for newer name LSA |
| 83 | NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey()); |
| 84 | // Discard or Update Name lsa, NPT, FIB |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool |
| 91 | Lsdb::addNameLsa(NameLsa &nlsa) |
| 92 | { |
| 93 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 94 | nameLsdb.end(), |
| 95 | bind(nameLsaCompare, _1, nlsa)); |
| 96 | |
| 97 | if( it == nameLsdb.end()){ |
| 98 | nameLsdb.push_back(nlsa); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | bool |
| 105 | Lsdb::removeNameLsa(string& key) |
| 106 | { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | Lsdb::printNameLsdb() |
| 112 | { |
| 113 | for( std::list<NameLsa>::iterator it=nameLsdb.begin(); |
| 114 | it!= nameLsdb.end() ; it++) |
| 115 | { |
| 116 | cout<< (*it) <<endl; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | bool |
| 121 | Lsdb::doesNameLsaExist(string key) |
| 122 | { |
| 123 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 124 | nameLsdb.end(), |
| 125 | bind(nameLsaCompareByKey, _1, key)); |
| 126 | |
| 127 | if( it == nameLsdb.end()){ |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | bool |
| 136 | Lsdb::doesAdjLsaExist(string key) |
| 137 | { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool |
| 142 | Lsdb::doesCorLsaExist(string key) |
| 143 | { |
| 144 | return false; |
| 145 | } |