blob: eac9db618a029d2848e63e20ba2f0470c0d9c19d [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <fstream>
3#include <string>
4#include <cstdlib>
5#include <sstream>
6
7#include "conf-file-processor.hpp"
8#include "conf-parameter.hpp"
9#include "utility/tokenizer.hpp"
10#include "adjacent.hpp"
11
12
13namespace nlsr {
14
15using namespace std;
16
17int
akmhoqueb6450b12014-04-24 00:01:03 -050018ConfFileProcessor::processConfFile()
akmhoque53353462014-04-22 08:43:45 -050019{
20 int ret = 0;
21 if (!m_confFileName.empty())
22 {
23 std::ifstream inputFile(m_confFileName.c_str());
24 if (inputFile.is_open())
25 {
26 for (string line; getline(inputFile, line);)
27 {
28 if (!line.empty())
29 {
30 if (line[0] != '#' && line[0] != '!')
31 {
akmhoqueb6450b12014-04-24 00:01:03 -050032 ret = processConfCommand(line);
akmhoque53353462014-04-22 08:43:45 -050033 if (ret == -1)
34 {
35 break;
36 }
37 }
38 }
39 }
40 }
41 else
42 {
43 std::cerr << "Configuration file: (" << m_confFileName << ") does not exist :(";
44 std::cerr << endl;
45 ret = -1;
46 }
47 }
48 return ret;
49}
50
51
52int
akmhoqueb6450b12014-04-24 00:01:03 -050053ConfFileProcessor::processConfCommand(string command)
akmhoque53353462014-04-22 08:43:45 -050054{
55 int ret = 0;
56 Tokenizer nt(command, " ");
57 if ((nt.getFirstToken() == "network"))
58 {
akmhoqueb6450b12014-04-24 00:01:03 -050059 ret = processConfCommandNetwork(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050060 }
61 else if ((nt.getFirstToken() == "site-name"))
62 {
akmhoqueb6450b12014-04-24 00:01:03 -050063 ret = processConfCommandSiteName(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050064 }
65 else if ((nt.getFirstToken() == "root-key-prefix"))
66 {
akmhoqueb6450b12014-04-24 00:01:03 -050067 ret = processConfCommandRootKeyPrefix(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050068 }
69 else if ((nt.getFirstToken() == "router-name"))
70 {
akmhoqueb6450b12014-04-24 00:01:03 -050071 ret = processConfCommandRouterName(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050072 }
73 else if ((nt.getFirstToken() == "ndnneighbor"))
74 {
akmhoqueb6450b12014-04-24 00:01:03 -050075 ret = processConfCommandNdnNeighbor(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050076 }
77 else if ((nt.getFirstToken() == "link-cost"))
78 {
akmhoqueb6450b12014-04-24 00:01:03 -050079 ret = processConfCommandLinkCost(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050080 }
81 else if ((nt.getFirstToken() == "ndnname"))
82 {
akmhoqueb6450b12014-04-24 00:01:03 -050083 ret = processConfCommandNdnName(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050084 }
85 else if ((nt.getFirstToken() == "interest-retry-num"))
86 {
akmhoqueb6450b12014-04-24 00:01:03 -050087 processConfCommandInterestRetryNumber(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050088 }
89 else if ((nt.getFirstToken() == "interest-resend-time"))
90 {
akmhoqueb6450b12014-04-24 00:01:03 -050091 processConfCommandInterestResendTime(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050092 }
93 else if ((nt.getFirstToken() == "lsa-refresh-time"))
94 {
akmhoqueb6450b12014-04-24 00:01:03 -050095 processConfCommandLsaRefreshTime(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -050096 }
97 else if ((nt.getFirstToken() == "max-faces-per-prefix"))
98 {
akmhoqueb6450b12014-04-24 00:01:03 -050099 processConfCommandMaxFacesPerPrefix(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500100 }
101 else if ((nt.getFirstToken() == "log-dir"))
102 {
akmhoqueb6450b12014-04-24 00:01:03 -0500103 processConfCommandLogDir(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500104 }
105 else if ((nt.getFirstToken() == "cert-dir"))
106 {
akmhoqueb6450b12014-04-24 00:01:03 -0500107 processConfCommandCertDir(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500108 }
109 else if ((nt.getFirstToken() == "detailed-logging"))
110 {
akmhoqueb6450b12014-04-24 00:01:03 -0500111 processConfCommandDetailedLogging(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500112 }
113 else if ((nt.getFirstToken() == "debugging"))
114 {
akmhoqueb6450b12014-04-24 00:01:03 -0500115 processConfCommandDebugging(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500116 }
117 else if ((nt.getFirstToken() == "chronosync-sync-prefix"))
118 {
akmhoqueb6450b12014-04-24 00:01:03 -0500119 processConfCommandChronosyncSyncPrefix(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500120 }
121 else if ((nt.getFirstToken() == "hyperbolic-cordinate"))
122 {
akmhoqueb6450b12014-04-24 00:01:03 -0500123 processConfCommandHyperbolicCordinate(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500124 }
125 else if ((nt.getFirstToken() == "hyperbolic-routing"))
126 {
akmhoqueb6450b12014-04-24 00:01:03 -0500127 processConfCommandIsHyperbolicCalc(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500128 }
129 else if ((nt.getFirstToken() == "tunnel-type"))
130 {
akmhoqueb6450b12014-04-24 00:01:03 -0500131 processConfCommandTunnelType(nt.getRestOfLine());
akmhoque53353462014-04-22 08:43:45 -0500132 }
133 else
134 {
135 cout << "Wrong configuration Command: " << nt.getFirstToken() << endl;
136 }
137 return ret;
138}
139
140int
akmhoqueb6450b12014-04-24 00:01:03 -0500141ConfFileProcessor::processConfCommandNetwork(string command)
akmhoque53353462014-04-22 08:43:45 -0500142{
143 if (command.empty())
144 {
145 cerr << " Network can not be null or empty :( !" << endl;
146 return -1;
147 }
148 else
149 {
150 if (command[command.size() - 1] == '/')
151 {
152 command.erase(command.size() - 1);
153 }
154 if (command[0] == '/')
155 {
156 command.erase(0, 1);
157 }
akmhoqueb6450b12014-04-24 00:01:03 -0500158 m_nlsr.getConfParameter().setNetwork(command);
akmhoque53353462014-04-22 08:43:45 -0500159 }
160 return 0;
161}
162
163int
akmhoqueb6450b12014-04-24 00:01:03 -0500164ConfFileProcessor::processConfCommandSiteName(string command)
akmhoque53353462014-04-22 08:43:45 -0500165{
166 if (command.empty())
167 {
168 cerr << "Site name can not be null or empty :( !" << endl;
169 return -1;
170 }
171 else
172 {
173 if (command[command.size() - 1] == '/')
174 {
175 command.erase(command.size() - 1);
176 }
177 if (command[0] == '/')
178 {
179 command.erase(0, 1);
180 }
akmhoqueb6450b12014-04-24 00:01:03 -0500181 m_nlsr.getConfParameter().setSiteName(command);
akmhoque53353462014-04-22 08:43:45 -0500182 }
183 return 0;
184}
185
186int
akmhoqueb6450b12014-04-24 00:01:03 -0500187ConfFileProcessor::processConfCommandRootKeyPrefix(string command)
akmhoque53353462014-04-22 08:43:45 -0500188{
189 if (command.empty())
190 {
191 cerr << "Root Key Prefix can not be null or empty :( !" << endl;
192 return -1;
193 }
194 else
195 {
196 if (command[command.size() - 1] == '/')
197 {
198 command.erase(command.size() - 1);
199 }
200 if (command[0] == '/')
201 {
202 command.erase(0, 1);
203 }
akmhoqueb6450b12014-04-24 00:01:03 -0500204 m_nlsr.getConfParameter().setRootKeyPrefix(command);
akmhoque53353462014-04-22 08:43:45 -0500205 }
206 return 0;
207}
208
209
210int
akmhoqueb6450b12014-04-24 00:01:03 -0500211ConfFileProcessor::processConfCommandRouterName(string command)
akmhoque53353462014-04-22 08:43:45 -0500212{
213 if (command.empty())
214 {
215 cerr << " Router name can not be null or empty :( !" << endl;
216 return -1;
217 }
218 else
219 {
220 if (command[command.size() - 1] == '/')
221 {
222 command.erase(command.size() - 1);
223 }
224 if (command[0] == '/')
225 {
226 command.erase(0, 1);
227 }
akmhoqueb6450b12014-04-24 00:01:03 -0500228 m_nlsr.getConfParameter().setRouterName(command);
akmhoque53353462014-04-22 08:43:45 -0500229 }
230 return 0;
231}
232
233int
akmhoqueb6450b12014-04-24 00:01:03 -0500234ConfFileProcessor::processConfCommandInterestRetryNumber(string command)
akmhoque53353462014-04-22 08:43:45 -0500235{
236 if (command.empty())
237 {
238 cerr << " Wrong command format ! [interest-retry-num n]" << endl;
239 }
240 else
241 {
242 int irn;
243 stringstream ss(command.c_str());
244 ss >> irn;
245 if (irn >= 1 && irn <= 5)
246 {
akmhoqueb6450b12014-04-24 00:01:03 -0500247 m_nlsr.getConfParameter().setInterestRetryNumber(irn);
akmhoque53353462014-04-22 08:43:45 -0500248 }
249 }
250 return 0;
251}
252
253int
akmhoqueb6450b12014-04-24 00:01:03 -0500254ConfFileProcessor::processConfCommandInterestResendTime(string command)
akmhoque53353462014-04-22 08:43:45 -0500255{
256 if (command.empty())
257 {
258 cerr << " Wrong command format ! [interest-resend-time s]" << endl;
259 }
260 else
261 {
262 int irt;
263 stringstream ss(command.c_str());
264 ss >> irt;
265 if (irt >= 1 && irt <= 20)
266 {
akmhoqueb6450b12014-04-24 00:01:03 -0500267 m_nlsr.getConfParameter().setInterestResendTime(irt);
akmhoque53353462014-04-22 08:43:45 -0500268 }
269 }
270 return 0;
271}
272
273int
akmhoqueb6450b12014-04-24 00:01:03 -0500274ConfFileProcessor::processConfCommandLsaRefreshTime(string command)
akmhoque53353462014-04-22 08:43:45 -0500275{
276 if (command.empty())
277 {
278 cerr << " Wrong command format ! [interest-resend-time s]" << endl;
279 }
280 else
281 {
282 int lrt;
283 stringstream ss(command.c_str());
284 ss >> lrt;
285 if (lrt >= 240 && lrt <= 7200)
286 {
akmhoqueb6450b12014-04-24 00:01:03 -0500287 m_nlsr.getConfParameter().setLsaRefreshTime(lrt);
akmhoque53353462014-04-22 08:43:45 -0500288 }
289 }
290 return 0;
291}
292
293int
akmhoqueb6450b12014-04-24 00:01:03 -0500294ConfFileProcessor::processConfCommandMaxFacesPerPrefix(string command)
akmhoque53353462014-04-22 08:43:45 -0500295{
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 {
akmhoqueb6450b12014-04-24 00:01:03 -0500307 m_nlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
akmhoque53353462014-04-22 08:43:45 -0500308 }
309 }
310 return 0;
311}
312
313int
akmhoqueb6450b12014-04-24 00:01:03 -0500314ConfFileProcessor::processConfCommandTunnelType(string command)
akmhoque53353462014-04-22 08:43:45 -0500315{
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 {
akmhoqueb6450b12014-04-24 00:01:03 -0500324 m_nlsr.getConfParameter().setTunnelType(1);
akmhoque53353462014-04-22 08:43:45 -0500325 }
326 else if (command == "udp" || command == "UDP")
327 {
akmhoqueb6450b12014-04-24 00:01:03 -0500328 m_nlsr.getConfParameter().setTunnelType(0);
akmhoque53353462014-04-22 08:43:45 -0500329 }
330 else
331 {
332 cerr << " Wrong command format ! [tunnel-type tcp/udp]!" << endl;
333 }
334 }
335 return 0;
336}
337
338int
akmhoqueb6450b12014-04-24 00:01:03 -0500339ConfFileProcessor::processConfCommandChronosyncSyncPrefix(string command)
akmhoque53353462014-04-22 08:43:45 -0500340{
341 if (command.empty())
342 {
343 cerr << " Wrong command format ! [chronosync-sync-prefix name/prefix]!" << endl;
344 }
345 else
346 {
akmhoqueb6450b12014-04-24 00:01:03 -0500347 m_nlsr.getConfParameter().setChronosyncSyncPrefix(command);
akmhoque53353462014-04-22 08:43:45 -0500348 }
349 return 0;
350}
351
352
353int
akmhoqueb6450b12014-04-24 00:01:03 -0500354ConfFileProcessor::processConfCommandLogDir(string command)
akmhoque53353462014-04-22 08:43:45 -0500355{
356 if (command.empty())
357 {
358 cerr << " Wrong command format ! [log-dir /path/to/log/dir]!" << endl;
359 }
360 else
361 {
akmhoqueb6450b12014-04-24 00:01:03 -0500362 m_nlsr.getConfParameter().setLogDir(command);
akmhoque53353462014-04-22 08:43:45 -0500363 }
364 return 0;
365}
366
367int
akmhoqueb6450b12014-04-24 00:01:03 -0500368ConfFileProcessor::processConfCommandCertDir(string command)
akmhoque53353462014-04-22 08:43:45 -0500369{
370 if (command.empty())
371 {
372 cerr << " Wrong command format ! [cert-dir /path/to/cert/dir]!" << endl;
373 }
374 else
375 {
akmhoqueb6450b12014-04-24 00:01:03 -0500376 m_nlsr.getConfParameter().setCertDir(command);
akmhoque53353462014-04-22 08:43:45 -0500377 }
378 return 0;
379}
380
381int
akmhoqueb6450b12014-04-24 00:01:03 -0500382ConfFileProcessor::processConfCommandDebugging(string command)
akmhoque53353462014-04-22 08:43:45 -0500383{
384 if (command.empty())
385 {
386 cerr << " Wrong command format ! [debugging on/of]!" << endl;
387 }
388 else
389 {
390 if (command == "on" || command == "ON")
391 {
akmhoqueb6450b12014-04-24 00:01:03 -0500392 m_nlsr.getConfParameter().setDebugging(1);
akmhoque53353462014-04-22 08:43:45 -0500393 }
394 else if (command == "off" || command == "off")
395 {
akmhoqueb6450b12014-04-24 00:01:03 -0500396 m_nlsr.getConfParameter().setDebugging(0);
akmhoque53353462014-04-22 08:43:45 -0500397 }
398 else
399 {
400 cerr << " Wrong command format ! [debugging on/off]!" << endl;
401 }
402 }
403 return 0;
404}
405
406int
akmhoqueb6450b12014-04-24 00:01:03 -0500407ConfFileProcessor::processConfCommandDetailedLogging(string command)
akmhoque53353462014-04-22 08:43:45 -0500408{
409 if (command.empty())
410 {
411 cerr << " Wrong command format ! [detailed-logging on/off]!" << endl;
412 }
413 else
414 {
415 if (command == "on" || command == "ON")
416 {
akmhoqueb6450b12014-04-24 00:01:03 -0500417 m_nlsr.getConfParameter().setDetailedLogging(1);
akmhoque53353462014-04-22 08:43:45 -0500418 }
419 else if (command == "off" || command == "off")
420 {
akmhoqueb6450b12014-04-24 00:01:03 -0500421 m_nlsr.getConfParameter().setDetailedLogging(0);
akmhoque53353462014-04-22 08:43:45 -0500422 }
423 else
424 {
425 cerr << " Wrong command format ! [detailed-logging on/off]!" << endl;
426 }
427 }
428 return 0;
429}
430
431int
akmhoqueb6450b12014-04-24 00:01:03 -0500432ConfFileProcessor::processConfCommandIsHyperbolicCalc(string command)
akmhoque53353462014-04-22 08:43:45 -0500433{
434 if (command.empty())
435 {
436 cerr << " Wrong command format ! [hyperbolic-routing on/off/dry-run]!" << endl;
437 }
438 else
439 {
440 if (command == "on" || command == "ON")
441 {
akmhoqueb6450b12014-04-24 00:01:03 -0500442 m_nlsr.getConfParameter().setIsHyperbolicCalc(1);
akmhoque53353462014-04-22 08:43:45 -0500443 }
444 else if (command == "dry-run" || command == "DRY-RUN")
445 {
akmhoqueb6450b12014-04-24 00:01:03 -0500446 m_nlsr.getConfParameter().setIsHyperbolicCalc(2);
akmhoque53353462014-04-22 08:43:45 -0500447 }
448 else if (command == "off" || command == "off")
449 {
akmhoqueb6450b12014-04-24 00:01:03 -0500450 m_nlsr.getConfParameter().setIsHyperbolicCalc(0);
akmhoque53353462014-04-22 08:43:45 -0500451 }
452 else
453 {
454 cerr << " Wrong command format ! [hyperbolic-routing on/off/dry-run]!" << endl;
455 }
456 }
457 return 0;
458}
459
460int
akmhoqueb6450b12014-04-24 00:01:03 -0500461ConfFileProcessor::processConfCommandHyperbolicCordinate(string command)
akmhoque53353462014-04-22 08:43:45 -0500462{
463 if (command.empty())
464 {
465 cerr << " Wrong command format ! [hyperbolic-cordinate r 0]!" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500466 if (m_nlsr.getConfParameter().getIsHyperbolicCalc() > 0)
akmhoque53353462014-04-22 08:43:45 -0500467 {
468 return -1;
469 }
470 }
471 else
472 {
473 Tokenizer nt(command, " ");
474 stringstream ssr(nt.getFirstToken().c_str());
475 stringstream sst(nt.getRestOfLine().c_str());
476 double r, theta;
477 ssr >> r;
478 sst >> theta;
akmhoqueb6450b12014-04-24 00:01:03 -0500479 m_nlsr.getConfParameter().setCorR(r);
480 m_nlsr.getConfParameter().setCorTheta(theta);
akmhoque53353462014-04-22 08:43:45 -0500481 }
482 return 0;
483}
484
485
486int
akmhoqueb6450b12014-04-24 00:01:03 -0500487ConfFileProcessor::processConfCommandNdnNeighbor(string command)
akmhoque53353462014-04-22 08:43:45 -0500488{
489 if (command.empty())
490 {
491 cerr << " Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!" << endl;
492 }
493 else
494 {
495 Tokenizer nt(command, " ");
496 if (nt.getRestOfLine().empty())
497 {
498 cerr << " Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!" << endl;
499 return 0;
500 }
501 else
502 {
503 stringstream sst(nt.getRestOfLine().c_str());
504 int faceId;
505 sst >> faceId;
506 Adjacent adj(nt.getFirstToken(), faceId, 0.0, 0, 0);
akmhoqueb6450b12014-04-24 00:01:03 -0500507 m_nlsr.getAdl().insert(adj);
akmhoque53353462014-04-22 08:43:45 -0500508 }
509 }
510 return 0;
511}
512
513int
akmhoqueb6450b12014-04-24 00:01:03 -0500514ConfFileProcessor::processConfCommandNdnName(string command)
akmhoque53353462014-04-22 08:43:45 -0500515{
516 if (command.empty())
517 {
518 cerr << " Wrong command format ! [ndnname name/prefix]!" << endl;
519 }
520 else
521 {
akmhoqueb6450b12014-04-24 00:01:03 -0500522 m_nlsr.getNpl().insert(command);
akmhoque53353462014-04-22 08:43:45 -0500523 }
524 return 0;
525}
526
527
528int
akmhoqueb6450b12014-04-24 00:01:03 -0500529ConfFileProcessor::processConfCommandLinkCost(string command)
akmhoque53353462014-04-22 08:43:45 -0500530{
531 if (command.empty())
532 {
533 cerr << " Wrong command format ! [link-cost nbr/name cost]!" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500534 if (m_nlsr.getConfParameter().getIsHyperbolicCalc() > 0)
akmhoque53353462014-04-22 08:43:45 -0500535 {
536 return -1;
537 }
538 }
539 else
540 {
541 Tokenizer nt(command, " ");
542 stringstream sst(nt.getRestOfLine().c_str());
543 double cost;
544 sst >> cost;
akmhoqueb6450b12014-04-24 00:01:03 -0500545 m_nlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(), cost);
akmhoque53353462014-04-22 08:43:45 -0500546 }
547 return 0;
548}
549
550} //namespace nlsr
551