akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame^] | 1 | #include<iostream> |
| 2 | #include<fstream> |
| 3 | #include<string> |
| 4 | #include<cstdlib> |
| 5 | #include <sstream> |
| 6 | |
| 7 | #include "conf_processor.hpp" |
| 8 | #include "conf_param.hpp" |
| 9 | #include "nlsr_tokenizer.hpp" |
| 10 | #include "adjacent.hpp" |
| 11 | |
| 12 | |
| 13 | using namespace std; |
| 14 | |
| 15 | int |
| 16 | ConfFileProcessor::processConfFile(nlsr& pnlsr){ |
| 17 | int ret=0; |
| 18 | |
| 19 | if ( !confFileName.empty()){ |
| 20 | std::ifstream inputFile(confFileName.c_str()); |
| 21 | for( string line; getline( inputFile, line ); ){ |
| 22 | if (!line.empty() ){ |
| 23 | if(line[0]!= '#' && line[0]!='!'){ |
| 24 | ret=processConfCommand(pnlsr, line); |
| 25 | if( ret == -1 ){ |
| 26 | break; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | int |
| 38 | ConfFileProcessor::processConfCommand(nlsr& pnlsr, string command){ |
| 39 | int ret=0; |
| 40 | nlsrTokenizer nt(command," "); |
| 41 | if( (nt.getFirstToken() == "network")){ |
| 42 | ret=processConfCommandNetwork(pnlsr,nt.getRestOfLine()); |
| 43 | } |
| 44 | else if( (nt.getFirstToken() == "site-name")){ |
| 45 | ret=processConfCommandSiteName(pnlsr,nt.getRestOfLine()); |
| 46 | } |
| 47 | else if ( (nt.getFirstToken() == "router-name")){ |
| 48 | ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine()); |
| 49 | } |
| 50 | else if( (nt.getFirstToken() == "ndnneighbor") ){ |
| 51 | ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine()); |
| 52 | } |
| 53 | else if( (nt.getFirstToken() == "link-cost")){ |
| 54 | ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine()); |
| 55 | } |
| 56 | else if( (nt.getFirstToken() == "ndnname") ){ |
| 57 | ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine()); |
| 58 | } |
| 59 | else if( (nt.getFirstToken() == "interest-retry-num")){ |
| 60 | processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine()); |
| 61 | } |
| 62 | else if( (nt.getFirstToken() == "interest-resend-time")){ |
| 63 | processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine()); |
| 64 | } |
| 65 | else if( (nt.getFirstToken() == "lsa-refresh-time")){ |
| 66 | processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine()); |
| 67 | } |
| 68 | else if( (nt.getFirstToken() == "max-faces-per-prefix")){ |
| 69 | processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine()); |
| 70 | } |
| 71 | else if( (nt.getFirstToken() == "logdir")){ |
| 72 | processConfCommandLogDir(pnlsr,nt.getRestOfLine()); |
| 73 | } |
| 74 | else if( (nt.getFirstToken() == "detailed-logging") ){ |
| 75 | processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine()); |
| 76 | } |
| 77 | else if( (nt.getFirstToken() == "debugging") ){ |
| 78 | processConfCommandDebugging(pnlsr,nt.getRestOfLine()); |
| 79 | } |
| 80 | else if( (nt.getFirstToken() == "chronosync-sync-prefix") ){ |
| 81 | processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine()); |
| 82 | } |
| 83 | else if( (nt.getFirstToken() == "hyperbolic-cordinate") ){ |
| 84 | processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine()); |
| 85 | } |
| 86 | else if( (nt.getFirstToken() == "hyperbolic-routing")){ |
| 87 | processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine()); |
| 88 | } |
| 89 | else if( (nt.getFirstToken() == "tunnel-type")){ |
| 90 | processConfCommandTunnelType(pnlsr,nt.getRestOfLine()); |
| 91 | } |
| 92 | else { |
| 93 | cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl; |
| 94 | } |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | ConfFileProcessor::processConfCommandNetwork(nlsr& pnlsr, string command){ |
| 101 | if(command.empty() ){ |
| 102 | cerr <<" Network can not be null or empty :( !"<<endl; |
| 103 | return -1; |
| 104 | }else{ |
| 105 | if(command[command.size()-1] == '/' ){ |
| 106 | command.erase(command.size() - 1); |
| 107 | } |
| 108 | if(command[0] == '/' ){ |
| 109 | command.erase(0,1); |
| 110 | } |
| 111 | pnlsr.confParam.setNetwork(command); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | ConfFileProcessor::processConfCommandSiteName(nlsr& pnlsr, string command){ |
| 118 | if(command.empty() ){ |
| 119 | cerr <<"Site name can not be null or empty :( !"<<endl; |
| 120 | return -1; |
| 121 | }else{ |
| 122 | if(command[command.size()-1] == '/' ){ |
| 123 | command.erase(command.size() - 1); |
| 124 | } |
| 125 | if(command[0] == '/' ){ |
| 126 | command.erase(0,1); |
| 127 | } |
| 128 | pnlsr.confParam.setSiteName(command); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int |
| 134 | ConfFileProcessor::processConfCommandRouterName(nlsr& pnlsr, string command){ |
| 135 | if(command.empty() ){ |
| 136 | cerr <<" Router name can not be null or empty :( !"<<endl; |
| 137 | return -1; |
| 138 | }else{ |
| 139 | if(command[command.size()-1] == '/' ){ |
| 140 | command.erase(command.size() - 1); |
| 141 | } |
| 142 | if(command[0] == '/' ){ |
| 143 | command.erase(0,1); |
| 144 | } |
| 145 | pnlsr.confParam.setRouterName(command); |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int |
| 151 | ConfFileProcessor::processConfCommandInterestRetryNumber(nlsr& pnlsr, string command){ |
| 152 | if(command.empty() ){ |
| 153 | cerr <<" Wrong command format ! [interest-retry-num n]"<<endl; |
| 154 | }else{ |
| 155 | int irn; |
| 156 | stringstream ss(command.c_str()); |
| 157 | ss>>irn; |
| 158 | if ( irn >=1 && irn <=5){ |
| 159 | pnlsr.confParam.setInterestRetryNumber(irn); |
| 160 | } |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int |
| 166 | ConfFileProcessor::processConfCommandInterestResendTime(nlsr& pnlsr, string command){ |
| 167 | if(command.empty() ){ |
| 168 | cerr <<" Wrong command format ! [interest-resend-time s]"<<endl; |
| 169 | }else{ |
| 170 | int irt; |
| 171 | stringstream ss(command.c_str()); |
| 172 | ss>>irt; |
| 173 | if( irt>=1 && irt <=20){ |
| 174 | pnlsr.confParam.setInterestResendTime(irt); |
| 175 | } |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | int |
| 181 | ConfFileProcessor::processConfCommandLsaRefreshTime(nlsr& pnlsr, string command){ |
| 182 | if(command.empty() ){ |
| 183 | cerr <<" Wrong command format ! [interest-resend-time s]"<<endl; |
| 184 | }else{ |
| 185 | int lrt; |
| 186 | stringstream ss(command.c_str()); |
| 187 | ss>>lrt; |
| 188 | if ( lrt>= 240 && lrt<=7200){ |
| 189 | pnlsr.confParam.setLsaRefreshTime(lrt); |
| 190 | } |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int |
| 196 | ConfFileProcessor::processConfCommandMaxFacesPerPrefix(nlsr& pnlsr, string command){ |
| 197 | if(command.empty() ){ |
| 198 | cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl; |
| 199 | }else{ |
| 200 | int mfpp; |
| 201 | stringstream ss(command.c_str()); |
| 202 | ss>>mfpp; |
| 203 | if ( mfpp>=0 && mfpp<=60){ |
| 204 | pnlsr.confParam.setMaxFacesPerPrefix(mfpp); |
| 205 | } |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int |
| 211 | ConfFileProcessor::processConfCommandTunnelType(nlsr& pnlsr, string command){ |
| 212 | if(command.empty() ){ |
| 213 | cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl; |
| 214 | }else{ |
| 215 | if(command == "tcp" || command == "TCP" ){ |
| 216 | pnlsr.confParam.setTunnelType(1); |
| 217 | } |
| 218 | else if(command == "udp" || command == "UDP"){ |
| 219 | pnlsr.confParam.setTunnelType(0); |
| 220 | }else{ |
| 221 | cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl; |
| 222 | } |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int |
| 228 | ConfFileProcessor::processConfCommandChronosyncSyncPrefix(nlsr& pnlsr, |
| 229 | string command){ |
| 230 | if(command.empty() ){ |
| 231 | cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl; |
| 232 | }else{ |
| 233 | pnlsr.confParam.setChronosyncSyncPrefix(command); |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | int |
| 240 | ConfFileProcessor::processConfCommandLogDir(nlsr& pnlsr, string command){ |
| 241 | if(command.empty() ){ |
| 242 | cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl; |
| 243 | }else{ |
| 244 | pnlsr.confParam.setLogDir(command); |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int |
| 250 | ConfFileProcessor::processConfCommandDebugging(nlsr& pnlsr, string command){ |
| 251 | if(command.empty() ){ |
| 252 | cerr <<" Wrong command format ! [debugging on/of]!"<<endl; |
| 253 | }else{ |
| 254 | if(command == "on" || command == "ON" ){ |
| 255 | pnlsr.confParam.setDebugging(1); |
| 256 | } |
| 257 | else if(command == "off" || command == "off"){ |
| 258 | pnlsr.confParam.setDebugging(0); |
| 259 | }else{ |
| 260 | cerr <<" Wrong command format ! [debugging on/off]!"<<endl; |
| 261 | } |
| 262 | } |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | int |
| 267 | ConfFileProcessor::processConfCommandDetailedLogging(nlsr& pnlsr, string command){ |
| 268 | if(command.empty() ){ |
| 269 | cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl; |
| 270 | }else{ |
| 271 | if(command == "on" || command == "ON" ){ |
| 272 | pnlsr.confParam.setDetailedLogging(1); |
| 273 | } |
| 274 | else if(command == "off" || command == "off"){ |
| 275 | pnlsr.confParam.setDetailedLogging(0); |
| 276 | }else{ |
| 277 | cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl; |
| 278 | } |
| 279 | } |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int |
| 284 | ConfFileProcessor::processConfCommandIsHyperbolicCalc(nlsr& pnlsr, string command){ |
| 285 | if(command.empty() ){ |
| 286 | cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl; |
| 287 | }else{ |
| 288 | if(command == "on" || command == "ON" ){ |
| 289 | pnlsr.confParam.setIsHyperbolicCalc(1); |
| 290 | } |
| 291 | else if(command == "dry-run" || command == "DRY-RUN"){ |
| 292 | pnlsr.confParam.setIsHyperbolicCalc(2); |
| 293 | } |
| 294 | else if(command == "off" || command == "off"){ |
| 295 | pnlsr.confParam.setIsHyperbolicCalc(0); |
| 296 | }else{ |
| 297 | cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl; |
| 298 | } |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | ConfFileProcessor::processConfCommandHyperbolicCordinate(nlsr& pnlsr, string command){ |
| 305 | if(command.empty() ){ |
| 306 | cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl; |
| 307 | if (pnlsr.confParam.getIsHyperbolicCalc() > 0 ){ |
| 308 | return -1; |
| 309 | } |
| 310 | }else{ |
| 311 | nlsrTokenizer nt(command," "); |
| 312 | stringstream ssr(nt.getFirstToken().c_str()); |
| 313 | stringstream sst(nt.getRestOfLine().c_str()); |
| 314 | |
| 315 | double r,theta; |
| 316 | ssr>>r; |
| 317 | sst>>theta; |
| 318 | |
| 319 | pnlsr.confParam.setCorR(r); |
| 320 | pnlsr.confParam.setCorTheta(theta); |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | int |
| 327 | ConfFileProcessor::processConfCommandNdnNeighbor(nlsr& pnlsr, string command){ |
| 328 | if(command.empty() ){ |
| 329 | cerr <<" Wrong command format ! [ndnneighbor /nbr/name/]!"<<endl; |
| 330 | }else{ |
| 331 | nlsrTokenizer nt(command," "); |
| 332 | Adjacent adj(nt.getFirstToken(),0,0.0,0,0); |
| 333 | pnlsr.adl.insert(adj); |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int |
| 339 | ConfFileProcessor::processConfCommandNdnName(nlsr& pnlsr, string command){ |
| 340 | if(command.empty() ){ |
| 341 | cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl; |
| 342 | }else{ |
| 343 | pnlsr.npl.insertIntoNpl(command); |
| 344 | } |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | int |
| 350 | ConfFileProcessor::processConfCommandLinkCost(nlsr& pnlsr, string command){ |
| 351 | if(command.empty() ){ |
| 352 | cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl; |
| 353 | if (pnlsr.confParam.getIsHyperbolicCalc() > 0 ){ |
| 354 | return -1; |
| 355 | } |
| 356 | }else{ |
| 357 | nlsrTokenizer nt(command," "); |
| 358 | stringstream sst(nt.getRestOfLine().c_str()); |
| 359 | |
| 360 | double cost; |
| 361 | sst>>cost; |
| 362 | |
| 363 | pnlsr.adl.updateAdjacentLinkCost(nt.getFirstToken(),cost); |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |