blob: e6067f9fab054c09bd904113ff90b20c8062e862 [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 }
102 else if( (nt.getFirstToken() == "logdir"))
103 {
104 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
105 }
106 else if( (nt.getFirstToken() == "detailed-logging") )
107 {
108 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
109 }
110 else if( (nt.getFirstToken() == "debugging") )
111 {
112 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
113 }
114 else if( (nt.getFirstToken() == "chronosync-sync-prefix") )
115 {
116 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
117 }
118 else if( (nt.getFirstToken() == "hyperbolic-cordinate") )
119 {
120 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
121 }
122 else if( (nt.getFirstToken() == "hyperbolic-routing"))
123 {
124 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
125 }
126 else if( (nt.getFirstToken() == "tunnel-type"))
127 {
128 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
129 }
130 else
131 {
132 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
133 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600134 return ret;
135 }
akmhoque298385a2014-02-13 14:13:09 -0600136
akmhoque1fd8c1e2014-02-19 19:41:49 -0600137 int
138 ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command)
139 {
140 if(command.empty() )
141 {
142 cerr <<" Network can not be null or empty :( !"<<endl;
143 return -1;
144 }
145 else
146 {
147 if(command[command.size()-1] == '/' )
148 {
149 command.erase(command.size() - 1);
150 }
151 if(command[0] == '/' )
152 {
153 command.erase(0,1);
154 }
155 pnlsr.getConfParameter().setNetwork(command);
156 }
157 return 0;
158 }
akmhoque298385a2014-02-13 14:13:09 -0600159
akmhoque1fd8c1e2014-02-19 19:41:49 -0600160 int
161 ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command)
162 {
163 if(command.empty() )
164 {
165 cerr <<"Site name can not be null or empty :( !"<<endl;
166 return -1;
167 }
168 else
169 {
170 if(command[command.size()-1] == '/' )
171 {
172 command.erase(command.size() - 1);
173 }
174 if(command[0] == '/' )
175 {
176 command.erase(0,1);
177 }
178 pnlsr.getConfParameter().setSiteName(command);
179 }
180 return 0;
181 }
akmhoque298385a2014-02-13 14:13:09 -0600182
akmhoque1fd8c1e2014-02-19 19:41:49 -0600183 int
akmhoque2bb198e2014-02-28 11:46:27 -0600184 ConfFileProcessor::processConfCommandRootKeyPrefix(Nlsr& pnlsr, string command)
185 {
186 if(command.empty() )
187 {
188 cerr <<"Root Key Prefix can not be null or empty :( !"<<endl;
189 return -1;
190 }
191 else
192 {
193 if(command[command.size()-1] == '/' )
194 {
195 command.erase(command.size() - 1);
196 }
197 if(command[0] == '/' )
198 {
199 command.erase(0,1);
200 }
201 pnlsr.getConfParameter().setRootKeyPrefix(command);
202 }
203 return 0;
204 }
205
206
207 int
akmhoque1fd8c1e2014-02-19 19:41:49 -0600208 ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command)
209 {
210 if(command.empty() )
211 {
212 cerr <<" Router name can not be null or empty :( !"<<endl;
213 return -1;
214 }
215 else
216 {
217 if(command[command.size()-1] == '/' )
218 {
219 command.erase(command.size() - 1);
220 }
221 if(command[0] == '/' )
222 {
223 command.erase(0,1);
224 }
225 pnlsr.getConfParameter().setRouterName(command);
226 }
227 return 0;
228 }
akmhoque298385a2014-02-13 14:13:09 -0600229
akmhoque1fd8c1e2014-02-19 19:41:49 -0600230 int
231 ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr,
232 string command)
233 {
234 if(command.empty() )
235 {
236 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
237 }
238 else
239 {
240 int irn;
241 stringstream ss(command.c_str());
242 ss>>irn;
243 if ( irn >=1 && irn <=5)
244 {
245 pnlsr.getConfParameter().setInterestRetryNumber(irn);
246 }
247 }
248 return 0;
249 }
akmhoque298385a2014-02-13 14:13:09 -0600250
akmhoque1fd8c1e2014-02-19 19:41:49 -0600251 int
252 ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr,
253 string command)
254 {
255 if(command.empty() )
256 {
257 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
258 }
259 else
260 {
261 int irt;
262 stringstream ss(command.c_str());
263 ss>>irt;
264 if( irt>=1 && irt <=20)
265 {
266 pnlsr.getConfParameter().setInterestResendTime(irt);
267 }
268 }
269 return 0;
270 }
akmhoque298385a2014-02-13 14:13:09 -0600271
akmhoque1fd8c1e2014-02-19 19:41:49 -0600272 int
273 ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command)
274 {
275 if(command.empty() )
276 {
277 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
278 }
279 else
280 {
281 int lrt;
282 stringstream ss(command.c_str());
283 ss>>lrt;
284 if ( lrt>= 240 && lrt<=7200)
285 {
286 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
287 }
288 }
289 return 0;
290 }
akmhoque298385a2014-02-13 14:13:09 -0600291
akmhoque1fd8c1e2014-02-19 19:41:49 -0600292 int
293 ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr,
294 string command)
295 {
296 if(command.empty() )
297 {
298 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
299 }
300 else
301 {
302 int mfpp;
303 stringstream ss(command.c_str());
304 ss>>mfpp;
305 if ( mfpp>=0 && mfpp<=60)
306 {
307 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
308 }
309 }
310 return 0;
311 }
312
313 int
314 ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command)
315 {
316 if(command.empty() )
317 {
318 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
319 }
320 else
321 {
322 if(command == "tcp" || command == "TCP" )
323 {
324 pnlsr.getConfParameter().setTunnelType(1);
325 }
326 else if(command == "udp" || command == "UDP")
327 {
328 pnlsr.getConfParameter().setTunnelType(0);
329 }
330 else
331 {
332 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
333 }
334 }
335 return 0;
336 }
337
338 int
339 ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr,
340 string command)
341 {
342 if(command.empty() )
343 {
344 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
345 }
346 else
347 {
348 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
349 }
350 return 0;
351 }
akmhoque298385a2014-02-13 14:13:09 -0600352
353
akmhoque1fd8c1e2014-02-19 19:41:49 -0600354 int
355 ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command)
356 {
357 if(command.empty() )
358 {
359 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
360 }
361 else
362 {
363 pnlsr.getConfParameter().setLogDir(command);
364 }
365 return 0;
366 }
akmhoque298385a2014-02-13 14:13:09 -0600367
akmhoque1fd8c1e2014-02-19 19:41:49 -0600368 int
369 ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command)
370 {
371 if(command.empty() )
372 {
373 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
374 }
375 else
376 {
377 if(command == "on" || command == "ON" )
378 {
379 pnlsr.getConfParameter().setDebugging(1);
380 }
381 else if(command == "off" || command == "off")
382 {
383 pnlsr.getConfParameter().setDebugging(0);
384 }
385 else
386 {
387 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
388 }
389 }
390 return 0;
391 }
akmhoque298385a2014-02-13 14:13:09 -0600392
akmhoque1fd8c1e2014-02-19 19:41:49 -0600393 int
394 ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr,
395 string command)
396 {
397 if(command.empty() )
398 {
399 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
400 }
401 else
402 {
403 if(command == "on" || command == "ON" )
404 {
405 pnlsr.getConfParameter().setDetailedLogging(1);
406 }
407 else if(command == "off" || command == "off")
408 {
409 pnlsr.getConfParameter().setDetailedLogging(0);
410 }
411 else
412 {
413 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
414 }
415 }
416 return 0;
417 }
akmhoque298385a2014-02-13 14:13:09 -0600418
akmhoque1fd8c1e2014-02-19 19:41:49 -0600419 int
420 ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr,
421 string command)
422 {
423 if(command.empty() )
424 {
425 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
426 }
427 else
428 {
429 if(command == "on" || command == "ON" )
430 {
431 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
432 }
433 else if(command == "dry-run" || command == "DRY-RUN")
434 {
435 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
436 }
437 else if(command == "off" || command == "off")
438 {
439 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
440 }
441 else
442 {
443 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
444 }
445 }
446 return 0;
447 }
akmhoque298385a2014-02-13 14:13:09 -0600448
akmhoque1fd8c1e2014-02-19 19:41:49 -0600449 int
450 ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr,
451 string command)
452 {
453 if(command.empty() )
454 {
455 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
456 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
457 {
458 return -1;
459 }
460 }
461 else
462 {
463 nlsrTokenizer nt(command," ");
464 stringstream ssr(nt.getFirstToken().c_str());
465 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600466 double r,theta;
467 ssr>>r;
468 sst>>theta;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600469 pnlsr.getConfParameter().setCorR(r);
470 pnlsr.getConfParameter().setCorTheta(theta);
471 }
472 return 0;
473 }
akmhoque298385a2014-02-13 14:13:09 -0600474
475
akmhoque1fd8c1e2014-02-19 19:41:49 -0600476 int
477 ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command)
478 {
479 if(command.empty() )
480 {
481 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
482 }
483 else
484 {
485 nlsrTokenizer nt(command," ");
486 if( nt.getRestOfLine().empty())
487 {
488 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
489 return 0;
490 }
491 else
492 {
493 stringstream sst(nt.getRestOfLine().c_str());
494 int faceId;
495 sst>>faceId;
496 Adjacent adj(nt.getFirstToken(),faceId,0.0,0,0);
497 pnlsr.getAdl().insert(adj);
498 }
499 }
500 return 0;
501 }
akmhoque298385a2014-02-13 14:13:09 -0600502
akmhoque1fd8c1e2014-02-19 19:41:49 -0600503 int
504 ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command)
505 {
506 if(command.empty() )
507 {
508 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
509 }
510 else
511 {
512 pnlsr.getNpl().insertIntoNpl(command);
513 }
514 return 0;
515 }
akmhoque298385a2014-02-13 14:13:09 -0600516
517
akmhoque1fd8c1e2014-02-19 19:41:49 -0600518 int
519 ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command)
520 {
521 if(command.empty() )
522 {
523 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
524 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
525 {
526 return -1;
527 }
528 }
529 else
530 {
531 nlsrTokenizer nt(command," ");
532 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600533 double cost;
534 sst>>cost;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600535 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
536 }
537 return 0;
538 }
akmhoque298385a2014-02-13 14:13:09 -0600539
akmhoqueb1710aa2014-02-19 17:13:36 -0600540} //namespace nlsr
541