blob: e307cf66fd04371635a7a35d2956e891f51545a2 [file] [log] [blame]
TamasBakai9b780332019-02-15 08:38:16 +00001var http = require('http');
2var https = require('https');
3var ArgumentParser = require('argparse').ArgumentParser;
4var express = require('express');
5const stream = require('stream');
6var app = express();
maximessonb8b94ae2019-03-25 16:03:08 +00007var fs = require('fs');
BjornMagnussonXA42dcb262019-04-26 19:29:54 +00008const sleep = (milliseconds) => {
9 return new Promise(resolve => setTimeout(resolve, milliseconds))
10}
TamasBakai9b780332019-02-15 08:38:16 +000011var privateKey = fs.readFileSync('cert/private.key', 'utf8');
12var certificate = fs.readFileSync('cert/certificate.crt', 'utf8');
13var credentials = {key: privateKey, cert: certificate};
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000014
BjornMagnussonXAa79a0432019-07-17 08:26:50 +000015var feeds="1:A"; //Comma separated list of feedId:filePrefix. Default is feedId=1 and file prefix 'A'
16var feedNames=[];
17var filePrefixes=[];
18var feedIndexes=[];
19
20
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000021//For execution time calculation
22var startTime = Date.now();
23
24//Test case constants
25const tc_normal = "normal";
26const tc_none_published = "none_published";
27const tc_all_published = "all_published"
28const tc_10p_no_response = "10p_no_response";
29const tc_10first_no_response = "10first_no_response";
30const tc_100first_no_response = "100first_no_response";
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000031const tc_all_delay_1s = "all_delay_1s";
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000032const tc_all_delay_10s = "all_delay_10s";
33const tc_10p_delay_10s = "10p_delay_10s";
34const tc_10p_error_response = "10p_error_response";
35const tc_10first_error_response = "10first_error_response";
36const tc_100first_error_response = "100first_error_response";
37
TamasBakaifa357df2019-08-15 08:25:27 +000038var drr_sim_ip = 'drsim_redir'; //IP for redirect to DR redir sim. Can be changed by env DRR_SIM_IP
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000039
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000040//Counters
BjornMagnussonXAa79a0432019-07-17 08:26:50 +000041var ctr_publish_query = [];
42var ctr_publish_query_bad_file_prefix = [];
43var ctr_publish_query_published = [];
44var ctr_publish_query_not_published = [];
45var ctr_publish_req = [];
46var ctr_publish_req_bad_file_prefix = [];
47var ctr_publish_req_redirect = [];
48var ctr_publish_req_published = [];
49var ctr_double_publish = [];
50
51//db of published files
52var published=[];
TamasBakai9b780332019-02-15 08:38:16 +000053
54var parser = new ArgumentParser({
55 version: '0.0.1',
56 addHelp:true,
57 description: 'Datarouter simulator'
58 });
59
60parser.addArgument('--tc' , { help: 'TC $NoOfTc' } );
61parser.addArgument('--printtc' ,
62 {
63 help: 'Print complete usage help',
64 action: 'storeTrue'
65 }
66 );
67
68var args = parser.parseArgs();
69
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000070if (args.tc==tc_normal) {
71 console.log("TC: " + args.tc)
72
73} else if (args.tc==tc_none_published) {
74 console.log("TC: " + args.tc)
75
76} else if (args.tc==tc_all_published) {
77 console.log("TC: " + args.tc)
78
79} else if (args.tc==tc_10p_no_response) {
80 console.log("TC: " + args.tc)
81
82} else if (args.tc==tc_10first_no_response) {
83 console.log("TC: " + args.tc)
84
85} else if (args.tc==tc_100first_no_response) {
86 console.log("TC: " + args.tc)
87
BjornMagnussonXA42dcb262019-04-26 19:29:54 +000088} else if (args.tc==tc_all_delay_1s) {
89 console.log("TC: " + args.tc)
90
BjornMagnussonXAf4e18362019-04-10 13:04:08 +000091} else if (args.tc==tc_all_delay_10s) {
92 console.log("TC: " + args.tc)
93
94} else if (args.tc==tc_10p_delay_10s) {
95 console.log("TC: " + args.tc)
96
97} else if (args.tc==tc_10p_error_response) {
98 console.log("TC: " + args.tc)
99
100} else if (args.tc==tc_10first_error_response) {
101 console.log("TC: " + args.tc)
102
103} else if (args.tc==tc_100first_error_response) {
104 console.log("TC: " + args.tc)
105} else {
106 console.log("No TC specified, use: --tc <tc-id>");
107 process.exit(0);
TamasBakai9b780332019-02-15 08:38:16 +0000108}
109
110if (args.printtc) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000111 console.log("TC " + tc_normal + ": Normal case, query respone based on published files. Publish responde with ok/redirect depending on if file is published or not.");
112 console.log("TC " + tc_none_published + ": Query responde 'ok'. Publish respond with redirect.");
113 console.log("TC " + tc_all_published + ": Query respond with filename. Publish respond with 'ok'.");
114 console.log("TC " + tc_10p_no_response + ": 10% % no response for query and publish. Otherwise normal case.");
115 console.log("TC " + tc_10first_no_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
116 console.log("TC " + tc_100first_no_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000117 console.log("TC " + tc_all_delay_1s + ": All responses delayed 1s (both query and publish).");
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000118 console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s (both query and publish).");
119 console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, (both query and publish).");
120 console.log("TC " + tc_10p_error_response + ": 10% error response for query and publish. Otherwise normal case.");
121 console.log("TC " + tc_10first_error_response + ": 10 first queries and requests gives no response for query and publish. Otherwise normal case.");
122 console.log("TC " + tc_100first_error_response + ": 100 first queries and requests gives no response for query and publish. Otherwise normal case.");
123
TamasBakai9b780332019-02-15 08:38:16 +0000124 process.exit(0);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000125 }
126
TamasBakai9b780332019-02-15 08:38:16 +0000127
128var bodyParser = require('body-parser')
129app.use(bodyParser.urlencoded({ extended: false }))
130
131// parse application/json
132app.use(bodyParser.json())
133
134// parse application/vnd.api+json as json
135app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
136
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000137// parse some custom thing into a Buffer (to cater for 60MB files)
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000138//Removed-file data not used in this simulator
139//app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' }))
140
TamasBakai9b780332019-02-15 08:38:16 +0000141// parse an HTML body into a string
142app.use(bodyParser.text({ type: 'text/html' }))
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000143
144
145
146//Is alive function
TamasBakai9b780332019-02-15 08:38:16 +0000147app.get("/",function(req, res){
148 res.send("ok");
149})
maximessonb8b94ae2019-03-25 16:03:08 +0000150
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000151function toCommaList(ctrArray) {
152 var str="";
153 for(i=0;i<feedNames.length;i++) {
154 if (i!=0) {
155 str=str+",";
156 }
157 str=str+ctrArray[i];
158 }
159 return str;
160}
161
162function sumList(ctrArray) {
163 var tmp=0;
164 for(i=0;i<feedNames.length;i++) {
165 tmp=tmp+ctrArray[i];
166 }
167 return ""+tmp;
168}
169
170function sumListLength(ctrArray) {
171 var tmp=0;
172 for(i=0;i<feedNames.length;i++) {
173 tmp=tmp+ctrArray[i].length;
174 }
175 return ""+tmp;
176}
177
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000178//Counter readout
179app.get("/ctr_publish_query",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000180 res.send(""+sumList(ctr_publish_query));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000181})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000182app.get("/feeds/ctr_publish_query",function(req, res){
183 res.send(toCommaList(ctr_publish_query));
184})
185app.get("/ctr_publish_query/:feedId",function(req, res){
186 var feedId = req.params.feedId;
187 res.send(""+ctr_publish_query[feedIndexes[feedId]]);
188})
189
190app.get("/ctr_publish_query_bad_file_prefix",function(req, res){
191 res.send(""+sumList(ctr_publish_query_bad_file_prefix));
192})
193app.get("/feeds/ctr_publish_query_bad_file_prefix",function(req, res){
194 res.send(toCommaList(ctr_publish_query_bad_file_prefix));
195})
196app.get("/ctr_publish_query_bad_file_prefix/:feedId",function(req, res){
197 var feedId = req.params.feedId;
198 res.send(""+ctr_publish_query_bad_file_prefix[feedIndexes[feedId]]);
199})
200
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000201app.get("/ctr_publish_query_published",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000202 res.send(""+sumList(ctr_publish_query_published));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000203})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000204app.get("/feeds/ctr_publish_query_published",function(req, res){
205 res.send(toCommaList(ctr_publish_query_published));
206})
207app.get("/ctr_publish_query_published/:feedId",function(req, res){
208 var feedId = req.params.feedId;
209 res.send(""+ctr_publish_query_published[feedIndexes[feedId]]);
210})
211
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000212app.get("/ctr_publish_query_not_published",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000213 res.send(""+sumList(ctr_publish_query_not_published));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000214})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000215app.get("/feeds/ctr_publish_query_not_published",function(req, res){
216 res.send(toCommaList(ctr_publish_query_not_published));
217})
218app.get("/ctr_publish_query_not_published/:feedId",function(req, res){
219 var feedId = req.params.feedId;
220 res.send(""+ctr_publish_query_not_published[feedIndexes[feedId]]);
221})
222
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000223app.get("/ctr_publish_req",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000224 res.send(""+sumList(ctr_publish_req));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000225})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000226app.get("/feeds/ctr_publish_req",function(req, res){
227 res.send(toCommaList(ctr_publish_req));
228})
229app.get("/ctr_publish_req/:feedId",function(req, res){
230 var feedId = req.params.feedId;
231 res.send(""+ctr_publish_req[feedIndexes[feedId]]);
232})
233
234app.get("/ctr_publish_req_bad_file_prefix",function(req, res){
235 res.send(""+sumList(ctr_publish_req_bad_file_prefix));
236})
237app.get("/feeds/ctr_publish_req_bad_file_prefix",function(req, res){
238 res.send(toCommaList(ctr_publish_req_bad_file_prefix));
239})
240app.get("/ctr_publish_req_bad_file_prefix/:feedId",function(req, res){
241 var feedId = req.params.feedId;
242 res.send(""+ctr_publish_req_bad_file_prefix[feedIndexes[feedId]]);
243})
244
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000245app.get("/ctr_publish_req_redirect",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000246 res.send(""+sumList(ctr_publish_req_redirect));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000247})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000248app.get("/feeds/ctr_publish_req_redirect",function(req, res){
249 res.send(toCommaList(ctr_publish_req_redirect));
250})
251app.get("/ctr_publish_req_redirect/:feedId",function(req, res){
252 var feedId = req.params.feedId;
253 res.send(""+ctr_publish_req_redirect[feedIndexes[feedId]]);
254})
255
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000256app.get("/ctr_publish_req_published",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000257 res.send(""+sumList(ctr_publish_req_published));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000258})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000259app.get("/feeds/ctr_publish_req_published",function(req, res){
260 res.send(toCommaList(ctr_publish_req_published));
261})
262app.get("/ctr_publish_req_published/:feedId",function(req, res){
263 var feedId = req.params.feedId;
264 res.send(""+ctr_publish_req_published[feedIndexes[feedId]]);
265})
266
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000267app.get("/ctr_published_files",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000268 res.send(""+sumListLength(published));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000269})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000270app.get("/feeds/ctr_published_files",function(req, res){
271 var str="";
272 for(i=0;i<feedNames.length;i++) {
273 if (i!=0) {
274 str=str+",";
275 }
276 str=str+published[i].length;
277 }
278 res.send(str);
279})
280app.get("/ctr_published_files/:feedId",function(req, res){
281 var feedId = req.params.feedId;
282 res.send(""+published[feedIndexes[feedId]].length);
283})
284
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000285app.get("/tc_info",function(req, res){
286 res.send(args.tc);
287})
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000288app.get("/ctr_double_publish",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000289 res.send(""+sumList(ctr_double_publish));
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000290})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000291app.get("/feeds/ctr_double_publish",function(req, res){
292 var str="";
293 for(i=0;i<feedNames.length;i++) {
294 if (i!=0) {
295 str=str+",";
296 }
297 str=str+ctr_double_publish[i];
298 }
299 res.send(str);
300})
301app.get("/ctr_double_publish/:feedId",function(req, res){
302 var feedId = req.params.feedId;
303 res.send(""+ctr_double_publish[feedIndexes[feedId]]);
304})
305
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000306function fmtMSS(s){
307 return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff in mm:ss
308}
309app.get("/execution_time",function(req, res){
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000310 var diff = fmtMSS(Math.floor((Date.now()-startTime)/1000));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000311 res.send(""+diff);
TamasBakai9b780332019-02-15 08:38:16 +0000312})
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000313app.get("/feeds",function(req, res){
314 res.send(feeds);
315})
maximessonb8b94ae2019-03-25 16:03:08 +0000316
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000317function filenameStartsWith(fileName, feedIndex) {
318 for(i=0;i<filePrefixes[feedIndex].length;i++) {
319 var prefix=filePrefixes[feedIndex][i];
320 if (fileName.startsWith(prefix)) {
321 return true;
322 }
323 }
324 return false;
325}
maximessonb8b94ae2019-03-25 16:03:08 +0000326
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000327app.get('/feedlog/:feedId',function(req, res){
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000328 console.log("url:"+req.url);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000329 var feedId = req.params.feedId;
330 ctr_publish_query[feedIndexes[feedId]]++;
maximessonb8b94ae2019-03-25 16:03:08 +0000331 var filename = req.query.filename;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000332 if (!filenameStartsWith(filename, feedIndexes[feedId])) {
333 ctr_publish_query_bad_file_prefix[feedIndexes[feedId]]++;
334 }
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000335 console.log(filename);
336 var qtype = req.query.type;
maximessonb8b94ae2019-03-25 16:03:08 +0000337 if(typeof(filename) == 'undefined'){
338 res.status(400).send({error: 'No filename provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000339 return;
340 } else if(typeof(qtype) == 'undefined'){
maximessonb8b94ae2019-03-25 16:03:08 +0000341 res.status(400).send({error: 'No type provided.'});
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000342 return;
343 }
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000344 var ctr = ctr_publish_query[feedIndexes[feedId]];
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000345 //Ugly fix, plus signs replaces with spaces in query params....need to put them back
346 filename = filename.replace(/ /g,"+");
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000347
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000348 var sleeptime=0;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000349 if (args.tc==tc_normal) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000350 sleeptime=0;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000351 } else if (args.tc==tc_10p_no_response && (ctr%10) == 0) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000352 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000353 } else if (args.tc==tc_10first_no_response && ctr<11) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000354 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000355 } else if (args.tc==tc_100first_no_response && ctr<101) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000356 return;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000357 } else if (args.tc==tc_all_delay_1s) {
358 sleeptime=1000;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000359 } else if (args.tc==tc_all_delay_10s) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000360 sleeptime=10000;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000361 } else if (args.tc==tc_10p_delay_10s && (ctr%10) == 0) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000362 sleeptime=10000;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000363 } else if (args.tc==tc_10p_error_response && (ctr%10) == 0) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000364 res.send(400);
365 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000366 } else if (args.tc==tc_10first_error_response && ctr<11) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000367 res.send(400);
368 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000369 } else if (args.tc==tc_100first_error_response & ctr<101) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000370 res.send(400);
371 return;
372 }
373
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000374 if (published[feedIndexes[feedId]].includes(filename)) {
375 ctr_publish_query_published[feedIndexes[feedId]]++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000376 strToSend="[" + filename + "]";
maximessonb8b94ae2019-03-25 16:03:08 +0000377 } else {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000378 ctr_publish_query_not_published[feedIndexes[feedId]]++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000379 strToSend="[]";
maximessonb8b94ae2019-03-25 16:03:08 +0000380 }
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000381 if (sleeptime > 0) {
382 sleep(sleeptime).then(() => {
383 res.send(strToSend);
384 });
385 } else {
386 res.send(strToSend);
387 }
388});
389
maximessonb8b94ae2019-03-25 16:03:08 +0000390
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000391app.put('/publish/:feedId/:filename', function (req, res) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000392 console.log("url:"+req.url);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000393// console.log("body (first 25 bytes):"+req.body.slice(0,25));
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000394 console.log("headers:"+req.headers);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000395 console.log(JSON.stringify(req.headers));
396 var feedId = req.params.feedId;
397 ctr_publish_req[feedIndexes[feedId]]++;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000398
399 var filename = req.params.filename;
400 console.log(filename);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000401 if (!filenameStartsWith(filename, feedIndexes[feedId])) {
402 ctr_publish_req_bad_file_prefix[feedIndexes[feedId]]++;
403 }
404 var ctr = ctr_publish_req[feedIndexes[feedId]];
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000405 if (args.tc==tc_normal) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000406 // Continue
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000407 } else if (args.tc==tc_none_published) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000408 ctr_publish_req_redirect[feedIndexes[feedId]]++;
409 res.redirect(301, 'http://' + drr_sim_ip + ':3908/publish/'+feedId+'/'+filename);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000410 return;
411 } else if (args.tc==tc_all_published) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000412 ctr_publish_req_published[feedIndexes[feedId]]++;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000413 res.send("ok");
414 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000415 }else if (args.tc==tc_10p_no_response && (ctr%10) == 0) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000416 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000417 } else if (args.tc==tc_10first_no_response && ctr<11) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000418 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000419 } else if (args.tc==tc_100first_no_response && ctr<101) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000420 return;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000421 } else if (args.tc==tc_all_delay_1s) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000422 do_publish_delay(res, filename, 1000, feedId);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000423 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000424 } else if (args.tc==tc_all_delay_10s) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000425 do_publish_delay(res, filename, 10000, feedId);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000426 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000427 } else if (args.tc==tc_10p_delay_10s && (ctr%10) == 0) {
428 do_publish_delay(res, filename, 10000, feedId);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000429 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000430 } else if (args.tc==tc_10p_error_response && (ctr%10) == 0) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000431 res.send(400);
432 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000433 } else if (args.tc==tc_10first_error_response && ctr<11) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000434 res.send(400);
435 return;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000436 } else if (args.tc==tc_100first_error_response & ctr<101) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000437 res.send(400);
438 return;
439 }
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000440 if (!published.includes(filename)) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000441 ctr_publish_req_redirect[feedIndexes[feedId]]++;
442 res.redirect(301, 'http://'+drr_sim_ip+':3908/publish/'+feedId+'/'+filename);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000443 } else {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000444 ctr_publish_req_published[feedIndexes[feedId]]++;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000445 res.send("ok");
446 }
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000447 return;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000448})
449
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000450function do_publish_delay(res, filename, sleeptime, feedId) {
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000451 if (!published.includes(filename)) {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000452 ctr_publish_req_redirect[feedIndexes[feedId]]++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000453 sleep(1000).then(() => {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000454 res.redirect(301, 'http://'+drr_sim_ip+':3908/publish/'+feedId+'/'+filename);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000455 });
456 } else {
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000457 ctr_publish_req_published[feedIndexes[feedId]]++;
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000458 sleep(1000).then(() => {
459 res.send("ok");
460 });
461 }
462}
463
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000464//Callback from DR REDIR server, when file is published ok this PUT request update the list of published files.
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000465app.put('/dr_redir_publish/:feedId/:filename', function (req, res) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000466 console.log("url:"+req.url);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000467 var feedId = req.params.feedId;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000468 var filename = req.params.filename;
469 console.log(filename);
470
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000471 if (!published[feedIndexes[feedId]].includes(filename)) {
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000472 console.log("File marked as published by callback from DR redir SIM. url: " + req.url);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000473 published[feedIndexes[feedId]].push(filename);
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000474 } else {
475 console.log("File already marked as published. Callback from DR redir SIM. url: " + req.url);
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000476 ctr_double_publish[feedIndexes[feedId]]++;
BjornMagnussonXAf4e18362019-04-10 13:04:08 +0000477 }
478
479 res.send("ok");
480})
maximessonb8b94ae2019-03-25 16:03:08 +0000481
TamasBakai9b780332019-02-15 08:38:16 +0000482var httpServer = http.createServer(app);
483var httpsServer = https.createServer(credentials, app);
484
maximessonb8b94ae2019-03-25 16:03:08 +0000485var httpPort=3906;
486var httpsPort=3907;
TamasBakai9b780332019-02-15 08:38:16 +0000487httpServer.listen(httpPort);
maximessonb8b94ae2019-03-25 16:03:08 +0000488console.log("DR-simulator listening (http) at "+httpPort);
TamasBakai9b780332019-02-15 08:38:16 +0000489httpsServer.listen(httpsPort);
BjornMagnussonXA42dcb262019-04-26 19:29:54 +0000490console.log("DR-simulator listening (https) at "+httpsPort);
491
492if (process.env.DRR_SIM_IP) {
493 drr_sim_ip=process.env.DRR_SIM_IP;
BjornMagnussonXAa79a0432019-07-17 08:26:50 +0000494}
495console.log("Using IP " + drr_sim_ip + " for redirect to DR redir sim");
496
497if (process.env.DR_FEEDS) {
498 feeds=process.env.DR_FEEDS;
499}
500
501console.log("Configured list of feeds mapped to file name prefixes: " + feeds);
502
503feedNames=feeds.split(',');
504for(i=0;i<feedNames.length;i++) {
505 var tmp=feedNames[i].split(':');
506 feedNames[i]=tmp[0].trim();
507 feedIndexes[feedNames[i]]=i;
508 filePrefixes[i]=[]
509 for(j=1;j<tmp.length;j++) {
510 filePrefixes[i][j-1]=tmp[j];
511 }
512
513 ctr_publish_query[i] = 0;
514 ctr_publish_query_published[i] = 0;
515 ctr_publish_query_not_published[i] = 0;
516 ctr_publish_req[i] = 0;
517 ctr_publish_req_redirect[i] = 0;
518 ctr_publish_req_published[i] = 0;
519 ctr_double_publish[i] = 0;
520 ctr_publish_query_bad_file_prefix[i] = 0;
521 ctr_publish_req_bad_file_prefix[i] = 0;
522 published[i] = [];
523}
524
525console.log("Parsed mapping between feed id and file name prefix");
526for(i=0;i<feedNames.length;i++) {
527 var fn = feedNames[i];
528 for (j=0;j<filePrefixes[i].length;j++) {
529 console.log("Feed id: " + fn + ", file name prefix: " + filePrefixes[i][j]);
530 }
531}
532