TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 1 | var http = require('http'); |
| 2 | var https = require('https'); |
| 3 | |
| 4 | var express = require('express'); |
| 5 | const stream = require('stream'); |
| 6 | var app = express(); |
| 7 | var fs = require("fs"); |
| 8 | var path = require('path'); |
BjornMagnussonXA | 42dcb26 | 2019-04-26 19:29:54 +0000 | [diff] [blame] | 9 | const sleep = (milliseconds) => { |
| 10 | return new Promise(resolve => setTimeout(resolve, milliseconds)) |
| 11 | } |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 12 | var ArgumentParser = require('argparse').ArgumentParser; |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 13 | var privateKey = fs.readFileSync('cert/private.key', 'utf8'); |
| 14 | var certificate = fs.readFileSync('cert/certificate.crt', 'utf8'); |
| 15 | var credentials = {key: privateKey, cert: certificate}; |
| 16 | |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 17 | var total_first_publish=0; |
| 18 | var total_last_publish=0 |
| 19 | var total_files=0; |
| 20 | var speed=0; |
| 21 | |
| 22 | var feeds="1:A"; //Comma separated list of feedId:filePrefix. Default is feedId=1 and file prefix 'A' |
| 23 | var feedNames=[]; |
| 24 | var filePrefixes=[]; |
| 25 | var feedIndexes=[]; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 26 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 27 | var bodyParser = require('body-parser') |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 28 | var startTime = Date.now(); |
| 29 | |
| 30 | var dr_callback_ip = '192.168.100.2'; //IP for DR when running as container. Can be changed by env DR_SIM_IP |
| 31 | |
| 32 | //Counters |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 33 | var ctr_publish_requests = []; |
| 34 | var ctr_publish_requests_bad_file_prefix = []; |
| 35 | var ctr_publish_responses = []; |
| 36 | var lastPublish = []; |
| 37 | var dwl_volume = []; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 38 | |
| 39 | var parser = new ArgumentParser({ |
| 40 | version: '0.0.1', |
| 41 | addHelp:true, |
| 42 | description: 'Datarouter redirect simulator' |
| 43 | }); |
| 44 | |
| 45 | parser.addArgument('--tc' , { help: 'TC $NoOfTc' } ); |
| 46 | parser.addArgument('--printtc' , |
| 47 | { |
| 48 | help: 'Print complete usage help', |
| 49 | action: 'storeTrue' |
| 50 | } |
| 51 | ); |
| 52 | |
| 53 | var args = parser.parseArgs(); |
| 54 | const tc_normal = "normal"; |
| 55 | const tc_no_publish ="no_publish" |
| 56 | const tc_10p_no_response = "10p_no_response"; |
| 57 | const tc_10first_no_response = "10first_no_response"; |
| 58 | const tc_100first_no_response = "100first_no_response"; |
BjornMagnussonXA | 42dcb26 | 2019-04-26 19:29:54 +0000 | [diff] [blame] | 59 | const tc_all_delay_1s = "all_delay_1s"; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 60 | const tc_all_delay_10s = "all_delay_10s"; |
| 61 | const tc_10p_delay_10s = "10p_delay_10s"; |
| 62 | const tc_10p_error_response = "10p_error_response"; |
| 63 | const tc_10first_error_response = "10first_error_response"; |
| 64 | const tc_100first_error_response = "100first_error_response"; |
| 65 | |
| 66 | if (args.tc==tc_normal) { |
| 67 | console.log("TC: " + args.tc) |
| 68 | |
| 69 | } else if (args.tc==tc_no_publish) { |
| 70 | console.log("TC: " + args.tc) |
| 71 | |
| 72 | } else if (args.tc==tc_10p_no_response) { |
| 73 | console.log("TC: " + args.tc) |
| 74 | |
| 75 | } else if (args.tc==tc_10first_no_response) { |
| 76 | console.log("TC: " + args.tc) |
| 77 | |
| 78 | } else if (args.tc==tc_100first_no_response) { |
| 79 | console.log("TC: " + args.tc) |
| 80 | |
BjornMagnussonXA | 42dcb26 | 2019-04-26 19:29:54 +0000 | [diff] [blame] | 81 | } else if (args.tc==tc_all_delay_1s) { |
| 82 | console.log("TC: " + args.tc) |
| 83 | |
| 84 | } else if (args.tc==tc_all_delay_10s) { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 85 | console.log("TC: " + args.tc) |
| 86 | |
| 87 | } else if (args.tc==tc_10p_delay_10s) { |
| 88 | console.log("TC: " + args.tc) |
| 89 | |
| 90 | } else if (args.tc==tc_10p_error_response) { |
| 91 | console.log("TC: " + args.tc) |
| 92 | |
| 93 | } else if (args.tc==tc_10first_error_response) { |
| 94 | console.log("TC: " + args.tc) |
| 95 | |
| 96 | } else if (args.tc==tc_100first_error_response) { |
| 97 | console.log("TC: " + args.tc) |
| 98 | } else { |
| 99 | console.log("No TC specified, use: --tc <tc-id>"); |
| 100 | process.exit(0); |
| 101 | } |
| 102 | |
| 103 | if (args.printtc) { |
| 104 | console.log("TC " + tc_normal + ": Normal case, all files publish and DR updated"); |
| 105 | console.log("TC " + tc_no_publish + ": Ok response but no files published"); |
| 106 | console.log("TC " + tc_10p_no_response + ": 10% % no response (file not published)"); |
| 107 | console.log("TC " + tc_10first_no_response + ": 10 first requests give no response (files not published)"); |
| 108 | console.log("TC " + tc_100first_no_response + ": 100 first requests give no response (files not published)"); |
BjornMagnussonXA | 42dcb26 | 2019-04-26 19:29:54 +0000 | [diff] [blame] | 109 | console.log("TC " + tc_all_delay_1s + ": All responses delayed 1s, normal publish"); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 110 | console.log("TC " + tc_all_delay_10s + ": All responses delayed 10s, normal publish"); |
| 111 | console.log("TC " + tc_10p_delay_10s + ": 10% of responses delayed 10s, normal publish"); |
| 112 | console.log("TC " + tc_10p_error_response + ": 10% error response (file not published)"); |
| 113 | console.log("TC " + tc_10first_error_response + ": 10 first requests give error response (file not published)"); |
| 114 | console.log("TC " + tc_100first_error_response + ": 100 first requests give error responses (file not published)"); |
| 115 | |
| 116 | process.exit(0); |
| 117 | } |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 118 | |
| 119 | // parse application/x-www-form-urlencoded |
| 120 | app.use(bodyParser.urlencoded({ extended: false })) |
| 121 | |
| 122 | // parse application/json |
| 123 | app.use(bodyParser.json()) |
| 124 | |
| 125 | // parse application/vnd.api+json as json |
| 126 | app.use(bodyParser.json({ type: 'application/vnd.api+json' })) |
| 127 | |
| 128 | // parse some custom thing into a Buffer |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 129 | app.use(bodyParser.raw({limit:1024*1024*60, type: 'application/octet-stream' })) |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 130 | |
| 131 | // parse an HTML body into a string |
| 132 | app.use(bodyParser.text({ type: 'text/html' })) |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 133 | |
| 134 | //Formatting |
| 135 | function fmtMSS(s){ |
| 136 | return(s-(s%=60))/60+(9<s?':':':0')+s //Format time diff to mm:ss |
| 137 | } |
| 138 | function fmtLargeNumber(x) { |
| 139 | return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "); //Format large with space, eg: 1 000 000 |
| 140 | } |
| 141 | |
| 142 | //I'm alive function |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 143 | app.get("/",function(req, res){ |
| 144 | res.send("ok"); |
| 145 | }) |
| 146 | |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 147 | function toCommaList(ctrArray) { |
| 148 | var str=""; |
| 149 | for(i=0;i<feedNames.length;i++) { |
| 150 | if (i!=0) { |
| 151 | str=str+","; |
| 152 | } |
| 153 | str=str+ctrArray[i]; |
| 154 | } |
| 155 | return str; |
| 156 | } |
| 157 | |
| 158 | function toCommaListTime(ctrArray) { |
| 159 | var str=""; |
| 160 | for(i=0;i<feedNames.length;i++) { |
| 161 | if (i!=0) { |
| 162 | str=str+","; |
| 163 | } |
| 164 | if (ctrArray[i] < 0) { |
| 165 | str=str+"--:--"; |
| 166 | } else { |
| 167 | str=str+fmtMSS(ctrArray[i]); |
| 168 | } |
| 169 | } |
| 170 | return str; |
| 171 | } |
| 172 | |
| 173 | function sumList(ctrArray) { |
| 174 | var tmp=0; |
| 175 | for(i=0;i<feedNames.length;i++) { |
| 176 | tmp=tmp+ctrArray[i]; |
| 177 | } |
| 178 | return ""+tmp; |
| 179 | } |
| 180 | |
| 181 | function largestInListTime(ctrArray) { |
| 182 | var tmp=-1; |
| 183 | var str="" |
| 184 | for(i=0;i<feedNames.length;i++) { |
| 185 | if (ctrArray[i] > tmp) { |
| 186 | tmp = ctrArray[i]; |
| 187 | } |
| 188 | } |
| 189 | if (tmp < 0) { |
| 190 | str="--:--"; |
| 191 | } else { |
| 192 | str=fmtMSS(tmp); |
| 193 | } |
| 194 | return str; |
| 195 | } |
| 196 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 197 | //Counter readout |
| 198 | app.get("/ctr_publish_requests",function(req, res){ |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 199 | res.send(""+sumList(ctr_publish_requests)); |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 200 | }) |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 201 | app.get("/feeds/ctr_publish_requests/",function(req, res){ |
| 202 | res.send(toCommaList(ctr_publish_requests)); |
| 203 | }) |
| 204 | app.get("/ctr_publish_requests/:feedId",function(req, res){ |
| 205 | var feedId = req.params.feedId; |
| 206 | res.send(""+ctr_publish_requests[feedIndexes[feedId]]); |
| 207 | }) |
| 208 | |
| 209 | app.get("/ctr_publish_requests_bad_file_prefix",function(req, res){ |
| 210 | res.send(""+sumList(ctr_publish_requests_bad_file_prefix)); |
| 211 | }) |
| 212 | app.get("/feeds/ctr_publish_requests_bad_file_prefix/",function(req, res){ |
| 213 | res.send(toCommaList(ctr_publish_requests_bad_file_prefix)); |
| 214 | }) |
| 215 | app.get("/ctr_publish_requests_bad_file_prefix/:feedId",function(req, res){ |
| 216 | var feedId = req.params.feedId; |
| 217 | res.send(""+ctr_publish_requests_bad_file_prefix[feedIndexes[feedId]]); |
| 218 | }) |
| 219 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 220 | app.get("/ctr_publish_responses",function(req, res){ |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 221 | res.send(""+sumList(ctr_publish_responses)); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 222 | }) |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 223 | app.get("/feeds/ctr_publish_responses/",function(req, res){ |
| 224 | res.send(toCommaList(ctr_publish_responses)); |
| 225 | }) |
| 226 | app.get("/ctr_publish_responses/:feedId",function(req, res){ |
| 227 | var feedId = req.params.feedId; |
| 228 | res.send(""+ctr_publish_responses[feedIndexes[feedId]]); |
| 229 | }) |
| 230 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 231 | app.get("/execution_time",function(req, res){ |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 232 | var diff = fmtMSS(Math.floor((Date.now()-startTime)/1000)); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 233 | res.send(""+diff); |
| 234 | }) |
| 235 | app.get("/time_lastpublish",function(req, res){ |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 236 | res.send(""+largestInListTime(lastPublish)); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 237 | }) |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 238 | app.get("/feeds/time_lastpublish/",function(req, res){ |
| 239 | res.send(toCommaListTime(lastPublish)); |
| 240 | }) |
| 241 | app.get("/time_lastpublish/:feedId",function(req, res){ |
| 242 | var feedId = req.params.feedId; |
| 243 | if (lastPublish[feedIndexes[feedId]] < 0) { |
| 244 | res.send("--:--"); |
| 245 | } |
| 246 | res.send(""+fmtMSS(lastPublish[feedIndexes[feedId]])); |
| 247 | }) |
| 248 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 249 | app.get("/dwl_volume",function(req, res){ |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 250 | res.send(""+fmtLargeNumber(sumList(dwl_volume))); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 251 | }) |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 252 | app.get("/feeds/dwl_volume/",function(req, res){ |
| 253 | var str=""; |
| 254 | for(i=0;i<feedNames.length;i++) { |
| 255 | if (i!=0) { |
| 256 | str=str+","; |
| 257 | } |
| 258 | str=str+fmtLargeNumber(dwl_volume[i]); |
| 259 | } |
| 260 | res.send(str); |
| 261 | }) |
| 262 | app.get("/dwl_volume/:feedId",function(req, res){ |
| 263 | var feedId = req.params.feedId; |
| 264 | res.send(""+fmtLargeNumber(dwl_volume[feedIndexes[feedId]])); |
| 265 | }) |
| 266 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 267 | app.get("/tc_info",function(req, res){ |
| 268 | res.send(args.tc); |
| 269 | }) |
| 270 | |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 271 | app.get("/feeds",function(req, res){ |
| 272 | res.send(feeds); |
| 273 | }) |
| 274 | |
| 275 | app.get("/speed",function(req, res){ |
| 276 | res.send(""+speed); |
| 277 | }) |
| 278 | |
| 279 | function filenameStartsWith(fileName, feedIndex) { |
| 280 | var i=0; |
| 281 | for(i=0;i<filePrefixes[feedIndex].length;i++) { |
| 282 | var prefix=filePrefixes[feedIndex][i]; |
| 283 | if (fileName.startsWith(prefix)) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | app.put('/publish/:feedId/:filename', function (req, res) { |
| 291 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 292 | console.log(req.url); |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 293 | var feedId=req.params.feedId; |
| 294 | // console.log("First 25 bytes of body: " + req.body.slice(0,25)) |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 295 | console.log(req.headers) |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 296 | ctr_publish_requests[feedIndexes[feedId]]++; |
| 297 | var filename = req.params.filename; |
| 298 | if (!filenameStartsWith(filename, feedIndexes[feedId])) { |
| 299 | ctr_publish_requests_bad_file_prefix[feedIndexes[feedId]]++; |
| 300 | } |
| 301 | var ctr = ctr_publish_requests[feedIndexes[feedId]]; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 302 | if (args.tc == tc_no_publish) { |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 303 | ctr_publish_responses[feedIndexes[feedId]]++; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 304 | res.send("ok") |
| 305 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 306 | } else if (args.tc==tc_10p_no_response && (ctr%10)==0) { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 307 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 308 | } else if (args.tc==tc_10first_no_response && ctr<11) { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 309 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 310 | } else if (args.tc==tc_100first_no_response && ctr<101) { |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 311 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 312 | } else if (args.tc==tc_10p_error_response && (ctr%10)==0) { |
| 313 | ctr_publish_responses[feedIndexes[feedId]]++; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 314 | res.send(400, ""); |
| 315 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 316 | } else if (args.tc==tc_10first_error_response && ctr<11) { |
| 317 | ctr_publish_responses[feedIndexes[feedId]]++; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 318 | res.send(400, ""); |
| 319 | return; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 320 | } else if (args.tc==tc_100first_error_response && ctr<101) { |
| 321 | ctr_publish_responses[feedIndexes[feedId]]++; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 322 | res.send(400, ""); |
| 323 | return; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | //Remaining part if normal file publish |
| 327 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 328 | console.log(filename); |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 329 | //Create filename (appending file size and feedid to name) to store |
| 330 | var storedFilename = path.resolve(__dirname, filename+"-"+feedId+"-"+req.body.length); |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 331 | fs.writeFile(storedFilename, "", function (error) { //Store file with zero size |
| 332 | if (error) { console.error(error); } |
| 333 | }); |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 334 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 335 | //Make callback to update list of publish files in DR sim |
| 336 | //Note the hard code ip-adress, DR sim get this ip if simulators started from the |
| 337 | //script in the 'simulatorgroup' dir. |
| 338 | //Work around: Could not get a normal http put to work from nodejs, using curl instead |
| 339 | var util = require('util'); |
| 340 | var exec = require('child_process').exec; |
| 341 | |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 342 | var command = 'curl -s -X PUT http://' + dr_callback_ip + ':3906/dr_redir_publish/'+feedId+'/'+filename; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 343 | |
| 344 | console.log("Callback to DR sim to report file published, cmd: " + command); |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 345 | var child = exec(command, function(error, stdout, stderr){ |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 346 | console.log('stdout: ' + stdout); |
| 347 | console.log('stderr: ' + stderr); |
| 348 | if(error !== null) { |
| 349 | console.log('exec error: ' + error); |
| 350 | } |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 351 | }); |
| 352 | |
| 353 | //Update status variables |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 354 | ctr_publish_responses[feedIndexes[feedId]]++; |
| 355 | lastPublish[feedIndexes[feedId]] = Math.floor((Date.now()-startTime)/1000); |
| 356 | dwl_volume[feedIndexes[feedId]] = dwl_volume[feedIndexes[feedId]] + req.body.length; |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 357 | |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 358 | if (args.tc==tc_10p_delay_10s && (ctr%10)==0) { |
BjornMagnussonXA | 42dcb26 | 2019-04-26 19:29:54 +0000 | [diff] [blame] | 359 | sleep(10000).then(() => { |
| 360 | res.send("ok"); |
| 361 | }); |
| 362 | return; |
| 363 | } else if (args.tc==tc_all_delay_10s) { |
| 364 | sleep(10000).then(() => { |
| 365 | res.send("ok"); |
| 366 | }); |
| 367 | return; |
| 368 | } else if (args.tc==tc_all_delay_1s) { |
| 369 | sleep(1000).then(() => { |
| 370 | res.send("ok"); |
| 371 | }); |
| 372 | return; |
| 373 | } |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 374 | if (total_first_publish == 0) { |
| 375 | total_first_publish=Date.now()/1000; |
| 376 | } |
| 377 | total_last_publish=Date.now()/1000; |
| 378 | total_files++; |
| 379 | if (total_last_publish > total_first_publish) { |
| 380 | speed = Math.round((total_files/(total_last_publish-total_first_publish))*10)/10; |
| 381 | } |
| 382 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 383 | res.send("ok") |
| 384 | }); |
| 385 | |
| 386 | |
TamasBakai | 9b78033 | 2019-02-15 08:38:16 +0000 | [diff] [blame] | 387 | var httpServer = http.createServer(app); |
| 388 | var httpsServer = https.createServer(credentials, app); |
| 389 | |
| 390 | var httpPort=3908 |
| 391 | var httpsPort=3909 |
| 392 | httpServer.listen(httpPort); |
| 393 | console.log("DR-simulator listening (http) at "+httpPort) |
| 394 | httpsServer.listen(httpsPort); |
| 395 | console.log("DR-simulator listening (https) at "+httpsPort) |
| 396 | |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 397 | if (process.env.DR_SIM_IP) { |
| 398 | dr_callback_ip=process.env.DR_SIM_IP; |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 399 | } |
BjornMagnussonXA | f4e1836 | 2019-04-10 13:04:08 +0000 | [diff] [blame] | 400 | console.log("Using IP " + dr_callback_ip + " for callback to DR sim"); |
BjornMagnussonXA | a79a043 | 2019-07-17 08:26:50 +0000 | [diff] [blame^] | 401 | |
| 402 | if (process.env.DR_REDIR_FEEDS) { |
| 403 | feeds=process.env.DR_REDIR_FEEDS; |
| 404 | } |
| 405 | console.log("Configured list of feeds: " + feeds); |
| 406 | |
| 407 | var i=0; |
| 408 | feedNames=feeds.split(','); |
| 409 | for(i=0;i<feedNames.length;i++) { |
| 410 | var tmp=feedNames[i].split(':'); |
| 411 | feedNames[i]=tmp[0].trim(); |
| 412 | feedIndexes[feedNames[i]]=i; |
| 413 | filePrefixes[i]=[] |
| 414 | var j=0; |
| 415 | for(j=1;j<tmp.length;j++) { |
| 416 | filePrefixes[i][j-1]=tmp[j]; |
| 417 | } |
| 418 | |
| 419 | ctr_publish_requests[i] = 0; |
| 420 | ctr_publish_requests_bad_file_prefix[i] = 0; |
| 421 | ctr_publish_responses[i] = 0; |
| 422 | lastPublish[i] = -1; |
| 423 | dwl_volume[i] = 0; |
| 424 | } |
| 425 | console.log("Parsed mapping between feed id and file name prefix"); |
| 426 | for(i=0;i<feedNames.length;i++) { |
| 427 | var fn = feedNames[i]; |
| 428 | for (j=0;j<filePrefixes[i].length;j++) { |
| 429 | console.log("Feed id: " + fn + ", file name prefix: " + filePrefixes[i][j]); |
| 430 | } |
| 431 | } |