blob: 13432d089d3d44b75025241eae5883cd29a94183 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#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"
akmhoque2bb198e2014-02-28 11:46:27 -06009#include "utility/nlsr_tokenizer.hpp"
akmhoque298385a2014-02-13 14:13:09 -060010#include "nlsr_adjacent.hpp"
11
12
akmhoque1fd8c1e2014-02-19 19:41:49 -060013namespace nlsr
14{
akmhoqueb1710aa2014-02-19 17:13:36 -060015
akmhoque1fd8c1e2014-02-19 19:41:49 -060016 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque1fd8c1e2014-02-19 19:41:49 -060018 int
19 ConfFileProcessor::processConfFile(Nlsr& pnlsr)
20 {
21 int ret=0;
akmhoque1fd8c1e2014-02-19 19:41:49 -060022 if ( !confFileName.empty())
23 {
24 std::ifstream inputFile(confFileName.c_str());
25 if ( inputFile.is_open())
26 {
27 for( string line; getline( inputFile, line ); )
28 {
29 if (!line.empty() )
30 {
31 if(line[0]!= '#' && line[0]!='!')
32 {
33 ret=processConfCommand(pnlsr, line);
34 if( ret == -1 )
35 {
36 break;
37 }
38 }
39 }
40 }
41 }
42 else
43 {
44 std::cerr <<"Configuration file: ("<<confFileName<<") does not exist :(";
45 std::cerr <<endl;
46 ret=-1;
47 }
48 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060049 return ret;
akmhoque298385a2014-02-13 14:13:09 -060050 }
51
akmhoque298385a2014-02-13 14:13:09 -060052
akmhoque1fd8c1e2014-02-19 19:41:49 -060053 int
54 ConfFileProcessor::processConfCommand(Nlsr& pnlsr, string command)
55 {
56 int ret=0;
57 nlsrTokenizer nt(command," ");
58 if( (nt.getFirstToken() == "network"))
59 {
60 ret=processConfCommandNetwork(pnlsr,nt.getRestOfLine());
61 }
62 else if( (nt.getFirstToken() == "site-name"))
63 {
64 ret=processConfCommandSiteName(pnlsr,nt.getRestOfLine());
65 }
akmhoque2bb198e2014-02-28 11:46:27 -060066 else if ( (nt.getFirstToken() == "root-key-prefix"))
67 {
68 ret=processConfCommandRootKeyPrefix(pnlsr,nt.getRestOfLine());
69 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060070 else if ( (nt.getFirstToken() == "router-name"))
71 {
72 ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine());
73 }
74 else if( (nt.getFirstToken() == "ndnneighbor") )
75 {
76 ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine());
77 }
78 else if( (nt.getFirstToken() == "link-cost"))
79 {
80 ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine());
81 }
82 else if( (nt.getFirstToken() == "ndnname") )
83 {
84 ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine());
85 }
86 else if( (nt.getFirstToken() == "interest-retry-num"))
87 {
88 processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine());
89 }
90 else if( (nt.getFirstToken() == "interest-resend-time"))
91 {
92 processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine());
93 }
94 else if( (nt.getFirstToken() == "lsa-refresh-time"))
95 {
96 processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine());
97 }
98 else if( (nt.getFirstToken() == "max-faces-per-prefix"))
99 {
100 processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine());
101 }
akmhoqueeb764c52014-03-11 16:01:09 -0500102 else if( (nt.getFirstToken() == "log-dir"))
akmhoque1fd8c1e2014-02-19 19:41:49 -0600103 {
104 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
105 }
akmhoqueeb764c52014-03-11 16:01:09 -0500106 else if( (nt.getFirstToken() == "cert-dir"))
107 {
108 processConfCommandCertDir(pnlsr,nt.getRestOfLine());
109 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600110 else if( (nt.getFirstToken() == "detailed-logging") )
111 {
112 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
113 }
114 else if( (nt.getFirstToken() == "debugging") )
115 {
116 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
117 }
118 else if( (nt.getFirstToken() == "chronosync-sync-prefix") )
119 {
120 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
121 }
122 else if( (nt.getFirstToken() == "hyperbolic-cordinate") )
123 {
124 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
125 }
126 else if( (nt.getFirstToken() == "hyperbolic-routing"))
127 {
128 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
129 }
130 else if( (nt.getFirstToken() == "tunnel-type"))
131 {
132 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
133 }
134 else
135 {
136 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
137 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600138 return ret;
139 }
akmhoque298385a2014-02-13 14:13:09 -0600140
akmhoque1fd8c1e2014-02-19 19:41:49 -0600141 int
142 ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command)
143 {
144 if(command.empty() )
145 {
146 cerr <<" Network can not be null or empty :( !"<<endl;
147 return -1;
148 }
149 else
150 {
151 if(command[command.size()-1] == '/' )
152 {
153 command.erase(command.size() - 1);
154 }
155 if(command[0] == '/' )
156 {
157 command.erase(0,1);
158 }
159 pnlsr.getConfParameter().setNetwork(command);
160 }
161 return 0;
162 }
akmhoque298385a2014-02-13 14:13:09 -0600163
akmhoque1fd8c1e2014-02-19 19:41:49 -0600164 int
165 ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command)
166 {
167 if(command.empty() )
168 {
169 cerr <<"Site name can not be null or empty :( !"<<endl;
170 return -1;
171 }
172 else
173 {
174 if(command[command.size()-1] == '/' )
175 {
176 command.erase(command.size() - 1);
177 }
178 if(command[0] == '/' )
179 {
180 command.erase(0,1);
181 }
182 pnlsr.getConfParameter().setSiteName(command);
183 }
184 return 0;
185 }
akmhoque298385a2014-02-13 14:13:09 -0600186
akmhoque1fd8c1e2014-02-19 19:41:49 -0600187 int
akmhoque2bb198e2014-02-28 11:46:27 -0600188 ConfFileProcessor::processConfCommandRootKeyPrefix(Nlsr& pnlsr, string command)
189 {
190 if(command.empty() )
191 {
192 cerr <<"Root Key Prefix can not be null or empty :( !"<<endl;
193 return -1;
194 }
195 else
196 {
197 if(command[command.size()-1] == '/' )
198 {
199 command.erase(command.size() - 1);
200 }
201 if(command[0] == '/' )
202 {
203 command.erase(0,1);
204 }
205 pnlsr.getConfParameter().setRootKeyPrefix(command);
206 }
207 return 0;
208 }
209
210
211 int
akmhoque1fd8c1e2014-02-19 19:41:49 -0600212 ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command)
213 {
214 if(command.empty() )
215 {
216 cerr <<" Router name can not be null or empty :( !"<<endl;
217 return -1;
218 }
219 else
220 {
221 if(command[command.size()-1] == '/' )
222 {
223 command.erase(command.size() - 1);
224 }
225 if(command[0] == '/' )
226 {
227 command.erase(0,1);
228 }
229 pnlsr.getConfParameter().setRouterName(command);
230 }
231 return 0;
232 }
akmhoque298385a2014-02-13 14:13:09 -0600233
akmhoque1fd8c1e2014-02-19 19:41:49 -0600234 int
235 ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr,
236 string command)
237 {
238 if(command.empty() )
239 {
240 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
241 }
242 else
243 {
244 int irn;
245 stringstream ss(command.c_str());
246 ss>>irn;
247 if ( irn >=1 && irn <=5)
248 {
249 pnlsr.getConfParameter().setInterestRetryNumber(irn);
250 }
251 }
252 return 0;
253 }
akmhoque298385a2014-02-13 14:13:09 -0600254
akmhoque1fd8c1e2014-02-19 19:41:49 -0600255 int
256 ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr,
257 string command)
258 {
259 if(command.empty() )
260 {
261 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
262 }
263 else
264 {
265 int irt;
266 stringstream ss(command.c_str());
267 ss>>irt;
268 if( irt>=1 && irt <=20)
269 {
270 pnlsr.getConfParameter().setInterestResendTime(irt);
271 }
272 }
273 return 0;
274 }
akmhoque298385a2014-02-13 14:13:09 -0600275
akmhoque1fd8c1e2014-02-19 19:41:49 -0600276 int
277 ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command)
278 {
279 if(command.empty() )
280 {
281 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
282 }
283 else
284 {
285 int lrt;
286 stringstream ss(command.c_str());
287 ss>>lrt;
288 if ( lrt>= 240 && lrt<=7200)
289 {
290 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
291 }
292 }
293 return 0;
294 }
akmhoque298385a2014-02-13 14:13:09 -0600295
akmhoque1fd8c1e2014-02-19 19:41:49 -0600296 int
297 ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr,
298 string command)
299 {
300 if(command.empty() )
301 {
302 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
303 }
304 else
305 {
306 int mfpp;
307 stringstream ss(command.c_str());
308 ss>>mfpp;
309 if ( mfpp>=0 && mfpp<=60)
310 {
311 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
312 }
313 }
314 return 0;
315 }
316
317 int
318 ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command)
319 {
320 if(command.empty() )
321 {
322 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
323 }
324 else
325 {
326 if(command == "tcp" || command == "TCP" )
327 {
328 pnlsr.getConfParameter().setTunnelType(1);
329 }
330 else if(command == "udp" || command == "UDP")
331 {
332 pnlsr.getConfParameter().setTunnelType(0);
333 }
334 else
335 {
336 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
337 }
338 }
339 return 0;
340 }
341
342 int
343 ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr,
344 string command)
345 {
346 if(command.empty() )
347 {
348 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
349 }
350 else
351 {
352 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
353 }
354 return 0;
355 }
akmhoque298385a2014-02-13 14:13:09 -0600356
357
akmhoque1fd8c1e2014-02-19 19:41:49 -0600358 int
359 ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command)
360 {
361 if(command.empty() )
362 {
363 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
364 }
365 else
366 {
367 pnlsr.getConfParameter().setLogDir(command);
368 }
369 return 0;
370 }
akmhoqueeb764c52014-03-11 16:01:09 -0500371
372 int
373 ConfFileProcessor::processConfCommandCertDir(Nlsr& pnlsr, string command)
374 {
375 if(command.empty() )
376 {
377 cerr <<" Wrong command format ! [cert-dir /path/to/cert/dir]!"<<endl;
378 }
379 else
380 {
381 pnlsr.getConfParameter().setCertDir(command);
382 }
383 return 0;
384 }
akmhoque298385a2014-02-13 14:13:09 -0600385
akmhoque1fd8c1e2014-02-19 19:41:49 -0600386 int
387 ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command)
388 {
389 if(command.empty() )
390 {
391 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
392 }
393 else
394 {
395 if(command == "on" || command == "ON" )
396 {
397 pnlsr.getConfParameter().setDebugging(1);
398 }
399 else if(command == "off" || command == "off")
400 {
401 pnlsr.getConfParameter().setDebugging(0);
402 }
403 else
404 {
405 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
406 }
407 }
408 return 0;
409 }
akmhoque298385a2014-02-13 14:13:09 -0600410
akmhoque1fd8c1e2014-02-19 19:41:49 -0600411 int
412 ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr,
413 string command)
414 {
415 if(command.empty() )
416 {
417 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
418 }
419 else
420 {
421 if(command == "on" || command == "ON" )
422 {
423 pnlsr.getConfParameter().setDetailedLogging(1);
424 }
425 else if(command == "off" || command == "off")
426 {
427 pnlsr.getConfParameter().setDetailedLogging(0);
428 }
429 else
430 {
431 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
432 }
433 }
434 return 0;
435 }
akmhoque298385a2014-02-13 14:13:09 -0600436
akmhoque1fd8c1e2014-02-19 19:41:49 -0600437 int
438 ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr,
439 string command)
440 {
441 if(command.empty() )
442 {
443 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
444 }
445 else
446 {
447 if(command == "on" || command == "ON" )
448 {
449 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
450 }
451 else if(command == "dry-run" || command == "DRY-RUN")
452 {
453 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
454 }
455 else if(command == "off" || command == "off")
456 {
457 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
458 }
459 else
460 {
461 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
462 }
463 }
464 return 0;
465 }
akmhoque298385a2014-02-13 14:13:09 -0600466
akmhoque1fd8c1e2014-02-19 19:41:49 -0600467 int
468 ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr,
469 string command)
470 {
471 if(command.empty() )
472 {
473 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
474 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
475 {
476 return -1;
477 }
478 }
479 else
480 {
481 nlsrTokenizer nt(command," ");
482 stringstream ssr(nt.getFirstToken().c_str());
483 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600484 double r,theta;
485 ssr>>r;
486 sst>>theta;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600487 pnlsr.getConfParameter().setCorR(r);
488 pnlsr.getConfParameter().setCorTheta(theta);
489 }
490 return 0;
491 }
akmhoque298385a2014-02-13 14:13:09 -0600492
493
akmhoque1fd8c1e2014-02-19 19:41:49 -0600494 int
495 ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command)
496 {
497 if(command.empty() )
498 {
499 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
500 }
501 else
502 {
503 nlsrTokenizer nt(command," ");
504 if( nt.getRestOfLine().empty())
505 {
506 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
507 return 0;
508 }
509 else
510 {
511 stringstream sst(nt.getRestOfLine().c_str());
512 int faceId;
513 sst>>faceId;
514 Adjacent adj(nt.getFirstToken(),faceId,0.0,0,0);
515 pnlsr.getAdl().insert(adj);
516 }
517 }
518 return 0;
519 }
akmhoque298385a2014-02-13 14:13:09 -0600520
akmhoque1fd8c1e2014-02-19 19:41:49 -0600521 int
522 ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command)
523 {
524 if(command.empty() )
525 {
526 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
527 }
528 else
529 {
530 pnlsr.getNpl().insertIntoNpl(command);
531 }
532 return 0;
533 }
akmhoque298385a2014-02-13 14:13:09 -0600534
535
akmhoque1fd8c1e2014-02-19 19:41:49 -0600536 int
537 ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command)
538 {
539 if(command.empty() )
540 {
541 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
542 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
543 {
544 return -1;
545 }
546 }
547 else
548 {
549 nlsrTokenizer nt(command," ");
550 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600551 double cost;
552 sst>>cost;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600553 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
554 }
555 return 0;
556 }
akmhoque298385a2014-02-13 14:13:09 -0600557
akmhoqueb1710aa2014-02-19 17:13:36 -0600558} //namespace nlsr
559