blob: b99cb6d9c55074f214e16d0503e26d756368767a [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
15 scenario, // test scenario name
16 scdone, // shows when scenario is over
17 evno, // next event number
18 evdata; // event data
19
20
21
22var rl = readline.createInterface(process.stdin, process.stdout);
23rl.setPrompt('ws> ');
Simon Hunta25cdcd2015-01-22 13:11:42 -080024
25
26var 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
32server.listen(port, function() {
33 console.log((new Date()) + ' Server is listening on port ' + port);
34});
35
36server.on('listening', function () {
37 console.log('ok, server is running');
38});
39
40var 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
50function originIsAllowed(origin) {
51 // put logic here to detect whether the specified origin is allowed.
52 return true;
53}
54
Simon Hunta25cdcd2015-01-22 13:11:42 -080055wsServer.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 Hunt711ee242015-01-22 15:08:02 -080066 origin = request.origin;
67 connection = request.accept(null, origin);
Simon Hunta25cdcd2015-01-22 13:11:42 -080068
69
70 console.log((new Date()) + ' Connection accepted.');
71 rl.prompt();
72
Simon Hunta25cdcd2015-01-22 13:11:42 -080073 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 Hunt711ee242015-01-22 15:08:02 -080087 connection = null;
88 origin = null;
Simon Hunta25cdcd2015-01-22 13:11:42 -080089 });
90});
91
92
Simon Hunt711ee242015-01-22 15:08:02 -080093setTimeout(doCli, 10); // allow async processes to write to stdout first
Simon Hunta25cdcd2015-01-22 13:11:42 -080094
95function doCli() {
96 rl.prompt();
97 rl.on('line', function (line) {
98 var words = line.trim().split(' '),
Simon Hunt354c8c92015-01-22 15:32:51 -080099 cmd = words.shift(),
100 str = words.join(' ');
101
102 if (!cmd) {
103 // repeat last command
104 cmd = lastcmd;
105 str = lastargs;
106 }
Simon Hunta25cdcd2015-01-22 13:11:42 -0800107
108 switch(cmd) {
Simon Hunt711ee242015-01-22 15:08:02 -0800109 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 Hunta25cdcd2015-01-22 13:11:42 -0800120
Simon Hunt711ee242015-01-22 15:08:02 -0800121 }).on('close', function () {
122 quit();
123 });
124}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800125
Simon Hunt711ee242015-01-22 15:08:02 -0800126var 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 Hunta25cdcd2015-01-22 13:11:42 -0800135
Simon Hunt711ee242015-01-22 15:08:02 -0800136function showHelp() {
137 console.log(helptext);
138}
139
140function connStatus() {
141 if (connection) {
142 console.log('Connection from ' + origin + ' established.');
143 } else {
144 console.log('No connection.');
145 }
146}
147
148function quit() {
149 console.log('quitting...');
150 process.exit(0);
151}
152
153function 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
162function 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
173function scenarioPath(evno) {
174 var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json';
175 return 'ev/' + scenario + file;
176}
177
178function 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 Hunta25cdcd2015-01-22 13:11:42 -0800199 }
200 rl.prompt();
Simon Hunta25cdcd2015-01-22 13:11:42 -0800201 });
Simon Hunt711ee242015-01-22 15:08:02 -0800202}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800203
Simon Hunt711ee242015-01-22 15:08:02 -0800204function 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 Hunta25cdcd2015-01-22 13:11:42 -0800230}