Simon Hunt

GUI -- Augmented mock server to allow descriptive names for events.

- also, lists scenarios at startup.

Change-Id: I6f32d15e7ed4ec2ade4546d68d2e245001c394ec
...@@ -145,7 +145,7 @@ ...@@ -145,7 +145,7 @@
145 function instColor(id, online) { 145 function instColor(id, online) {
146 // TODO: fix this.. 146 // TODO: fix this..
147 //return cat7.get(id, !online, network.view.getTheme()); 147 //return cat7.get(id, !online, network.view.getTheme());
148 - return 'blue'; 148 + return '#3E5780';
149 } 149 }
150 150
151 // ============================== 151 // ==============================
......
...@@ -6,7 +6,8 @@ var fs = require('fs'), ...@@ -6,7 +6,8 @@ var fs = require('fs'),
6 readline = require('readline'), 6 readline = require('readline'),
7 http = require('http'), 7 http = require('http'),
8 WebSocketServer = require('websocket').server, 8 WebSocketServer = require('websocket').server,
9 - port = 8123; 9 + port = 8123,
10 + scenarioRoot = 'ev/';
10 11
11 var lastcmd, // last command executed 12 var lastcmd, // last command executed
12 lastargs, // arguments to last command 13 lastargs, // arguments to last command
...@@ -15,10 +16,19 @@ var lastcmd, // last command executed ...@@ -15,10 +16,19 @@ var lastcmd, // last command executed
15 scid, // scenario ID 16 scid, // scenario ID
16 scdata, // scenario data 17 scdata, // scenario data
17 scdone, // shows when scenario is over 18 scdone, // shows when scenario is over
19 + eventsById, // map of event file names
20 + maxEvno, // highest loaded event number
18 evno, // next event number 21 evno, // next event number
19 evdata; // event data 22 evdata; // event data
20 23
21 24
25 +var scFiles = fs.readdirSync(scenarioRoot);
26 +console.log('Mock Server v1.0');
27 +console.log('================');
28 +console.log('Scenarios ...');
29 +console.log(scFiles.join(', '));
30 +console.log();
31 +
22 32
23 var rl = readline.createInterface(process.stdin, process.stdout); 33 var rl = readline.createInterface(process.stdin, process.stdout);
24 rl.setPrompt('ws> '); 34 rl.setPrompt('ws> ');
...@@ -175,8 +185,8 @@ function showScenarioStatus() { ...@@ -175,8 +185,8 @@ function showScenarioStatus() {
175 } 185 }
176 186
177 function scenarioPath(evno) { 187 function scenarioPath(evno) {
178 - var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json'; 188 + var file = evno ? ('/' + eventsById[evno].fname) : '/scenario.json';
179 - return 'ev/' + scid + file; 189 + return scenarioRoot + scid + file;
180 } 190 }
181 191
182 192
...@@ -189,6 +199,35 @@ function initScenario(verb) { ...@@ -189,6 +199,35 @@ function initScenario(verb) {
189 }); 199 });
190 evno = 1; 200 evno = 1;
191 scdone = false; 201 scdone = false;
202 + readEventFilenames();
203 +}
204 +
205 +function readEventFilenames() {
206 + var files = fs.readdirSync(scenarioRoot + scid),
207 + eventCount = 0,
208 + match, id, tag;
209 +
210 + maxEvno = 0;
211 +
212 + eventsById = {};
213 + files.forEach(function (f) {
214 + match = /^ev_(\d+)_(.*)\.json$/.exec(f);
215 + if (match) {
216 + eventCount++;
217 + id = match[1];
218 + tag = match[2];
219 + eventsById[id] = {
220 + fname: f,
221 + num: id,
222 + tag: tag
223 + };
224 + if (Number(id) > Number(maxEvno)) {
225 + maxEvno = id;
226 + }
227 + }
228 +
229 + });
230 + console.log('[' + eventCount + ' events loaded, (max=' + maxEvno + ')]');
192 } 231 }
193 232
194 function setScenario(id) { 233 function setScenario(id) {
...@@ -229,20 +268,25 @@ function nextEvent() { ...@@ -229,20 +268,25 @@ function nextEvent() {
229 console.warn('No current connection.'); 268 console.warn('No current connection.');
230 rl.prompt(); 269 rl.prompt();
231 } else { 270 } else {
232 - path = scenarioPath(evno); 271 + if (Number(evno) > Number(maxEvno)) {
233 - fs.readFile(path, 'utf8', function (err, data) { 272 + // done
234 - if (err) { 273 + scdone = true;
235 - console.log('No event #' + evno); 274 + console.log('Scenario DONE.');
236 - scdone = true; 275 + } else {
237 - console.log('Scenario DONE'); 276 + // fire next event
238 - } else { 277 + path = scenarioPath(evno);
239 - evdata = JSON.parse(data); 278 + fs.readFile(path, 'utf8', function (err, data) {
240 - console.log(); // get past prompt 279 + if (err) {
241 - console.log('Sending event #' + evno + ' [' + evdata.event + ']'); 280 + console.log('Oops error: ' + err);
242 - connection.sendUTF(data); 281 + } else {
243 - evno++; 282 + evdata = JSON.parse(data);
244 - } 283 + console.log(); // get past prompt
245 - rl.prompt(); 284 + console.log('Sending event #' + evno + ' [' + evdata.event + ']');
246 - }); 285 + connection.sendUTF(data);
286 + evno++;
287 + }
288 + rl.prompt();
289 + });
290 + }
247 } 291 }
248 } 292 }
......