2 * Copyright (c) 2013, Intel Corporation.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 /* Dummy backend for faking websocket daemon */
11 var dummyBackend = true;
13 var WS_REQUEST_TYPE = {
31 /* web socket module to connect to the settings daemon */
32 var wsAPI = (function() {
33 /* number of connection retries to attempt if the socket closes */
35 this.connected = false;
36 this.event_callbacks = $.Callbacks();
38 /* default values for WebSocket */
39 this.socketUrl = 'ws://localhost:16000/settings';
40 this.socketProtocol = 'http-only';
43 this.methodCalls = [];
44 for (var i = 0; i < 100; i++) {
45 this.methodCalls[i] = null;
48 this.MethodCall = function(id, name, success_cb, error_cb) {
50 this.successCB = success_cb;
51 this.errorCB = error_cb;
52 this.transactionid = id;
55 this.start = function() {
56 me.timeout = setTimeout(function() {
57 if (me.errorCB !== undefined) {
58 me.errorCB('\"' + me.name + '\" method timed out after ' + self.timeouttime + 'ms');
63 this.finish = function() {
64 if (me.timeout !== undefined) {
65 clearTimeout(me.timeout);
71 this.EventObject = function(type, name, value) {
78 function connect(url, protocol, sucess_cb, error_cb) {
79 self.socketUrl = typeof url !== 'undefined' ? url : self.socketUrl;
80 self.socketProtocol = typeof protocol !== 'undefined' ? protocol : self.socketProtocol;
81 self.successCB = sucess_cb;
82 self.errorCB = error_cb;
84 if ('WebSocket' in window) {
85 if (self.socketProtocol.length > 0) {
86 self.socket = new WebSocket(self.socketUrl, self.socketProtocol);
88 self.socket = new WebSocket(self);
91 self.socket.onopen = function() {
92 self.connected = true;
93 console.log('socket opened');
97 self.socket.onclose = function() {
98 self.connected = false;
99 console.log('socket closed');
102 /* fake the connection for now */
103 self.connected = true;
108 self.socket.onerror = function(e) {
109 console.log('socket error: ', e.data);
113 self.socket.onmessage = function(e) {
114 self.receive(e.data);
117 console.log('Websockets not supported');
121 function send(msg, success_cb, error_cb) {
122 if (!this.connected) {
123 if (errorCB !== undefined) {
124 errorCB('\"' + obj.name + '\" method failed because socket is closed');
128 var i = this.methodIdx;
129 this.methodIdx = (this.methodIdx + 1) % 100;
130 this.methodCalls[i] = new this.MethodCall(msg.transactionid, msg.name, success_cb, error_cb);
131 this.methodCalls[i].start();
132 console.log('methodCalls[', i, '].start()');
134 /* fake with dummy data */
135 dummyBackendSend(msg);
137 this.socket.send(JSON.stringify(msg));
141 function fireEvent(type, name, value) {
142 var event = new this.EventObject(type, name, value);
143 console.log('Fire event: [' + type + ', ' + name + ', ' + value + ']');
144 event_callbacks.fire(event);
147 function receive(msg) {
151 event = JSON.parse(msg);
153 self.iErrorCB('GARBAGE MESSAGE: ' + msg);
157 if ((event === undefined) || (event.type === undefined) || (event.name === undefined)) {
158 self.iErrorCB('BADLY FORMED MESSAGE: ' + msg);
161 if (event.type === 'methodReply') {
162 var calls = this.methodCalls;
163 for (var i = 0; i < calls.length; i++) {
165 if (call && (!call.done) && (call.transactionid === event.transactionid)) {
167 console.log('methodCalls[', i, '].finish()');
168 if (event.error !== undefined) {
169 call.errorCB(event.error);
171 if (event.data !== undefined && call.successCB !== undefined) {
172 call.successCB(event.data);
178 else if (event.type === 'event') {
179 self.fireSevent(event.name, event.data);
184 function generateTransactionId() {
186 for (i = 0; i < 8; i++) {
187 var num = Math.floor((Math.random() + 1) * 65536);
188 val[i] = num.toString(16).substring(1);
190 var uuid = val[0] + val[1] + '-' + val[2] + '-' + val[3] + '-' + val[4] + '-' + val[5] + val[6] + val[7];
194 function sendRequest(request_type, request_name, request_args, success_cb, error_cb) {
196 'type': request_type,
197 'name': request_name,
198 'transactionid': generateTransactionId(),
202 console.log('wsAPI.sendRequest(): ', request_type);
203 console.log('request name: ', request_name);
204 console.log('request args: ', request_args);
205 send(msg, success_cb, error_cb);
208 function subscribeEvents(callback) {
209 event_callbacks.add(callback);
212 function unsubscribeEvents(callback) {
213 event_callbacks.remove(callback);
216 /* this is dummy data for testing purposes */
217 function dummyBackendSend(msg) {
219 console.log('Sending to dummy server');
221 var calls = this.methodCalls;
224 for (var i = 0; i < calls.length; i++) {
226 if (call && (!call.done) && (call.transactionid === msg.transactionid)) {
228 console.log('methodCalls[', i, '].finish()');
229 if (msg.error !== undefined) {
230 call.errorCB(msg.error);
232 if (msg.data !== undefined && call.successCB && call.errorCB !== undefined) {
233 console.log('Send request to dummy backend: type - ', msg.type);
234 console.log('Send request to dummy backend: name - ', msg.name);
235 console.log('Send request to dummy backend: args - ', msg.data);
238 case WS_REQUEST_TYPE.BLUETOOTH:
239 if (msg.name === 'enable') {
240 fireEvent(WS_EVENT_TYPE.BLUETOOTH, 'enabled', true);
241 } else if (msg.name === 'disable') {
242 fireEvent(WS_EVENT_TYPE.BLUETOOTH, 'enabled', false);
245 call.errorCB('Unsupported request name: ' + msg.name);
249 case WS_REQUEST_TYPE.WIFI:
250 case WS_REQUEST_TYPE.DISPLAY:
251 case WS_REQUEST_TYPE.SOUND:
252 case WS_REQUEST_TYPE.DATETIME:
253 case WS_REQUEST_TYPE.LOCALE:
254 call.errorCB('Request not implemented');
257 call.errorCB('Invalid request type: ' + msg.type);
260 call.successCB(replyMsg);
270 sendRequest: sendRequest,
271 subscribeEvents: subscribeEvents