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