blob: fd9e2173ee5bb7f3cd494671a3c8efdf5a40ceaf [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"
9#include "nlsr_tokenizer.hpp"
10#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 }
66 else if ( (nt.getFirstToken() == "router-name"))
67 {
68 ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine());
69 }
70 else if( (nt.getFirstToken() == "ndnneighbor") )
71 {
72 ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine());
73 }
74 else if( (nt.getFirstToken() == "link-cost"))
75 {
76 ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine());
77 }
78 else if( (nt.getFirstToken() == "ndnname") )
79 {
80 ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine());
81 }
82 else if( (nt.getFirstToken() == "interest-retry-num"))
83 {
84 processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine());
85 }
86 else if( (nt.getFirstToken() == "interest-resend-time"))
87 {
88 processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine());
89 }
90 else if( (nt.getFirstToken() == "lsa-refresh-time"))
91 {
92 processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine());
93 }
94 else if( (nt.getFirstToken() == "max-faces-per-prefix"))
95 {
96 processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine());
97 }
98 else if( (nt.getFirstToken() == "logdir"))
99 {
100 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
101 }
102 else if( (nt.getFirstToken() == "detailed-logging") )
103 {
104 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
105 }
106 else if( (nt.getFirstToken() == "debugging") )
107 {
108 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
109 }
110 else if( (nt.getFirstToken() == "chronosync-sync-prefix") )
111 {
112 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
113 }
114 else if( (nt.getFirstToken() == "hyperbolic-cordinate") )
115 {
116 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
117 }
118 else if( (nt.getFirstToken() == "hyperbolic-routing"))
119 {
120 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
121 }
122 else if( (nt.getFirstToken() == "tunnel-type"))
123 {
124 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
125 }
126 else
127 {
128 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
129 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600130 return ret;
131 }
akmhoque298385a2014-02-13 14:13:09 -0600132
akmhoque1fd8c1e2014-02-19 19:41:49 -0600133 int
134 ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command)
135 {
136 if(command.empty() )
137 {
138 cerr <<" Network can not be null or empty :( !"<<endl;
139 return -1;
140 }
141 else
142 {
143 if(command[command.size()-1] == '/' )
144 {
145 command.erase(command.size() - 1);
146 }
147 if(command[0] == '/' )
148 {
149 command.erase(0,1);
150 }
151 pnlsr.getConfParameter().setNetwork(command);
152 }
153 return 0;
154 }
akmhoque298385a2014-02-13 14:13:09 -0600155
akmhoque1fd8c1e2014-02-19 19:41:49 -0600156 int
157 ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command)
158 {
159 if(command.empty() )
160 {
161 cerr <<"Site name can not be null or empty :( !"<<endl;
162 return -1;
163 }
164 else
165 {
166 if(command[command.size()-1] == '/' )
167 {
168 command.erase(command.size() - 1);
169 }
170 if(command[0] == '/' )
171 {
172 command.erase(0,1);
173 }
174 pnlsr.getConfParameter().setSiteName(command);
175 }
176 return 0;
177 }
akmhoque298385a2014-02-13 14:13:09 -0600178
akmhoque1fd8c1e2014-02-19 19:41:49 -0600179 int
180 ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command)
181 {
182 if(command.empty() )
183 {
184 cerr <<" Router name can not be null or empty :( !"<<endl;
185 return -1;
186 }
187 else
188 {
189 if(command[command.size()-1] == '/' )
190 {
191 command.erase(command.size() - 1);
192 }
193 if(command[0] == '/' )
194 {
195 command.erase(0,1);
196 }
197 pnlsr.getConfParameter().setRouterName(command);
198 }
199 return 0;
200 }
akmhoque298385a2014-02-13 14:13:09 -0600201
akmhoque1fd8c1e2014-02-19 19:41:49 -0600202 int
203 ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr,
204 string command)
205 {
206 if(command.empty() )
207 {
208 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
209 }
210 else
211 {
212 int irn;
213 stringstream ss(command.c_str());
214 ss>>irn;
215 if ( irn >=1 && irn <=5)
216 {
217 pnlsr.getConfParameter().setInterestRetryNumber(irn);
218 }
219 }
220 return 0;
221 }
akmhoque298385a2014-02-13 14:13:09 -0600222
akmhoque1fd8c1e2014-02-19 19:41:49 -0600223 int
224 ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr,
225 string command)
226 {
227 if(command.empty() )
228 {
229 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
230 }
231 else
232 {
233 int irt;
234 stringstream ss(command.c_str());
235 ss>>irt;
236 if( irt>=1 && irt <=20)
237 {
238 pnlsr.getConfParameter().setInterestResendTime(irt);
239 }
240 }
241 return 0;
242 }
akmhoque298385a2014-02-13 14:13:09 -0600243
akmhoque1fd8c1e2014-02-19 19:41:49 -0600244 int
245 ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command)
246 {
247 if(command.empty() )
248 {
249 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
250 }
251 else
252 {
253 int lrt;
254 stringstream ss(command.c_str());
255 ss>>lrt;
256 if ( lrt>= 240 && lrt<=7200)
257 {
258 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
259 }
260 }
261 return 0;
262 }
akmhoque298385a2014-02-13 14:13:09 -0600263
akmhoque1fd8c1e2014-02-19 19:41:49 -0600264 int
265 ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr,
266 string command)
267 {
268 if(command.empty() )
269 {
270 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
271 }
272 else
273 {
274 int mfpp;
275 stringstream ss(command.c_str());
276 ss>>mfpp;
277 if ( mfpp>=0 && mfpp<=60)
278 {
279 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
280 }
281 }
282 return 0;
283 }
284
285 int
286 ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command)
287 {
288 if(command.empty() )
289 {
290 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
291 }
292 else
293 {
294 if(command == "tcp" || command == "TCP" )
295 {
296 pnlsr.getConfParameter().setTunnelType(1);
297 }
298 else if(command == "udp" || command == "UDP")
299 {
300 pnlsr.getConfParameter().setTunnelType(0);
301 }
302 else
303 {
304 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
305 }
306 }
307 return 0;
308 }
309
310 int
311 ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr,
312 string command)
313 {
314 if(command.empty() )
315 {
316 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
317 }
318 else
319 {
320 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
321 }
322 return 0;
323 }
akmhoque298385a2014-02-13 14:13:09 -0600324
325
akmhoque1fd8c1e2014-02-19 19:41:49 -0600326 int
327 ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command)
328 {
329 if(command.empty() )
330 {
331 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
332 }
333 else
334 {
335 pnlsr.getConfParameter().setLogDir(command);
336 }
337 return 0;
338 }
akmhoque298385a2014-02-13 14:13:09 -0600339
akmhoque1fd8c1e2014-02-19 19:41:49 -0600340 int
341 ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command)
342 {
343 if(command.empty() )
344 {
345 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
346 }
347 else
348 {
349 if(command == "on" || command == "ON" )
350 {
351 pnlsr.getConfParameter().setDebugging(1);
352 }
353 else if(command == "off" || command == "off")
354 {
355 pnlsr.getConfParameter().setDebugging(0);
356 }
357 else
358 {
359 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
360 }
361 }
362 return 0;
363 }
akmhoque298385a2014-02-13 14:13:09 -0600364
akmhoque1fd8c1e2014-02-19 19:41:49 -0600365 int
366 ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr,
367 string command)
368 {
369 if(command.empty() )
370 {
371 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
372 }
373 else
374 {
375 if(command == "on" || command == "ON" )
376 {
377 pnlsr.getConfParameter().setDetailedLogging(1);
378 }
379 else if(command == "off" || command == "off")
380 {
381 pnlsr.getConfParameter().setDetailedLogging(0);
382 }
383 else
384 {
385 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
386 }
387 }
388 return 0;
389 }
akmhoque298385a2014-02-13 14:13:09 -0600390
akmhoque1fd8c1e2014-02-19 19:41:49 -0600391 int
392 ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr,
393 string command)
394 {
395 if(command.empty() )
396 {
397 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
398 }
399 else
400 {
401 if(command == "on" || command == "ON" )
402 {
403 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
404 }
405 else if(command == "dry-run" || command == "DRY-RUN")
406 {
407 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
408 }
409 else if(command == "off" || command == "off")
410 {
411 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
412 }
413 else
414 {
415 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
416 }
417 }
418 return 0;
419 }
akmhoque298385a2014-02-13 14:13:09 -0600420
akmhoque1fd8c1e2014-02-19 19:41:49 -0600421 int
422 ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr,
423 string command)
424 {
425 if(command.empty() )
426 {
427 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
428 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
429 {
430 return -1;
431 }
432 }
433 else
434 {
435 nlsrTokenizer nt(command," ");
436 stringstream ssr(nt.getFirstToken().c_str());
437 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600438 double r,theta;
439 ssr>>r;
440 sst>>theta;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600441 pnlsr.getConfParameter().setCorR(r);
442 pnlsr.getConfParameter().setCorTheta(theta);
443 }
444 return 0;
445 }
akmhoque298385a2014-02-13 14:13:09 -0600446
447
akmhoque1fd8c1e2014-02-19 19:41:49 -0600448 int
449 ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command)
450 {
451 if(command.empty() )
452 {
453 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
454 }
455 else
456 {
457 nlsrTokenizer nt(command," ");
458 if( nt.getRestOfLine().empty())
459 {
460 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
461 return 0;
462 }
463 else
464 {
465 stringstream sst(nt.getRestOfLine().c_str());
466 int faceId;
467 sst>>faceId;
468 Adjacent adj(nt.getFirstToken(),faceId,0.0,0,0);
469 pnlsr.getAdl().insert(adj);
470 }
471 }
472 return 0;
473 }
akmhoque298385a2014-02-13 14:13:09 -0600474
akmhoque1fd8c1e2014-02-19 19:41:49 -0600475 int
476 ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command)
477 {
478 if(command.empty() )
479 {
480 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
481 }
482 else
483 {
484 pnlsr.getNpl().insertIntoNpl(command);
485 }
486 return 0;
487 }
akmhoque298385a2014-02-13 14:13:09 -0600488
489
akmhoque1fd8c1e2014-02-19 19:41:49 -0600490 int
491 ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command)
492 {
493 if(command.empty() )
494 {
495 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
496 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 )
497 {
498 return -1;
499 }
500 }
501 else
502 {
503 nlsrTokenizer nt(command," ");
504 stringstream sst(nt.getRestOfLine().c_str());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600505 double cost;
506 sst>>cost;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600507 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
508 }
509 return 0;
510 }
akmhoque298385a2014-02-13 14:13:09 -0600511
akmhoqueb1710aa2014-02-19 17:13:36 -0600512} //namespace nlsr
513