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()); |
| 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 111 | pnlsr.getConfParameter().setNetwork(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 128 | pnlsr.getConfParameter().setSiteName(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | } |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 145 | pnlsr.getConfParameter().setRouterName(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 159 | pnlsr.getConfParameter().setInterestRetryNumber(irn); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 174 | pnlsr.getConfParameter().setInterestResendTime(irt); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 189 | pnlsr.getConfParameter().setLsaRefreshTime(lrt); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 204 | pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 216 | pnlsr.getConfParameter().setTunnelType(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 217 | } |
| 218 | else if(command == "udp" || command == "UDP"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 219 | pnlsr.getConfParameter().setTunnelType(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 233 | pnlsr.getConfParameter().setChronosyncSyncPrefix(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 244 | pnlsr.getConfParameter().setLogDir(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 255 | pnlsr.getConfParameter().setDebugging(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 256 | } |
| 257 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 258 | pnlsr.getConfParameter().setDebugging(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 272 | pnlsr.getConfParameter().setDetailedLogging(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 273 | } |
| 274 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 275 | pnlsr.getConfParameter().setDetailedLogging(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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" ){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 289 | pnlsr.getConfParameter().setIsHyperbolicCalc(1); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 290 | } |
| 291 | else if(command == "dry-run" || command == "DRY-RUN"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 292 | pnlsr.getConfParameter().setIsHyperbolicCalc(2); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 293 | } |
| 294 | else if(command == "off" || command == "off"){ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 295 | pnlsr.getConfParameter().setIsHyperbolicCalc(0); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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; |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 307 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){ |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 319 | pnlsr.getConfParameter().setCorR(r); |
| 320 | pnlsr.getConfParameter().setCorTheta(theta); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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); |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 333 | pnlsr.getAdl().insert(adj); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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{ |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 343 | pnlsr.getNpl().insertIntoNpl(command); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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; |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 353 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){ |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 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 | |
akmhoque | a8cd6b9 | 2014-01-31 20:13:26 -0600 | [diff] [blame] | 363 | pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost); |
akmhoque | 87347a3 | 2014-01-31 11:00:44 -0600 | [diff] [blame] | 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | |