blob: 319c4c8074f8c3ee3935bd93e16db7c653ebf24f [file] [log] [blame]
Simon Hunta25cdcd2015-01-22 13:11:42 -08001#!/usr/bin/env node
Simon Hunta25cdcd2015-01-22 13:11:42 -08002
Simon Hunt711ee242015-01-22 15:08:02 -08003// === Mock Web Socket Server - for testing the topology view
4
5var fs = require('fs'),
6 readline = require('readline'),
7 http = require('http'),
8 WebSocketServer = require('websocket').server,
9 port = 8123;
10
11var lastcmd, // last command executed
12 lastargs, // arguments to last command
13 connection, // ws connection
14 origin, // origin of connection
Simon Hunt626d2102015-01-29 11:54:50 -080015 scid, // scenario ID
16 scdata, // scenario data
Simon Hunt711ee242015-01-22 15:08:02 -080017 scdone, // shows when scenario is over
18 evno, // next event number
19 evdata; // event data
20
21
22
23var rl = readline.createInterface(process.stdin, process.stdout);
24rl.setPrompt('ws> ');
Simon Hunta25cdcd2015-01-22 13:11:42 -080025
26
27var server = http.createServer(function(request, response) {
28 console.log((new Date()) + ' Received request for ' + request.url);
29 response.writeHead(404);
30 response.end();
31});
32
33server.listen(port, function() {
34 console.log((new Date()) + ' Server is listening on port ' + port);
35});
36
37server.on('listening', function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -080038 console.log('OK, server is running');
Simon Hunt626d2102015-01-29 11:54:50 -080039 console.log('(? for help)');
Simon Hunta25cdcd2015-01-22 13:11:42 -080040});
41
42var wsServer = new WebSocketServer({
43 httpServer: server,
44 // You should not use autoAcceptConnections for production
45 // applications, as it defeats all standard cross-origin protection
46 // facilities built into the protocol and the browser. You should
47 // *always* verify the connection's origin and decide whether or not
48 // to accept it.
49 autoAcceptConnections: false
50});
51
52function originIsAllowed(origin) {
53 // put logic here to detect whether the specified origin is allowed.
54 return true;
55}
56
Simon Hunta25cdcd2015-01-22 13:11:42 -080057wsServer.on('request', function(request) {
58 console.log(); // newline after prompt
59 console.log("Origin: ", request.origin);
60
61 if (!originIsAllowed(request.origin)) {
62 // Make sure we only accept requests from an allowed origin
63 request.reject();
64 console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
65 return;
66 }
67
Simon Hunt711ee242015-01-22 15:08:02 -080068 origin = request.origin;
69 connection = request.accept(null, origin);
Simon Hunta25cdcd2015-01-22 13:11:42 -080070
71
72 console.log((new Date()) + ' Connection accepted.');
73 rl.prompt();
74
Simon Hunta25cdcd2015-01-22 13:11:42 -080075 connection.on('message', function(message) {
76 if (message.type === 'utf8') {
77 console.log(); // newline after prompt
78 console.log('Received Message: ' + message.utf8Data);
79 //connection.sendUTF(message.utf8Data);
80 rl.prompt();
81 }
82 else if (message.type === 'binary') {
83 console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
84 //connection.sendBytes(message.binaryData);
85 }
86 });
87 connection.on('close', function(reasonCode, description) {
88 console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
Simon Hunt711ee242015-01-22 15:08:02 -080089 connection = null;
90 origin = null;
Simon Hunta25cdcd2015-01-22 13:11:42 -080091 });
92});
93
94
Simon Hunt711ee242015-01-22 15:08:02 -080095setTimeout(doCli, 10); // allow async processes to write to stdout first
Simon Hunta25cdcd2015-01-22 13:11:42 -080096
97function doCli() {
98 rl.prompt();
99 rl.on('line', function (line) {
100 var words = line.trim().split(' '),
Simon Hunt354c8c92015-01-22 15:32:51 -0800101 cmd = words.shift(),
102 str = words.join(' ');
103
104 if (!cmd) {
105 // repeat last command
106 cmd = lastcmd;
107 str = lastargs;
108 }
Simon Hunta25cdcd2015-01-22 13:11:42 -0800109
110 switch(cmd) {
Simon Hunt711ee242015-01-22 15:08:02 -0800111 case 'c': connStatus(); break;
112 case 'm': customMessage(str); break;
113 case 's': setScenario(str); break;
114 case 'n': nextEvent(); break;
Simon Huntb0ec1e52015-01-28 18:13:49 -0800115 case 'r': restartScenario(); break;
Simon Hunt711ee242015-01-22 15:08:02 -0800116 case 'q': quit(); break;
117 case '?': showHelp(); break;
118 default: console.log('Say what?! (? for help)'); break;
119 }
120 lastcmd = cmd;
121 lastargs = str;
122 rl.prompt();
Simon Hunta25cdcd2015-01-22 13:11:42 -0800123
Simon Hunt711ee242015-01-22 15:08:02 -0800124 }).on('close', function () {
125 quit();
126 });
127}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800128
Simon Hunt711ee242015-01-22 15:08:02 -0800129var helptext = '\n' +
130 'c - show connection status\n' +
131 'm {text} - send custom message to client\n' +
Simon Huntb0ec1e52015-01-28 18:13:49 -0800132 's {id} - load scenario {id}\n' +
Simon Hunt711ee242015-01-22 15:08:02 -0800133 's - show scenario staus\n' +
134 //'a - auto-send events\n' +
135 'n - send next event\n' +
Simon Huntb0ec1e52015-01-28 18:13:49 -0800136 'r - restart the scenario\n' +
Simon Hunt711ee242015-01-22 15:08:02 -0800137 'q - exit the server\n' +
138 '? - display this help text\n';
Simon Hunta25cdcd2015-01-22 13:11:42 -0800139
Simon Hunt711ee242015-01-22 15:08:02 -0800140function showHelp() {
141 console.log(helptext);
142}
143
144function connStatus() {
145 if (connection) {
146 console.log('Connection from ' + origin + ' established.');
147 } else {
148 console.log('No connection.');
149 }
150}
151
152function quit() {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800153 console.log('Quitting...');
Simon Hunt711ee242015-01-22 15:08:02 -0800154 process.exit(0);
155}
156
157function customMessage(m) {
158 if (connection) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800159 console.log('Sending message: ' + m);
Simon Hunt711ee242015-01-22 15:08:02 -0800160 connection.sendUTF(m);
161 } else {
162 console.warn('No current connection.');
163 }
164}
165
166function showScenarioStatus() {
167 var msg;
Simon Hunt626d2102015-01-29 11:54:50 -0800168 if (!scid) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800169 console.log('No scenario loaded.');
Simon Hunt711ee242015-01-22 15:08:02 -0800170 } else {
Simon Hunt626d2102015-01-29 11:54:50 -0800171 msg = 'Scenario: "' + scid + '", ' +
Simon Hunt711ee242015-01-22 15:08:02 -0800172 (scdone ? 'DONE' : 'next event: ' + evno);
173 console.log(msg);
174 }
175}
176
177function scenarioPath(evno) {
178 var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json';
Simon Hunt626d2102015-01-29 11:54:50 -0800179 return 'ev/' + scid + file;
180}
181
182
183function initScenario(verb) {
184 console.log(); // get past prompt
185 console.log(verb + ' scenario "' + scid + '"');
186 console.log(scdata.title);
187 scdata.description.forEach(function (d) {
188 console.log(' ' + d);
189 });
190 evno = 1;
191 scdone = false;
Simon Hunt711ee242015-01-22 15:08:02 -0800192}
193
194function setScenario(id) {
195 if (!id) {
196 return showScenarioStatus();
197 }
198
199 evdata = null;
Simon Hunt626d2102015-01-29 11:54:50 -0800200 scid = id;
Simon Hunt711ee242015-01-22 15:08:02 -0800201 fs.readFile(scenarioPath(), 'utf8', function (err, data) {
202 if (err) {
203 console.warn('No scenario named "' + id + '"', err);
Simon Hunt626d2102015-01-29 11:54:50 -0800204 scid = null;
Simon Hunt711ee242015-01-22 15:08:02 -0800205 } else {
Simon Hunt626d2102015-01-29 11:54:50 -0800206 scdata = JSON.parse(data);
207 initScenario('Loading');
Simon Hunta25cdcd2015-01-22 13:11:42 -0800208 }
209 rl.prompt();
Simon Hunta25cdcd2015-01-22 13:11:42 -0800210 });
Simon Hunt711ee242015-01-22 15:08:02 -0800211}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800212
Simon Huntb0ec1e52015-01-28 18:13:49 -0800213function restartScenario() {
Simon Hunt626d2102015-01-29 11:54:50 -0800214 if (!scid) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800215 console.log('No scenario loaded.');
216 } else {
Simon Hunt626d2102015-01-29 11:54:50 -0800217 initScenario('Restarting');
Simon Huntb0ec1e52015-01-28 18:13:49 -0800218 }
219 rl.prompt();
220}
221
Simon Hunt711ee242015-01-22 15:08:02 -0800222function nextEvent() {
223 var path;
224
Simon Hunt626d2102015-01-29 11:54:50 -0800225 if (!scid) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800226 console.log('No scenario loaded.');
Simon Hunt711ee242015-01-22 15:08:02 -0800227 rl.prompt();
228 } else if (!connection) {
229 console.warn('No current connection.');
230 rl.prompt();
231 } else {
232 path = scenarioPath(evno);
233 fs.readFile(path, 'utf8', function (err, data) {
234 if (err) {
235 console.log('No event #' + evno);
236 scdone = true;
237 console.log('Scenario DONE');
238 } else {
239 evdata = JSON.parse(data);
240 console.log(); // get past prompt
Simon Huntb0ec1e52015-01-28 18:13:49 -0800241 console.log('Sending event #' + evno + ' [' + evdata.event + ']');
Simon Hunt711ee242015-01-22 15:08:02 -0800242 connection.sendUTF(data);
243 evno++;
244 }
245 rl.prompt();
246 });
247 }
Simon Hunta25cdcd2015-01-22 13:11:42 -0800248}