blob: e25192ed0ec0ef39f6ef577c077726e8a2a5938b [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
akmhoqueb1710aa2014-02-19 17:13:36 -060013namespace nlsr {
14
akmhoque298385a2014-02-13 14:13:09 -060015using namespace std;
16
17int
akmhoque1a481092014-02-19 16:34:22 -060018ConfFileProcessor::processConfFile(Nlsr& pnlsr){
akmhoque298385a2014-02-13 14:13:09 -060019 int ret=0;
20
21 if ( !confFileName.empty()){
22 std::ifstream inputFile(confFileName.c_str());
23 if ( inputFile.is_open()){
24 for( string line; getline( inputFile, line ); ){
25 if (!line.empty() ){
26 if(line[0]!= '#' && line[0]!='!'){
27 ret=processConfCommand(pnlsr, line);
28 if( ret == -1 ){
29 break;
30 }
31 }
32 }
33 }
34 }
35 else{
36 std::cerr <<"Configuration file: ("<<confFileName<<") does not exist :(";
37 std::cerr <<endl;
38 ret=-1;
39 }
40 }
41
42 return ret;
43}
44
45
46int
akmhoque1a481092014-02-19 16:34:22 -060047ConfFileProcessor::processConfCommand(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -060048 int ret=0;
49 nlsrTokenizer nt(command," ");
50 if( (nt.getFirstToken() == "network")){
51 ret=processConfCommandNetwork(pnlsr,nt.getRestOfLine());
52 }
53 else if( (nt.getFirstToken() == "site-name")){
54 ret=processConfCommandSiteName(pnlsr,nt.getRestOfLine());
55 }
56 else if ( (nt.getFirstToken() == "router-name")){
57 ret=processConfCommandRouterName(pnlsr,nt.getRestOfLine());
58 }
59 else if( (nt.getFirstToken() == "ndnneighbor") ){
60 ret=processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine());
61 }
62 else if( (nt.getFirstToken() == "link-cost")){
63 ret=processConfCommandLinkCost(pnlsr, nt.getRestOfLine());
64 }
65 else if( (nt.getFirstToken() == "ndnname") ){
66 ret=processConfCommandNdnName(pnlsr, nt.getRestOfLine());
67 }
68 else if( (nt.getFirstToken() == "interest-retry-num")){
69 processConfCommandInterestRetryNumber(pnlsr,nt.getRestOfLine());
70 }
71 else if( (nt.getFirstToken() == "interest-resend-time")){
72 processConfCommandInterestResendTime(pnlsr,nt.getRestOfLine());
73 }
74 else if( (nt.getFirstToken() == "lsa-refresh-time")){
75 processConfCommandLsaRefreshTime(pnlsr,nt.getRestOfLine());
76 }
77 else if( (nt.getFirstToken() == "max-faces-per-prefix")){
78 processConfCommandMaxFacesPerPrefix(pnlsr,nt.getRestOfLine());
79 }
80 else if( (nt.getFirstToken() == "logdir")){
81 processConfCommandLogDir(pnlsr,nt.getRestOfLine());
82 }
83 else if( (nt.getFirstToken() == "detailed-logging") ){
84 processConfCommandDetailedLogging(pnlsr,nt.getRestOfLine());
85 }
86 else if( (nt.getFirstToken() == "debugging") ){
87 processConfCommandDebugging(pnlsr,nt.getRestOfLine());
88 }
89 else if( (nt.getFirstToken() == "chronosync-sync-prefix") ){
90 processConfCommandChronosyncSyncPrefix(pnlsr,nt.getRestOfLine());
91 }
92 else if( (nt.getFirstToken() == "hyperbolic-cordinate") ){
93 processConfCommandHyperbolicCordinate(pnlsr,nt.getRestOfLine());
94 }
95 else if( (nt.getFirstToken() == "hyperbolic-routing")){
96 processConfCommandIsHyperbolicCalc(pnlsr,nt.getRestOfLine());
97 }
98 else if( (nt.getFirstToken() == "tunnel-type")){
99 processConfCommandTunnelType(pnlsr,nt.getRestOfLine());
100 }
101 else {
102 cout << "Wrong configuration Command: "<< nt.getFirstToken()<<endl;
103 }
104
105 return ret;
106}
107
108int
akmhoque1a481092014-02-19 16:34:22 -0600109ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600110 if(command.empty() ){
111 cerr <<" Network can not be null or empty :( !"<<endl;
112 return -1;
113 }else{
114 if(command[command.size()-1] == '/' ){
115 command.erase(command.size() - 1);
116 }
117 if(command[0] == '/' ){
118 command.erase(0,1);
119 }
120 pnlsr.getConfParameter().setNetwork(command);
121 }
122 return 0;
123}
124
125int
akmhoque1a481092014-02-19 16:34:22 -0600126ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600127 if(command.empty() ){
128 cerr <<"Site name can not be null or empty :( !"<<endl;
129 return -1;
130 }else{
131 if(command[command.size()-1] == '/' ){
132 command.erase(command.size() - 1);
133 }
134 if(command[0] == '/' ){
135 command.erase(0,1);
136 }
137 pnlsr.getConfParameter().setSiteName(command);
138 }
139 return 0;
140}
141
142int
akmhoque1a481092014-02-19 16:34:22 -0600143ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600144 if(command.empty() ){
145 cerr <<" Router name can not be null or empty :( !"<<endl;
146 return -1;
147 }else{
148 if(command[command.size()-1] == '/' ){
149 command.erase(command.size() - 1);
150 }
151 if(command[0] == '/' ){
152 command.erase(0,1);
153 }
154 pnlsr.getConfParameter().setRouterName(command);
155 }
156 return 0;
157}
158
159int
akmhoque1a481092014-02-19 16:34:22 -0600160ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600161 if(command.empty() ){
162 cerr <<" Wrong command format ! [interest-retry-num n]"<<endl;
163 }else{
164 int irn;
165 stringstream ss(command.c_str());
166 ss>>irn;
167 if ( irn >=1 && irn <=5){
168 pnlsr.getConfParameter().setInterestRetryNumber(irn);
169 }
170 }
171 return 0;
172}
173
174int
akmhoque1a481092014-02-19 16:34:22 -0600175ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600176 if(command.empty() ){
177 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
178 }else{
179 int irt;
180 stringstream ss(command.c_str());
181 ss>>irt;
182 if( irt>=1 && irt <=20){
183 pnlsr.getConfParameter().setInterestResendTime(irt);
184 }
185 }
186 return 0;
187}
188
189int
akmhoque1a481092014-02-19 16:34:22 -0600190ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600191 if(command.empty() ){
192 cerr <<" Wrong command format ! [interest-resend-time s]"<<endl;
193 }else{
194 int lrt;
195 stringstream ss(command.c_str());
196 ss>>lrt;
197 if ( lrt>= 240 && lrt<=7200){
198 pnlsr.getConfParameter().setLsaRefreshTime(lrt);
199 }
200 }
201 return 0;
202}
203
204int
akmhoque1a481092014-02-19 16:34:22 -0600205ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600206 if(command.empty() ){
207 cerr <<" Wrong command format ! [max-faces-per-prefix n]"<<endl;
208 }else{
209 int mfpp;
210 stringstream ss(command.c_str());
211 ss>>mfpp;
212 if ( mfpp>=0 && mfpp<=60){
213 pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp);
214 }
215 }
216 return 0;
217}
218
219int
akmhoque1a481092014-02-19 16:34:22 -0600220ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600221 if(command.empty() ){
222 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
223 }else{
224 if(command == "tcp" || command == "TCP" ){
225 pnlsr.getConfParameter().setTunnelType(1);
226 }
227 else if(command == "udp" || command == "UDP"){
228 pnlsr.getConfParameter().setTunnelType(0);
229 }else{
230 cerr <<" Wrong command format ! [tunnel-type tcp/udp]!"<<endl;
231 }
232 }
233 return 0;
234}
235
236int
akmhoque1a481092014-02-19 16:34:22 -0600237ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr,
akmhoque298385a2014-02-13 14:13:09 -0600238 string command){
239 if(command.empty() ){
240 cerr <<" Wrong command format ! [chronosync-sync-prefix name/prefix]!"<<endl;
241 }else{
242 pnlsr.getConfParameter().setChronosyncSyncPrefix(command);
243 }
244 return 0;
245}
246
247
248int
akmhoque1a481092014-02-19 16:34:22 -0600249ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600250 if(command.empty() ){
251 cerr <<" Wrong command format ! [log-dir /path/to/log/dir]!"<<endl;
252 }else{
253 pnlsr.getConfParameter().setLogDir(command);
254 }
255 return 0;
256}
257
258int
akmhoque1a481092014-02-19 16:34:22 -0600259ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600260 if(command.empty() ){
261 cerr <<" Wrong command format ! [debugging on/of]!"<<endl;
262 }else{
263 if(command == "on" || command == "ON" ){
264 pnlsr.getConfParameter().setDebugging(1);
265 }
266 else if(command == "off" || command == "off"){
267 pnlsr.getConfParameter().setDebugging(0);
268 }else{
269 cerr <<" Wrong command format ! [debugging on/off]!"<<endl;
270 }
271 }
272 return 0;
273}
274
275int
akmhoque1a481092014-02-19 16:34:22 -0600276ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600277 if(command.empty() ){
278 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
279 }else{
280 if(command == "on" || command == "ON" ){
281 pnlsr.getConfParameter().setDetailedLogging(1);
282 }
283 else if(command == "off" || command == "off"){
284 pnlsr.getConfParameter().setDetailedLogging(0);
285 }else{
286 cerr <<" Wrong command format ! [detailed-logging on/off]!"<<endl;
287 }
288 }
289 return 0;
290}
291
292int
akmhoque1a481092014-02-19 16:34:22 -0600293ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600294 if(command.empty() ){
295 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
296 }else{
297 if(command == "on" || command == "ON" ){
298 pnlsr.getConfParameter().setIsHyperbolicCalc(1);
299 }
300 else if(command == "dry-run" || command == "DRY-RUN"){
301 pnlsr.getConfParameter().setIsHyperbolicCalc(2);
302 }
303 else if(command == "off" || command == "off"){
304 pnlsr.getConfParameter().setIsHyperbolicCalc(0);
305 }else{
306 cerr <<" Wrong command format ! [hyperbolic-routing on/off/dry-run]!"<<endl;
307 }
308 }
309 return 0;
310}
311
312int
akmhoque1a481092014-02-19 16:34:22 -0600313ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600314 if(command.empty() ){
315 cerr <<" Wrong command format ! [hyperbolic-cordinate r 0]!"<<endl;
316 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){
317 return -1;
318 }
319 }else{
320 nlsrTokenizer nt(command," ");
321 stringstream ssr(nt.getFirstToken().c_str());
322 stringstream sst(nt.getRestOfLine().c_str());
323
324 double r,theta;
325 ssr>>r;
326 sst>>theta;
327
328 pnlsr.getConfParameter().setCorR(r);
329 pnlsr.getConfParameter().setCorTheta(theta);
330 }
331 return 0;
332}
333
334
335int
akmhoque1a481092014-02-19 16:34:22 -0600336ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600337 if(command.empty() ){
338 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
339 }else{
340 nlsrTokenizer nt(command," ");
341 if( nt.getRestOfLine().empty())
342 {
343 cerr <<" Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!"<<endl;
344 return 0;
345 }
346 else
347 {
348 stringstream sst(nt.getRestOfLine().c_str());
349 int faceId;
350 sst>>faceId;
351 Adjacent adj(nt.getFirstToken(),faceId,0.0,0,0);
352 pnlsr.getAdl().insert(adj);
353 }
354 }
355 return 0;
356}
357
358int
akmhoque1a481092014-02-19 16:34:22 -0600359ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600360 if(command.empty() ){
361 cerr <<" Wrong command format ! [ndnname name/prefix]!"<<endl;
362 }else{
363 pnlsr.getNpl().insertIntoNpl(command);
364 }
365 return 0;
366}
367
368
369int
akmhoque1a481092014-02-19 16:34:22 -0600370ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command){
akmhoque298385a2014-02-13 14:13:09 -0600371 if(command.empty() ){
372 cerr <<" Wrong command format ! [link-cost nbr/name cost]!"<<endl;
373 if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0 ){
374 return -1;
375 }
376 }else{
377 nlsrTokenizer nt(command," ");
378 stringstream sst(nt.getRestOfLine().c_str());
379
380 double cost;
381 sst>>cost;
382
383 pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(),cost);
384 }
385 return 0;
386}
387
akmhoqueb1710aa2014-02-19 17:13:36 -0600388} //namespace nlsr
389