blob: 7bb29dc62aa8523b7adb56793c6a022f3c47a4ce [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 Hunt711ee242015-01-22 15:08:02 -080099 cmd = words.shift() || lastcmd,
100 str = words.join(' ') || lastargs;
Simon Hunta25cdcd2015-01-22 13:11:42 -0800101
102 switch(cmd) {
Simon Hunt711ee242015-01-22 15:08:02 -0800103 case 'c': connStatus(); break;
104 case 'm': customMessage(str); 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();
Simon Hunta25cdcd2015-01-22 13:11:42 -0800114
Simon Hunt711ee242015-01-22 15:08:02 -0800115 }).on('close', function () {
116 quit();
117 });
118}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800119
Simon Hunt711ee242015-01-22 15:08:02 -0800120var helptext = '\n' +
121 'c - show connection status\n' +
122 'm {text} - send custom message to client\n' +
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';
Simon Hunta25cdcd2015-01-22 13:11:42 -0800129
Simon Hunt711ee242015-01-22 15:08:02 -0800130function showHelp() {
131 console.log(helptext);
132}
133
134function connStatus() {
135 if (connection) {
136 console.log('Connection from ' + origin + ' established.');
137 } else {
138 console.log('No connection.');
139 }
140}
141
142function quit() {
143 console.log('quitting...');
144 process.exit(0);
145}
146
147function 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
156function 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
167function scenarioPath(evno) {
168 var file = evno ? ('/ev_' + evno + '_onos.json') : '/scenario.json';
169 return 'ev/' + scenario + file;
170}
171
172function 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);
190 });
191 evno = 1;
192 scdone = false;
Simon Hunta25cdcd2015-01-22 13:11:42 -0800193 }
194 rl.prompt();
Simon Hunta25cdcd2015-01-22 13:11:42 -0800195 });
Simon Hunt711ee242015-01-22 15:08:02 -0800196}
Simon Hunta25cdcd2015-01-22 13:11:42 -0800197
Simon Hunt711ee242015-01-22 15:08:02 -0800198function nextEvent() {
199 var path;
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 }
Simon Hunta25cdcd2015-01-22 13:11:42 -0800224}