Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 1 | #!/usr/bin/env node |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 2 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 3 | // === Mock Web Socket Server - for testing the topology view |
| 4 | |
| 5 | var fs = require('fs'), |
| 6 | readline = require('readline'), |
| 7 | http = require('http'), |
| 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> '); |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | var server = http.createServer(function(request, response) { |
| 27 | console.log((new Date()) + ' Received request for ' + request.url); |
| 28 | response.writeHead(404); |
| 29 | response.end(); |
| 30 | }); |
| 31 | |
| 32 | server.listen(port, function() { |
| 33 | console.log((new Date()) + ' Server is listening on port ' + port); |
| 34 | }); |
| 35 | |
| 36 | server.on('listening', function () { |
| 37 | console.log('ok, server is running'); |
| 38 | }); |
| 39 | |
| 40 | var wsServer = new WebSocketServer({ |
| 41 | httpServer: server, |
| 42 | // You should not use autoAcceptConnections for production |
| 43 | // applications, as it defeats all standard cross-origin protection |
| 44 | // facilities built into the protocol and the browser. You should |
| 45 | // *always* verify the connection's origin and decide whether or not |
| 46 | // to accept it. |
| 47 | autoAcceptConnections: false |
| 48 | }); |
| 49 | |
| 50 | function originIsAllowed(origin) { |
| 51 | // put logic here to detect whether the specified origin is allowed. |
| 52 | return true; |
| 53 | } |
| 54 | |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 55 | wsServer.on('request', function(request) { |
| 56 | console.log(); // newline after prompt |
| 57 | console.log("Origin: ", request.origin); |
| 58 | |
| 59 | if (!originIsAllowed(request.origin)) { |
| 60 | // Make sure we only accept requests from an allowed origin |
| 61 | request.reject(); |
| 62 | console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); |
| 63 | return; |
| 64 | } |
| 65 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 66 | origin = request.origin; |
| 67 | connection = request.accept(null, origin); |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 68 | |
| 69 | |
| 70 | console.log((new Date()) + ' Connection accepted.'); |
| 71 | rl.prompt(); |
| 72 | |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 73 | connection.on('message', function(message) { |
| 74 | if (message.type === 'utf8') { |
| 75 | console.log(); // newline after prompt |
| 76 | console.log('Received Message: ' + message.utf8Data); |
| 77 | //connection.sendUTF(message.utf8Data); |
| 78 | rl.prompt(); |
| 79 | } |
| 80 | else if (message.type === 'binary') { |
| 81 | console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); |
| 82 | //connection.sendBytes(message.binaryData); |
| 83 | } |
| 84 | }); |
| 85 | connection.on('close', function(reasonCode, description) { |
| 86 | console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 87 | connection = null; |
| 88 | origin = null; |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 89 | }); |
| 90 | }); |
| 91 | |
| 92 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 93 | setTimeout(doCli, 10); // allow async processes to write to stdout first |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 94 | |
| 95 | function doCli() { |
| 96 | rl.prompt(); |
| 97 | rl.on('line', function (line) { |
| 98 | var words = line.trim().split(' '), |
Simon Hunt | 354c8c9 | 2015-01-22 15:32:51 -0800 | [diff] [blame] | 99 | cmd = words.shift(), |
| 100 | str = words.join(' '); |
| 101 | |
| 102 | if (!cmd) { |
| 103 | // repeat last command |
| 104 | cmd = lastcmd; |
| 105 | str = lastargs; |
| 106 | } |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 107 | |
| 108 | switch(cmd) { |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 109 | case 'c': connStatus(); break; |
| 110 | case 'm': customMessage(str); break; |
| 111 | case 's': setScenario(str); break; |
| 112 | case 'n': nextEvent(); break; |
| 113 | case 'q': quit(); break; |
| 114 | case '?': showHelp(); break; |
| 115 | default: console.log('Say what?! (? for help)'); break; |
| 116 | } |
| 117 | lastcmd = cmd; |
| 118 | lastargs = str; |
| 119 | rl.prompt(); |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 120 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 121 | }).on('close', function () { |
| 122 | quit(); |
| 123 | }); |
| 124 | } |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 125 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 126 | var helptext = '\n' + |
| 127 | 'c - show connection status\n' + |
| 128 | 'm {text} - send custom message to client\n' + |
| 129 | 's {id} - set scenario\n' + |
| 130 | 's - show scenario staus\n' + |
| 131 | //'a - auto-send events\n' + |
| 132 | 'n - send next event\n' + |
| 133 | 'q - exit the server\n' + |
| 134 | '? - display this help text\n'; |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 135 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 136 | function showHelp() { |
| 137 | console.log(helptext); |
| 138 | } |
| 139 | |
| 140 | function connStatus() { |
| 141 | if (connection) { |
| 142 | console.log('Connection from ' + origin + ' established.'); |
| 143 | } else { |
| 144 | console.log('No connection.'); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | function quit() { |
| 149 | console.log('quitting...'); |
| 150 | process.exit(0); |
| 151 | } |
| 152 | |
| 153 | function customMessage(m) { |
| 154 | if (connection) { |
| 155 | console.log('sending message: ' + m); |
| 156 | connection.sendUTF(m); |
| 157 | } else { |
| 158 | console.warn('No current connection.'); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | function showScenarioStatus() { |
| 163 | var msg; |
| 164 | if (!scenario) { |
| 165 | console.log('No scenario selected.'); |
| 166 | } else { |
| 167 | msg = 'Scenario: "' + scenario + '", ' + |
| 168 | (scdone ? 'DONE' : 'next event: ' + evno); |
| 169 | console.log(msg); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | function scenarioPath(evno) { |
| 174 | var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json'; |
| 175 | return 'ev/' + scenario + file; |
| 176 | } |
| 177 | |
| 178 | function setScenario(id) { |
| 179 | if (!id) { |
| 180 | return showScenarioStatus(); |
| 181 | } |
| 182 | |
| 183 | evdata = null; |
| 184 | scenario = id; |
| 185 | fs.readFile(scenarioPath(), 'utf8', function (err, data) { |
| 186 | if (err) { |
| 187 | console.warn('No scenario named "' + id + '"', err); |
| 188 | scenario = null; |
| 189 | } else { |
| 190 | evdata = JSON.parse(data); |
| 191 | console.log(); // get past prompt |
| 192 | console.log('setting scenario to "' + id + '"'); |
| 193 | console.log(evdata.title); |
| 194 | evdata.description.forEach(function (d) { |
| 195 | console.log(' ' + d); |
| 196 | }); |
| 197 | evno = 1; |
| 198 | scdone = false; |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 199 | } |
| 200 | rl.prompt(); |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 201 | }); |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 202 | } |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 203 | |
Simon Hunt | 711ee24 | 2015-01-22 15:08:02 -0800 | [diff] [blame] | 204 | function nextEvent() { |
| 205 | var path; |
| 206 | |
| 207 | if (!scenario) { |
| 208 | console.log('No scenario selected.'); |
| 209 | rl.prompt(); |
| 210 | } else if (!connection) { |
| 211 | console.warn('No current connection.'); |
| 212 | rl.prompt(); |
| 213 | } else { |
| 214 | path = scenarioPath(evno); |
| 215 | fs.readFile(path, 'utf8', function (err, data) { |
| 216 | if (err) { |
| 217 | console.log('No event #' + evno); |
| 218 | scdone = true; |
| 219 | console.log('Scenario DONE'); |
| 220 | } else { |
| 221 | evdata = JSON.parse(data); |
| 222 | console.log(); // get past prompt |
| 223 | console.log('sending event #' + evno + ' [' + evdata.event + ']'); |
| 224 | connection.sendUTF(data); |
| 225 | evno++; |
| 226 | } |
| 227 | rl.prompt(); |
| 228 | }); |
| 229 | } |
Simon Hunt | a25cdcd | 2015-01-22 13:11:42 -0800 | [diff] [blame] | 230 | } |