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,36 +269,67 @@ function restartScenario() { ...@@ -263,36 +269,67 @@ 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;
276 - if (Number(evno) > Number(maxEvno)) { 283 + }
277 - // done 284 +
278 - scdone = true; 285 + if (Number(evno) > Number(maxEvno)) {
279 - console.log('Scenario DONE.'); 286 + scdone = true;
280 - } else { 287 + console.log('Scenario DONE.');
281 - // fire next event 288 + return false;
282 - path = scenarioPath(evno); 289 + }
283 - fs.readFile(path, 'utf8', function (err, data) { 290 + return true;
284 - if (err) { 291 +}
285 - console.log('Oops error: ' + err); 292 +
286 - } else { 293 +function autoAdvance() {
287 - evdata = JSON.parse(data); 294 + if (evno > autoLast) {
288 - console.log(); // get past prompt 295 + console.log('[auto done]');
289 - console.log('Sending event #' + evno + ' [' + evdata.event + 296 + return;
290 - '] from ' + eventsById[evno].fname); 297 + }
291 - connection.sendUTF(data); 298 +
292 - evno++; 299 + // need to recurse with a callback, since each event send relies
293 - } 300 + // on an async load of event data...
294 - rl.prompt(); 301 + function callback() {
295 - }); 302 + if (eventAvailable() && evno <= autoLast) {
303 + _nextEvent(callback);
296 } 304 }
297 } 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 +
319 + fs.readFile(path, 'utf8', function (err, data) {
320 + if (err) {
321 + console.error('Oops error: ' + err);
322 + } else {
323 + evdata = JSON.parse(data);
324 + console.log(); // get past prompt
325 + console.log('Sending event #' + evno + ' [' + evdata.event +
326 + '] from ' + eventsById[evno].fname);
327 + connection.sendUTF(data);
328 + evno++;
329 + if (callback) {
330 + callback();
331 + }
332 + }
333 + rl.prompt();
334 + });
298 } 335 }
......