blob: fd3c9effc3b25effb0bdfe6273a0e614bc6a667f [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"
akmhoque05d5fcf2014-04-15 14:58:45 -050011#include "utility/nlsr_logger.hpp"
12
13#define THIS_FILE "nlsr_conf_processor.cpp"
akmhoque298385a2014-02-13 14:13:09 -060014
15
akmhoque1fd8c1e2014-02-19 19:41:49 -060016namespace nlsr
17{
akmhoqueb1710aa2014-02-19 17:13:36 -060018
akmhoque5a44dd42014-03-12 18:11:32 -050019 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060020
akmhoque5a44dd42014-03-12 18:11:32 -050021 int
22 ConfFileProcessor::processConfFile(Nlsr& pnlsr)
23 {
24 int ret=0;
akmhoque05d5fcf2014-04-15 14:58:45 -050025 if ( !m_confFileName.empty())
akmhoque1fd8c1e2014-02-19 19:41:49 -060026 {
akmhoque05d5fcf2014-04-15 14:58:45 -050027 std::ifstream inputFile(m_confFileName.c_str());
akmhoque5a44dd42014-03-12 18:11:32 -050028 if ( inputFile.is_open())
29 {
30 for( string line; getline( inputFile, line ); )
akmhoque1fd8c1e2014-02-19 19:41:49 -060031 {
akmhoque5a44dd42014-03-12 18:11:32 -050032 if (!line.empty() )
33 {
34 if(line[0]!= '#' && line[0]!='!')
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 {
akmhoque5a44dd42014-03-12 18:11:32 -050036 ret=processConfCommand(pnlsr, line);
37 if( ret == -1 )
38 {
39 break;
40 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060041 }
akmhoque5a44dd42014-03-12 18:11:32 -050042 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060043 }
akmhoque5a44dd42014-03-12 18:11:32 -050044 }
45 else
46 {
akmhoque05d5fcf2014-04-15 14:58:45 -050047 std::cerr <<"Configuration file: ("<<m_confFileName<<") does not exist :(";
akmhoque5a44dd42014-03-12 18:11:32 -050048 std::cerr <<endl;
49 ret=-1;
50 }
akmhoque298385a2014-02-13 14:13:09 -060051 }
akmhoque5a44dd42014-03-12 18:11:32 -050052 return ret;
53 }
akmhoque298385a2014-02-13 14:13:09 -060054
akmhoque298385a2014-02-13 14:13:09 -060055
akmhoque5a44dd42014-03-12 18:11:32 -050056 int
57 ConfFileProcessor::processConfCommand(Nlsr& pnlsr, string command)
58 {
59 int ret=0;
60 nlsrTokenizer nt(command," ");
61 if( (nt.getFirstToken() == "network"))
akmhoque1fd8c1e2014-02-19 19:41:49 -060062 {
akmhoque5a44dd42014-03-12 18:11:32 -050063 ret=processConfCommandNetwork(pnlsr,nt.getRestOfLine());
akmhoque1fd8c1e2014-02-19 19:41:49 -060064 }
akmhoque5a44dd42014-03-12 18:11:32 -050065 else if( (nt.getFirstToken() == "site-name"))
akmhoque1fd8c1e2014-02-19 19:41:49 -060066 {
akmhoque5a44dd42014-03-12 18:11:32 -050067 ret=processConfCommandSiteName(pnlsr,nt.getRestOfLine());
68 }
69 else if ( (nt.getFirstToken() == "root-key-prefix"))
70 {
71 ret=processConfCommandRootKeyPrefix(pnlsr,nt.getRestOfLine());
72 }
73 else if ( (nt.getFirstToken() == "router-name"))
74 {
75 ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine());
76 }
77 else if( (nt.getFirstToken() == "ndnneighbor") )
78 {
79 ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine());
80 }
81 else if( (nt.getFirstToken() == "link-cost"))
82 {
83 ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine());
84 }
85 else if( (nt.getFirstToken() == "ndnname") )
86 {
87 ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine());
88 }
89 else if( (nt.getFirstToken() == "interest-retry-num"))
90 {
91 processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine());
92 }
93 else if( (nt.getFirstToken() == "interest-resend-time"))
94 {
95 processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine());
96 }
97 else if( (nt.getFirstToken() == "lsa-refresh-time"))
98 {
99 processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine());
100 }
101 else if( (nt.getFirstToken() == "max-faces-per-prefix"))
102 {
103 processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine());
104 }
105 else if( (nt.getFirstToken() == "log-dir"))
106 {
107 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
108 }
109 else if( (nt.getFirstToken() == "cert-dir"))
110 {
111 processConfCommandCertDir(pnlsr,nt.getRestOfLine());
112 }
113 else if( (nt.getFirstToken() == "detailed-logging") )
114 {
115 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
116 }
117 else if( (nt.getFirstToken() == "debugging") )
118 {
119 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
120 }
121 else if( (nt.getFirstToken() == "chronosync-sync-prefix") )
122 {
123 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
124 }
125 else if( (nt.getFirstToken() == "hyperbolic-cordinate") )
126 {
127 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
128 }
129 else if( (nt.getFirstToken() == "hyperbolic-routing"))
130 {
131 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
132 }
133 else if( (nt.getFirstToken() == "tunnel-type"))
134 {
135 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
136 }
137 else
138 {
139 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
140 }
141 return ret;
142 }
143
144 int
145 ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command)
146 {
147 if(command.empty() )
148 {
149 cerr <<" Network can not be null or empty :( !"<<endl;
150 return -1;
151 }
152 else
153 {
154 if(command[command.size()-1] == '/' )
155 {
156 command.erase(command.size() - 1);
157 }
158 if(command[0] == '/' )
159 {
160 command.erase(0,1);
161 }
162 pnlsr.getConfParameter().setNetwork(command);
163 }
164 return 0;
165 }
166
167 int
168 ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command)
169 {
170 if(command.empty() )
171 {
172 cerr <<"Site name can not be null or empty :( !"<<endl;
173 return -1;
174 }
175 else
176 {
177 if(command[command.size()-1] == '/' )
178 {
179 command.erase(command.size() - 1);
180 }
181 if(command[0] == '/' )
182 {
183 command.erase(0,1);
184 }
185 pnlsr.getConfParameter().setSiteName(command);
186 }
187 return 0;
188 }
189
190 int
191 ConfFileProcessor::processConfCommandRootKeyPrefix(Nlsr& pnlsr, string command)
192 {
193 if(command.empty() )
194 {
195 cerr <<"Root Key Prefix can not be null or empty :( !"<<endl;
196 return -1;
197 }
198 else
199 {
200 if(command[command.size()-1] == '/' )
201 {
202 command.erase(command.size() - 1);
203 }
204 if(command[0] == '/' )
205 {
206 command.erase(0,1);
207 }
208 pnlsr.getConfParameter().setRootKeyPrefix(command);
209 }
210 return 0;
211 }
212
213
214 int
215 ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command)
216 {
217 if(command.empty() )
218 {
219 cerr <<" Router name can not be null or empty :( !"<<endl;
220 return -1;
221 }
222 else
223 {
224 if(command[command.size()-1] == '/' )
225 {
226 command.erase(command.size() - 1);
227 }
228 if(command[0] == '/' )
229 {
230 command.erase(0,1);
231 }
232 pnlsr.getConfParameter().setRouterName(command);
233 }
234 return 0;
235 }
236
237 int
238 ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr,
239 string command)
240 {
241 if(command.empty() )
242 {
243 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
244 }
245 else
246 {
247 int irn;
248 stringstream ss(command.c_str());
249 ss>>irn;
250 if ( irn >=1 && irn <=5)
251 {
252 pnlsr.getConfParameter().setInterestRetryNumber(irn);
253 }
254 }
255 return 0;
256 }
257
258 int
259 ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr,
260 string command)
261 {
262 if(command.empty() )
263 {
264 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
265 }
266 else
267 {
268 int irt;
269 stringstream ss(command.c_str());
270 ss>>irt;
271 if( irt>=1 && irt <=20)
272 {
273 pnlsr.getConfParameter().setInterestResendTime(irt);
274 }
275 }
276 return 0;
277 }
278
279 int
280 ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command)
281 {
282 if(command.empty() )
283 {
284 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
285 }
286 else
287 {
288 int lrt;
289 stringstream ss(command.c_str());
290 ss>>lrt;
291 if ( lrt>= 240 && lrt<=7200)
292 {
293 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
294 }
295 }
296 return 0;
297 }
298
299 int
300 ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr,
301 string command)
302 {
303 if(command.empty() )
304 {
305 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
306 }
307 else
308 {
309 int mfpp;
310 stringstream ss(command.c_str());
311 ss>>mfpp;
312 if ( mfpp>=0 && mfpp<=60)
313 {
314 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
315 }
316 }
317 return 0;
318 }
319
320 int
321 ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command)
322 {
323 if(command.empty() )
324 {
325 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
326 }
327 else
328 {
329 if(command == "tcp" || command == "TCP" )
330 {
331 pnlsr.getConfParameter().setTunnelType(1);
332 }
333 else if(command == "udp" || command == "UDP")
334 {
335 pnlsr.getConfParameter().setTunnelType(0);
336 }
337 else
338 {
339 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
340 }
341 }
342 return 0;
343 }
344
345 int
346 ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr,
347 string command)
348 {
349 if(command.empty() )
350 {
351 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
352 }
353 else
354 {
355 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
356 }
357 return 0;
358 }
359
360
361 int
362 ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command)
363 {
364 if(command.empty() )
365 {
366 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
367 }
368 else
369 {
370 pnlsr.getConfParameter().setLogDir(command);
371 }
372 return 0;
373 }
374
375 int
376 ConfFileProcessor::processConfCommandCertDir(Nlsr& pnlsr, string command)
377 {
378 if(command.empty() )
379 {
380 cerr <<" Wrong command format ! [cert-dir /path/to/cert/dir]!"<<endl;
381 }
382 else
383 {
384 pnlsr.getConfParameter().setCertDir(command);
385 }
386 return 0;
387 }
388
389 int
390 ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command)
391 {
392 if(command.empty() )
393 {
394 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
395 }
396 else
397 {
398 if(command == "on" || command == "ON" )
399 {
400 pnlsr.getConfParameter().setDebugging(1);
401 }
402 else if(command == "off" || command == "off")
403 {
404 pnlsr.getConfParameter().setDebugging(0);
405 }
406 else
407 {
408 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
409 }
410 }
411 return 0;
412 }
413
414 int
415 ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr,
416 string command)
417 {
418 if(command.empty() )
419 {
420 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
421 }
422 else
423 {
424 if(command == "on" || command == "ON" )
425 {
426 pnlsr.getConfParameter().setDetailedLogging(1);
427 }
428 else if(command == "off" || command == "off")
429 {
430 pnlsr.getConfParameter().setDetailedLogging(0);
431 }
432 else
433 {
434 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
435 }
436 }
437 return 0;
438 }
439
440 int
441 ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr,
442 string command)
443 {
444 if(command.empty() )
445 {
446 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
447 }
448 else
449 {
450 if(command == "on" || command == "ON" )
451 {
452 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
453 }
454 else if(command == "dry-run" || command == "DRY-RUN")
455 {
456 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
457 }
458 else if(command == "off" || command == "off")
459 {
460 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
461 }
462 else
463 {
464 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
465 }
466 }
467 return 0;
468 }
469
470 int
471 ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr,
472 string command)
473 {
474 if(command.empty() )
475 {
476 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
477 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
478 {
479 return -1;
480 }
481 }
482 else
483 {
484 nlsrTokenizer nt(command," ");
485 stringstream ssr(nt.getFirstToken().c_str());
486 stringstream sst(nt.getRestOfLine().c_str());
487 double r,theta;
488 ssr>>r;
489 sst>>theta;
490 pnlsr.getConfParameter().setCorR(r);
491 pnlsr.getConfParameter().setCorTheta(theta);
492 }
493 return 0;
494 }
495
496
497 int
498 ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command)
499 {
500 if(command.empty() )
501 {
502 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
503 }
504 else
505 {
506 nlsrTokenizer nt(command," ");
507 if( nt.getRestOfLine().empty())
508 {
509 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600510 return 0;
akmhoque5a44dd42014-03-12 18:11:32 -0500511 }
512 else
513 {
514 stringstream sst(nt.getRestOfLine().c_str());
515 int faceId;
516 sst>>faceId;
517 Adjacent adj(nt.getFirstToken(),faceId,0.0,0,0);
518 pnlsr.getAdl().insert(adj);
519 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600520 }
akmhoque5a44dd42014-03-12 18:11:32 -0500521 return 0;
522 }
akmhoque298385a2014-02-13 14:13:09 -0600523
akmhoque5a44dd42014-03-12 18:11:32 -0500524 int
525 ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command)
526 {
527 if(command.empty() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600528 {
akmhoque5a44dd42014-03-12 18:11:32 -0500529 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600530 }
akmhoque5a44dd42014-03-12 18:11:32 -0500531 else
akmhoque2bb198e2014-02-28 11:46:27 -0600532 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500533 pnlsr.getNpl().insert(command);
akmhoque2bb198e2014-02-28 11:46:27 -0600534 }
akmhoque5a44dd42014-03-12 18:11:32 -0500535 return 0;
536 }
akmhoque2bb198e2014-02-28 11:46:27 -0600537
538
akmhoque5a44dd42014-03-12 18:11:32 -0500539 int
540 ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command)
541 {
542 if(command.empty() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600543 {
akmhoque5a44dd42014-03-12 18:11:32 -0500544 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
545 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
546 {
547 return -1;
548 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600549 }
akmhoque5a44dd42014-03-12 18:11:32 -0500550 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600551 {
akmhoque5a44dd42014-03-12 18:11:32 -0500552 nlsrTokenizer nt(command," ");
553 stringstream sst(nt.getRestOfLine().c_str());
554 double cost;
555 sst>>cost;
556 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600557 }
akmhoque5a44dd42014-03-12 18:11:32 -0500558 return 0;
559 }
akmhoque298385a2014-02-13 14:13:09 -0600560
akmhoqueb1710aa2014-02-19 17:13:36 -0600561} //namespace nlsr
562