Simon Hunt

GUI -- TopoView - mockserver.js -- re-instated the "auto-advance" feature.

Change-Id: I867430cf42483fa7f2cacdd0b6fdd97d0eaa90a9
...@@ -18,6 +18,7 @@ var lastcmd, // last command executed ...@@ -18,6 +18,7 @@ var lastcmd, // last command executed
18 scdone, // shows when scenario is over 18 scdone, // shows when scenario is over
19 eventsById, // map of event file names 19 eventsById, // map of event file names
20 maxEvno, // highest loaded event number 20 maxEvno, // highest loaded event number
21 + autoLast, // last event number for auto-advance
21 evno, // next event number 22 evno, // next event number
22 evdata; // event data 23 evdata; // event data
23 24
...@@ -119,6 +120,7 @@ function doCli() { ...@@ -119,6 +120,7 @@ function doCli() {
119 case 'c': connStatus(); break; 120 case 'c': connStatus(); break;
120 case 'm': customMessage(str); break; 121 case 'm': customMessage(str); break;
121 case 's': setScenario(str); break; 122 case 's': setScenario(str); break;
123 + case 'a': autoAdvance(); break;
122 case 'n': nextEvent(); break; 124 case 'n': nextEvent(); break;
123 case 'r': restartScenario(); break; 125 case 'r': restartScenario(); break;
124 case 'q': quit(); break; 126 case 'q': quit(); break;
...@@ -140,7 +142,7 @@ var helptext = '\n' + ...@@ -140,7 +142,7 @@ var helptext = '\n' +
140 'm {text} - send custom message to client\n' + 142 'm {text} - send custom message to client\n' +
141 's {id} - load scenario {id}\n' + 143 's {id} - load scenario {id}\n' +
142 's - show scenario status\n' + 144 's - show scenario status\n' +
143 - //'a - auto-send events\n' + 145 + 'a - auto-send events\n' +
144 'n - send next event\n' + 146 'n - send next event\n' +
145 'r - restart the scenario\n' + 147 'r - restart the scenario\n' +
146 'q - exit the server\n' + 148 'q - exit the server\n' +
...@@ -202,6 +204,10 @@ function initScenario(verb) { ...@@ -202,6 +204,10 @@ function initScenario(verb) {
202 scdata.description.forEach(function (d) { 204 scdata.description.forEach(function (d) {
203 console.log(' ' + d); 205 console.log(' ' + d);
204 }); 206 });
207 + autoLast = (scdata.params && scdata.params.lastAuto) || 0;
208 + if (autoLast) {
209 + console.log('[auto-advance: ' + autoLast + ']');
210 + }
205 evno = 1; 211 evno = 1;
206 scdone = false; 212 scdone = false;
207 readEventFilenames(); 213 readEventFilenames();
...@@ -263,26 +269,56 @@ function restartScenario() { ...@@ -263,26 +269,56 @@ function restartScenario() {
263 rl.prompt(); 269 rl.prompt();
264 } 270 }
265 271
266 -function nextEvent() { 272 +function eventAvailable() {
267 - var path;
268 -
269 if (!scid) { 273 if (!scid) {
270 console.log('No scenario loaded.'); 274 console.log('No scenario loaded.');
271 rl.prompt(); 275 rl.prompt();
272 - } else if (!connection) { 276 + return false;
273 - console.warn('No current connection.'); 277 + }
278 +
279 + if (!connection) {
280 + console.log('No current connection.');
274 rl.prompt(); 281 rl.prompt();
275 - } else { 282 + return false;
283 + }
284 +
276 if (Number(evno) > Number(maxEvno)) { 285 if (Number(evno) > Number(maxEvno)) {
277 - // done
278 scdone = true; 286 scdone = true;
279 console.log('Scenario DONE.'); 287 console.log('Scenario DONE.');
280 - } else { 288 + return false;
281 - // fire next event 289 + }
282 - path = scenarioPath(evno); 290 + return true;
291 +}
292 +
293 +function autoAdvance() {
294 + if (evno > autoLast) {
295 + console.log('[auto done]');
296 + return;
297 + }
298 +
299 + // need to recurse with a callback, since each event send relies
300 + // on an async load of event data...
301 + function callback() {
302 + if (eventAvailable() && evno <= autoLast) {
303 + _nextEvent(callback);
304 + }
305 + }
306 +
307 + callback();
308 +}
309 +
310 +function nextEvent() {
311 + if (eventAvailable()) {
312 + _nextEvent();
313 + }
314 +}
315 +
316 +function _nextEvent(callback) {
317 + var path = scenarioPath(evno);
318 +
283 fs.readFile(path, 'utf8', function (err, data) { 319 fs.readFile(path, 'utf8', function (err, data) {
284 if (err) { 320 if (err) {
285 - console.log('Oops error: ' + err); 321 + console.error('Oops error: ' + err);
286 } else { 322 } else {
287 evdata = JSON.parse(data); 323 evdata = JSON.parse(data);
288 console.log(); // get past prompt 324 console.log(); // get past prompt
...@@ -290,9 +326,10 @@ function nextEvent() { ...@@ -290,9 +326,10 @@ function nextEvent() {
290 '] from ' + eventsById[evno].fname); 326 '] from ' + eventsById[evno].fname);
291 connection.sendUTF(data); 327 connection.sendUTF(data);
292 evno++; 328 evno++;
329 + if (callback) {
330 + callback();
331 + }
293 } 332 }
294 rl.prompt(); 333 rl.prompt();
295 }); 334 });
296 - }
297 - }
298 } 335 }
......