blob: 691a2852480a9d64a0fb77a2f7abd9a82fbdc692 [file] [log] [blame]
akmhoque87347a32014-01-31 11:00:44 -06001#include<iostream>
2#include<fstream>
3#include<string>
4#include<cstdlib>
5#include <sstream>
6
akmhoque204e7542014-01-31 16:08:25 -06007#include "nlsr_conf_processor.hpp"
8#include "nlsr_conf_param.hpp"
akmhoque87347a32014-01-31 11:00:44 -06009#include "nlsr_tokenizer.hpp"
akmhoque204e7542014-01-31 16:08:25 -060010#include "nlsr_adjacent.hpp"
akmhoque87347a32014-01-31 11:00:44 -060011
12
13using namespace std;
14
15int
16ConfFileProcessor::processConfFile(nlsr& pnlsr){
17 int ret=0;
18
19 if ( !confFileName.empty()){
20 std::ifstream inputFile(confFileName.c_str());
21 for( string line; getline( inputFile, line ); ){
22 if (!line.empty() ){
23 if(line[0]!= '#' && line[0]!='!'){
24 ret=processConfCommand(pnlsr, line);
25 if( ret == -1 ){
26 break;
27 }
28 }
29 }
30 }
31 }
32
33 return ret;
34}
35
36
37int
38ConfFileProcessor::processConfCommand(nlsr& pnlsr, string command){
39 int ret=0;
40 nlsrTokenizer nt(command," ");
41 if( (nt.getFirstToken() == "network")){
42 ret=processConfCommandNetwork(pnlsr,nt.getRestOfLine());
43 }
44 else if( (nt.getFirstToken() == "site-name")){
45 ret=processConfCommandSiteName(pnlsr,nt.getRestOfLine());
46 }
47 else if ( (nt.getFirstToken() == "router-name")){
48 ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine());
49 }
50 else if( (nt.getFirstToken() == "ndnneighbor") ){
51 ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine());
52 }
53 else if( (nt.getFirstToken() == "link-cost")){
54 ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine());
55 }
56 else if( (nt.getFirstToken() == "ndnname") ){
57 ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine());
58 }
59 else if( (nt.getFirstToken() == "interest-retry-num")){
60 processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine());
61 }
62 else if( (nt.getFirstToken() == "interest-resend-time")){
63 processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine());
64 }
65 else if( (nt.getFirstToken() == "lsa-refresh-time")){
66 processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine());
67 }
68 else if( (nt.getFirstToken() == "max-faces-per-prefix")){
69 processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine());
70 }
71 else if( (nt.getFirstToken() == "logdir")){
72 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
73 }
74 else if( (nt.getFirstToken() == "detailed-logging") ){
75 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
76 }
77 else if( (nt.getFirstToken() == "debugging") ){
78 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
79 }
80 else if( (nt.getFirstToken() == "chronosync-sync-prefix") ){
81 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
82 }
83 else if( (nt.getFirstToken() == "hyperbolic-cordinate") ){
84 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
85 }
86 else if( (nt.getFirstToken() == "hyperbolic-routing")){
87 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
88 }
89 else if( (nt.getFirstToken() == "tunnel-type")){
90 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
91 }
92 else {
93 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
94 }
95
96 return ret;
97}
98
99int
100ConfFileProcessor::processConfCommandNetwork(nlsr& pnlsr, string command){
101 if(command.empty() ){
102 cerr <<" Network can not be null or empty :( !"<<endl;
103 return -1;
104 }else{
105 if(command[command.size()-1] == '/' ){
106 command.erase(command.size() - 1);
107 }
108 if(command[0] == '/' ){
109 command.erase(0,1);
110 }
akmhoquea8cd6b92014-01-31 20:13:26 -0600111 pnlsr.getConfParameter().setNetwork(command);
akmhoque87347a32014-01-31 11:00:44 -0600112 }
113 return 0;
114}
115
116int
117ConfFileProcessor::processConfCommandSiteName(nlsr& pnlsr, string command){
118 if(command.empty() ){
119 cerr <<"Site name can not be null or empty :( !"<<endl;
120 return -1;
121 }else{
122 if(command[command.size()-1] == '/' ){
123 command.erase(command.size() - 1);
124 }
125 if(command[0] == '/' ){
126 command.erase(0,1);
127 }
akmhoquea8cd6b92014-01-31 20:13:26 -0600128 pnlsr.getConfParameter().setSiteName(command);
akmhoque87347a32014-01-31 11:00:44 -0600129 }
130 return 0;
131}
132
133int
134ConfFileProcessor::processConfCommandRouterName(nlsr& pnlsr, string command){
135 if(command.empty() ){
136 cerr <<" Router name can not be null or empty :( !"<<endl;
137 return -1;
138 }else{
139 if(command[command.size()-1] == '/' ){
140 command.erase(command.size() - 1);
141 }
142 if(command[0] == '/' ){
143 command.erase(0,1);
144 }
akmhoquea8cd6b92014-01-31 20:13:26 -0600145 pnlsr.getConfParameter().setRouterName(command);
akmhoque87347a32014-01-31 11:00:44 -0600146 }
147 return 0;
148}
149
150int
151ConfFileProcessor::processConfCommandInterestRetryNumber(nlsr& pnlsr, string command){
152 if(command.empty() ){
153 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
154 }else{
155 int irn;
156 stringstream ss(command.c_str());
157 ss>>irn;
158 if ( irn >=1 && irn <=5){
akmhoquea8cd6b92014-01-31 20:13:26 -0600159 pnlsr.getConfParameter().setInterestRetryNumber(irn);
akmhoque87347a32014-01-31 11:00:44 -0600160 }
161 }
162 return 0;
163}
164
165int
166ConfFileProcessor::processConfCommandInterestResendTime(nlsr& pnlsr, string command){
167 if(command.empty() ){
168 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
169 }else{
170 int irt;
171 stringstream ss(command.c_str());
172 ss>>irt;
173 if( irt>=1 && irt <=20){
akmhoquea8cd6b92014-01-31 20:13:26 -0600174 pnlsr.getConfParameter().setInterestResendTime(irt);
akmhoque87347a32014-01-31 11:00:44 -0600175 }
176 }
177 return 0;
178}
179
180int
181ConfFileProcessor::processConfCommandLsaRefreshTime(nlsr& pnlsr, string command){
182 if(command.empty() ){
183 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
184 }else{
185 int lrt;
186 stringstream ss(command.c_str());
187 ss>>lrt;
188 if ( lrt>= 240 && lrt<=7200){
akmhoquea8cd6b92014-01-31 20:13:26 -0600189 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
akmhoque87347a32014-01-31 11:00:44 -0600190 }
191 }
192 return 0;
193}
194
195int
196ConfFileProcessor::processConfCommandMaxFacesPerPrefix(nlsr& pnlsr, string command){
197 if(command.empty() ){
198 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
199 }else{
200 int mfpp;
201 stringstream ss(command.c_str());
202 ss>>mfpp;
203 if ( mfpp>=0 && mfpp<=60){
akmhoquea8cd6b92014-01-31 20:13:26 -0600204 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
akmhoque87347a32014-01-31 11:00:44 -0600205 }
206 }
207 return 0;
208}
209
210int
211ConfFileProcessor::processConfCommandTunnelType(nlsr& pnlsr, string command){
212 if(command.empty() ){
213 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
214 }else{
215 if(command == "tcp" || command == "TCP" ){
akmhoquea8cd6b92014-01-31 20:13:26 -0600216 pnlsr.getConfParameter().setTunnelType(1);
akmhoque87347a32014-01-31 11:00:44 -0600217 }
218 else if(command == "udp" || command == "UDP"){
akmhoquea8cd6b92014-01-31 20:13:26 -0600219 pnlsr.getConfParameter().setTunnelType(0);
akmhoque87347a32014-01-31 11:00:44 -0600220 }else{
221 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
222 }
223 }
224 return 0;
225}
226
227int
228ConfFileProcessor::processConfCommandChronosyncSyncPrefix(nlsr& pnlsr,
229 string command){
230 if(command.empty() ){
231 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
232 }else{
akmhoquea8cd6b92014-01-31 20:13:26 -0600233 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
akmhoque87347a32014-01-31 11:00:44 -0600234 }
235 return 0;
236}
237
238
239int
240ConfFileProcessor::processConfCommandLogDir(nlsr& pnlsr, string command){
241 if(command.empty() ){
242 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
243 }else{
akmhoquea8cd6b92014-01-31 20:13:26 -0600244 pnlsr.getConfParameter().setLogDir(command);
akmhoque87347a32014-01-31 11:00:44 -0600245 }
246 return 0;
247}
248
249int
250ConfFileProcessor::processConfCommandDebugging(nlsr& pnlsr, string command){
251 if(command.empty() ){
252 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
253 }else{
254 if(command == "on" || command == "ON" ){
akmhoquea8cd6b92014-01-31 20:13:26 -0600255 pnlsr.getConfParameter().setDebugging(1);
akmhoque87347a32014-01-31 11:00:44 -0600256 }
257 else if(command == "off" || command == "off"){
akmhoquea8cd6b92014-01-31 20:13:26 -0600258 pnlsr.getConfParameter().setDebugging(0);
akmhoque87347a32014-01-31 11:00:44 -0600259 }else{
260 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
261 }
262 }
263 return 0;
264}
265
266int
267ConfFileProcessor::processConfCommandDetailedLogging(nlsr& pnlsr, string command){
268 if(command.empty() ){
269 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
270 }else{
271 if(command == "on" || command == "ON" ){
akmhoquea8cd6b92014-01-31 20:13:26 -0600272 pnlsr.getConfParameter().setDetailedLogging(1);
akmhoque87347a32014-01-31 11:00:44 -0600273 }
274 else if(command == "off" || command == "off"){
akmhoquea8cd6b92014-01-31 20:13:26 -0600275 pnlsr.getConfParameter().setDetailedLogging(0);
akmhoque87347a32014-01-31 11:00:44 -0600276 }else{
277 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
278 }
279 }
280 return 0;
281}
282
283int
284ConfFileProcessor::processConfCommandIsHyperbolicCalc(nlsr& pnlsr, string command){
285 if(command.empty() ){
286 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
287 }else{
288 if(command == "on" || command == "ON" ){
akmhoquea8cd6b92014-01-31 20:13:26 -0600289 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
akmhoque87347a32014-01-31 11:00:44 -0600290 }
291 else if(command == "dry-run" || command == "DRY-RUN"){
akmhoquea8cd6b92014-01-31 20:13:26 -0600292 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
akmhoque87347a32014-01-31 11:00:44 -0600293 }
294 else if(command == "off" || command == "off"){
akmhoquea8cd6b92014-01-31 20:13:26 -0600295 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
akmhoque87347a32014-01-31 11:00:44 -0600296 }else{
297 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
298 }
299 }
300 return 0;
301}
302
303int
304ConfFileProcessor::processConfCommandHyperbolicCordinate(nlsr& pnlsr, string command){
305 if(command.empty() ){
306 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
akmhoquea8cd6b92014-01-31 20:13:26 -0600307 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){
akmhoque87347a32014-01-31 11:00:44 -0600308 return -1;
309 }
310 }else{
311 nlsrTokenizer nt(command," ");
312 stringstream ssr(nt.getFirstToken().c_str());
313 stringstream sst(nt.getRestOfLine().c_str());
314
315 double r,theta;
316 ssr>>r;
317 sst>>theta;
318
akmhoquea8cd6b92014-01-31 20:13:26 -0600319 pnlsr.getConfParameter().setCorR(r);
320 pnlsr.getConfParameter().setCorTheta(theta);
akmhoque87347a32014-01-31 11:00:44 -0600321 }
322 return 0;
323}
324
325
326int
327ConfFileProcessor::processConfCommandNdnNeighbor(nlsr& pnlsr, string command){
328 if(command.empty() ){
329 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/]!"<<endl;
330 }else{
331 nlsrTokenizer nt(command," ");
332 Adjacent adj(nt.getFirstToken(),0,0.0,0,0);
akmhoquea8cd6b92014-01-31 20:13:26 -0600333 pnlsr.getAdl().insert(adj);
akmhoque87347a32014-01-31 11:00:44 -0600334 }
335 return 0;
336}
337
338int
339ConfFileProcessor::processConfCommandNdnName(nlsr& pnlsr, string command){
340 if(command.empty() ){
341 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
342 }else{
akmhoquea8cd6b92014-01-31 20:13:26 -0600343 pnlsr.getNpl().insertIntoNpl(command);
akmhoque87347a32014-01-31 11:00:44 -0600344 }
345 return 0;
346}
347
348
349int
350ConfFileProcessor::processConfCommandLinkCost(nlsr& pnlsr, string command){
351 if(command.empty() ){
352 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
akmhoquea8cd6b92014-01-31 20:13:26 -0600353 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){
akmhoque87347a32014-01-31 11:00:44 -0600354 return -1;
355 }
356 }else{
357 nlsrTokenizer nt(command," ");
358 stringstream sst(nt.getRestOfLine().c_str());
359
360 double cost;
361 sst>>cost;
362
akmhoquea8cd6b92014-01-31 20:13:26 -0600363 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
akmhoque87347a32014-01-31 11:00:44 -0600364 }
365 return 0;
366}
367