Committed by
Gerrit Code Review
GUI -- Mock WebSocket server now serves up scenario events.
Change-Id: I469550362b2215bbc054cb58a157c2ef5f4479ca
Showing
2 changed files
with
142 additions
and
31 deletions
| 1 | #!/usr/bin/env node | 1 | #!/usr/bin/env node |
| 2 | + | ||
| 2 | // === Mock Web Socket Server - for testing the topology view | 3 | // === Mock Web Socket Server - for testing the topology view |
| 3 | -// | ||
| 4 | 4 | ||
| 5 | -var readline = require('readline'); | 5 | +var fs = require('fs'), |
| 6 | -var WebSocketServer = require('websocket').server; | 6 | + readline = require('readline'), |
| 7 | -var http = require('http'); | 7 | + http = require('http'), |
| 8 | -var port = 8123; | 8 | + WebSocketServer = require('websocket').server, |
| 9 | + port = 8123; | ||
| 10 | + | ||
| 11 | +var lastcmd, // last command executed | ||
| 12 | + lastargs, // arguments to last command | ||
| 13 | + connection, // ws connection | ||
| 14 | + origin, // origin of connection | ||
| 15 | + scenario, // test scenario name | ||
| 16 | + scdone, // shows when scenario is over | ||
| 17 | + evno, // next event number | ||
| 18 | + evdata; // event data | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + | ||
| 22 | +var rl = readline.createInterface(process.stdin, process.stdout); | ||
| 23 | +rl.setPrompt('ws> '); | ||
| 9 | 24 | ||
| 10 | 25 | ||
| 11 | var server = http.createServer(function(request, response) { | 26 | var server = http.createServer(function(request, response) { |
| ... | @@ -37,8 +52,6 @@ function originIsAllowed(origin) { | ... | @@ -37,8 +52,6 @@ function originIsAllowed(origin) { |
| 37 | return true; | 52 | return true; |
| 38 | } | 53 | } |
| 39 | 54 | ||
| 40 | -var connection; | ||
| 41 | - | ||
| 42 | wsServer.on('request', function(request) { | 55 | wsServer.on('request', function(request) { |
| 43 | console.log(); // newline after prompt | 56 | console.log(); // newline after prompt |
| 44 | console.log("Origin: ", request.origin); | 57 | console.log("Origin: ", request.origin); |
| ... | @@ -50,13 +63,13 @@ wsServer.on('request', function(request) { | ... | @@ -50,13 +63,13 @@ wsServer.on('request', function(request) { |
| 50 | return; | 63 | return; |
| 51 | } | 64 | } |
| 52 | 65 | ||
| 53 | - connection = request.accept(null, request.origin); | 66 | + origin = request.origin; |
| 67 | + connection = request.accept(null, origin); | ||
| 54 | 68 | ||
| 55 | 69 | ||
| 56 | console.log((new Date()) + ' Connection accepted.'); | 70 | console.log((new Date()) + ' Connection accepted.'); |
| 57 | rl.prompt(); | 71 | rl.prompt(); |
| 58 | 72 | ||
| 59 | - | ||
| 60 | connection.on('message', function(message) { | 73 | connection.on('message', function(message) { |
| 61 | if (message.type === 'utf8') { | 74 | if (message.type === 'utf8') { |
| 62 | console.log(); // newline after prompt | 75 | console.log(); // newline after prompt |
| ... | @@ -71,44 +84,141 @@ wsServer.on('request', function(request) { | ... | @@ -71,44 +84,141 @@ wsServer.on('request', function(request) { |
| 71 | }); | 84 | }); |
| 72 | connection.on('close', function(reasonCode, description) { | 85 | connection.on('close', function(reasonCode, description) { |
| 73 | console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); | 86 | console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); |
| 87 | + connection = null; | ||
| 88 | + origin = null; | ||
| 74 | }); | 89 | }); |
| 75 | }); | 90 | }); |
| 76 | 91 | ||
| 77 | 92 | ||
| 78 | -var rl = readline.createInterface(process.stdin, process.stdout); | 93 | +setTimeout(doCli, 10); // allow async processes to write to stdout first |
| 79 | -rl.setPrompt('ws> '); | ||
| 80 | - | ||
| 81 | -setTimeout(doCli, 10); | ||
| 82 | 94 | ||
| 83 | function doCli() { | 95 | function doCli() { |
| 84 | rl.prompt(); | 96 | rl.prompt(); |
| 85 | rl.on('line', function (line) { | 97 | rl.on('line', function (line) { |
| 86 | var words = line.trim().split(' '), | 98 | var words = line.trim().split(' '), |
| 87 | - cmd = words.shift(), | 99 | + cmd = words.shift() || lastcmd, |
| 88 | - str = words.join(' '); | 100 | + str = words.join(' ') || lastargs; |
| 89 | 101 | ||
| 90 | switch(cmd) { | 102 | switch(cmd) { |
| 91 | - case 'hello': | 103 | + case 'c': connStatus(); break; |
| 92 | - console.log('hello back: ' + str); | 104 | + case 'm': customMessage(str); break; |
| 93 | - break; | 105 | + case 's': setScenario(str); break; |
| 106 | + case 'n': nextEvent(); break; | ||
| 107 | + case 'q': quit(); break; | ||
| 108 | + case '?': showHelp(); break; | ||
| 109 | + default: console.log('Say what?! (? for help)'); break; | ||
| 110 | + } | ||
| 111 | + lastcmd = cmd; | ||
| 112 | + lastargs = str; | ||
| 113 | + rl.prompt(); | ||
| 94 | 114 | ||
| 95 | - case 'quit': | 115 | + }).on('close', function () { |
| 96 | - process.exit(0); | 116 | + quit(); |
| 97 | - break; | 117 | + }); |
| 118 | +} | ||
| 98 | 119 | ||
| 99 | - case 'm': | 120 | +var helptext = '\n' + |
| 100 | - console.log('sending message: ' + str); | 121 | + 'c - show connection status\n' + |
| 101 | - connection.sendUTF(str); | 122 | + 'm {text} - send custom message to client\n' + |
| 102 | - break; | 123 | + 's {id} - set scenario\n' + |
| 124 | + 's - show scenario staus\n' + | ||
| 125 | + //'a - auto-send events\n' + | ||
| 126 | + 'n - send next event\n' + | ||
| 127 | + 'q - exit the server\n' + | ||
| 128 | + '? - display this help text\n'; | ||
| 129 | + | ||
| 130 | +function showHelp() { | ||
| 131 | + console.log(helptext); | ||
| 132 | +} | ||
| 103 | 133 | ||
| 104 | - default: | 134 | +function connStatus() { |
| 105 | - console.log('Say what?! [' + line.trim() + ']'); | 135 | + if (connection) { |
| 106 | - break; | 136 | + console.log('Connection from ' + origin + ' established.'); |
| 137 | + } else { | ||
| 138 | + console.log('No connection.'); | ||
| 107 | } | 139 | } |
| 108 | - rl.prompt(); | 140 | +} |
| 109 | - }).on('close', function () { | 141 | + |
| 142 | +function quit() { | ||
| 110 | console.log('quitting...'); | 143 | console.log('quitting...'); |
| 111 | process.exit(0); | 144 | process.exit(0); |
| 145 | +} | ||
| 146 | + | ||
| 147 | +function customMessage(m) { | ||
| 148 | + if (connection) { | ||
| 149 | + console.log('sending message: ' + m); | ||
| 150 | + connection.sendUTF(m); | ||
| 151 | + } else { | ||
| 152 | + console.warn('No current connection.'); | ||
| 153 | + } | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +function showScenarioStatus() { | ||
| 157 | + var msg; | ||
| 158 | + if (!scenario) { | ||
| 159 | + console.log('No scenario selected.'); | ||
| 160 | + } else { | ||
| 161 | + msg = 'Scenario: "' + scenario + '", ' + | ||
| 162 | + (scdone ? 'DONE' : 'next event: ' + evno); | ||
| 163 | + console.log(msg); | ||
| 164 | + } | ||
| 165 | +} | ||
| 166 | + | ||
| 167 | +function scenarioPath(evno) { | ||
| 168 | + var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json'; | ||
| 169 | + return 'ev/' + scenario + file; | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +function setScenario(id) { | ||
| 173 | + if (!id) { | ||
| 174 | + return showScenarioStatus(); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + evdata = null; | ||
| 178 | + scenario = id; | ||
| 179 | + fs.readFile(scenarioPath(), 'utf8', function (err, data) { | ||
| 180 | + if (err) { | ||
| 181 | + console.warn('No scenario named "' + id + '"', err); | ||
| 182 | + scenario = null; | ||
| 183 | + } else { | ||
| 184 | + evdata = JSON.parse(data); | ||
| 185 | + console.log(); // get past prompt | ||
| 186 | + console.log('setting scenario to "' + id + '"'); | ||
| 187 | + console.log(evdata.title); | ||
| 188 | + evdata.description.forEach(function (d) { | ||
| 189 | + console.log(' ' + d); | ||
| 112 | }); | 190 | }); |
| 191 | + evno = 1; | ||
| 192 | + scdone = false; | ||
| 193 | + } | ||
| 194 | + rl.prompt(); | ||
| 195 | + }); | ||
| 196 | +} | ||
| 197 | + | ||
| 198 | +function nextEvent() { | ||
| 199 | + var path; | ||
| 113 | 200 | ||
| 201 | + if (!scenario) { | ||
| 202 | + console.log('No scenario selected.'); | ||
| 203 | + rl.prompt(); | ||
| 204 | + } else if (!connection) { | ||
| 205 | + console.warn('No current connection.'); | ||
| 206 | + rl.prompt(); | ||
| 207 | + } else { | ||
| 208 | + path = scenarioPath(evno); | ||
| 209 | + fs.readFile(path, 'utf8', function (err, data) { | ||
| 210 | + if (err) { | ||
| 211 | + console.log('No event #' + evno); | ||
| 212 | + scdone = true; | ||
| 213 | + console.log('Scenario DONE'); | ||
| 214 | + } else { | ||
| 215 | + evdata = JSON.parse(data); | ||
| 216 | + console.log(); // get past prompt | ||
| 217 | + console.log('sending event #' + evno + ' [' + evdata.event + ']'); | ||
| 218 | + connection.sendUTF(data); | ||
| 219 | + evno++; | ||
| 220 | + } | ||
| 221 | + rl.prompt(); | ||
| 222 | + }); | ||
| 223 | + } | ||
| 114 | } | 224 | } | ... | ... |
-
Please register or login to post a comment