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 | |
akmhoque | 204e754 | 2014-01-31 16:08:25 -0600 | [diff] [blame] | 7 | #include "nlsr_conf_processor.hpp" |
| 8 | #include "nlsr_conf_param.hpp" |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 9 | #include "nlsr_tokenizer.hpp" |
akmhoque | 204e754 | 2014-01-31 16:08:25 -0600 | [diff] [blame] | 10 | #include "nlsr_adjacent.hpp" |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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()); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame^] | 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 | } |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | } |
| 32 | } |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame^] | 33 | else{ |
| 34 | std::cerr <<"Configuration file: ("<<confFileName<<") does not exist :("; |
| 35 | std::cerr <<endl; |
| 36 | ret=-1; |
| 37 | } |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 118 | pnlsr.getConfParameter().setNetwork(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 135 | pnlsr.getConfParameter().setSiteName(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 152 | pnlsr.getConfParameter().setRouterName(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 166 | pnlsr.getConfParameter().setInterestRetryNumber(irn); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 181 | pnlsr.getConfParameter().setInterestResendTime(irt); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 196 | pnlsr.getConfParameter().setLsaRefreshTime(lrt); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 211 | pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 223 | pnlsr.getConfParameter().setTunnelType(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 224 | } |
| 225 | else if(command == "udp" || command == "UDP"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 226 | pnlsr.getConfParameter().setTunnelType(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 240 | pnlsr.getConfParameter().setChronosyncSyncPrefix(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 251 | pnlsr.getConfParameter().setLogDir(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 262 | pnlsr.getConfParameter().setDebugging(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 263 | } |
| 264 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 265 | pnlsr.getConfParameter().setDebugging(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 279 | pnlsr.getConfParameter().setDetailedLogging(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 280 | } |
| 281 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 282 | pnlsr.getConfParameter().setDetailedLogging(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 296 | pnlsr.getConfParameter().setIsHyperbolicCalc(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 297 | } |
| 298 | else if(command == "dry-run" || command == "DRY-RUN"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 299 | pnlsr.getConfParameter().setIsHyperbolicCalc(2); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 300 | } |
| 301 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 302 | pnlsr.getConfParameter().setIsHyperbolicCalc(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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; |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 314 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){ |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 326 | pnlsr.getConfParameter().setCorR(r); |
| 327 | pnlsr.getConfParameter().setCorTheta(theta); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | int |
| 334 | ConfFileProcessor::processConfCommandNdnNeighbor(nlsr& pnlsr, string command){ |
| 335 | if(command.empty() ){ |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame^] | 336 | cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl; |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 337 | }else{ |
| 338 | nlsrTokenizer nt(command," "); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame^] | 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 | } |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 361 | pnlsr.getNpl().insertIntoNpl(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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; |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 371 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){ |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 381 | pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |