blob: 15e992d2f444b3df0bb82e893fd1737084576f2e [file] [log] [blame]
Simon Hunt1e4a0012015-01-21 11:36:08 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Remote -- Web Socket Service
19 */
20(function () {
21 'use strict';
22
Simon Hunt584122a2015-01-21 15:32:40 -080023 var fs;
Simon Hunt1e4a0012015-01-21 11:36:08 -080024
Simon Hunt970e7fd2015-01-22 17:46:28 -080025 function fnOpen(f) {
26 return fs.isF(f);
27 }
28
29 function fnMessage(f) {
30 // wrap the onMessage function; we will attempt to decode the
31 // message event payload as JSON and pass that in...
32 var fn = fs.isF(f);
33 if (!fn) {
34 return null;
35 }
36
37 return function (msgEvent) {
38 var ev;
39 try {
40 ev = JSON.parse(msgEvent.data);
41 } catch (e) {
42 ev = {
43 error: 'Failed to parse JSON',
44 e: e
45 };
46 }
47 fn(ev);
48 }
49 }
50
51 function fnClose(f) {
52 return fs.isF(f);
53 }
54
Simon Hunt584122a2015-01-21 15:32:40 -080055 angular.module('onosRemote')
Simon Huntbb920fd2015-01-22 17:06:32 -080056 .factory('WebSocketService',
57 ['$log', '$location', 'UrlFnService', 'FnService',
58
59 function ($log, $loc, ufs, _fs_) {
Simon Hunt584122a2015-01-21 15:32:40 -080060 fs = _fs_;
61
62 // creates a web socket for the given path, returning a "handle".
Simon Huntbb920fd2015-01-22 17:06:32 -080063 // opts contains the event handler callbacks.
64 function createWebSocket(path, opts) {
Simon Hunt970e7fd2015-01-22 17:46:28 -080065 var o = opts || {},
66 wsport = opts && opts.wsport,
Simon Huntbb920fd2015-01-22 17:06:32 -080067 fullUrl = ufs.wsUrl(path, wsport),
Simon Hunt584122a2015-01-21 15:32:40 -080068 api = {
Simon Hunt970e7fd2015-01-22 17:46:28 -080069 meta: { path: fullUrl, ws: null },
Simon Hunt584122a2015-01-21 15:32:40 -080070 send: send,
71 close: close
Simon Hunt970e7fd2015-01-22 17:46:28 -080072 },
73 ws;
74
75 try {
76 ws = new WebSocket(fullUrl);
77 api.meta.ws = ws;
78 } catch (e) {
79 }
Simon Hunt584122a2015-01-21 15:32:40 -080080
Simon Huntbb920fd2015-01-22 17:06:32 -080081 $log.debug('Attempting to open websocket to: ' + fullUrl);
82
Simon Hunt970e7fd2015-01-22 17:46:28 -080083 if (ws) {
84 ws.onopen = fnOpen(o.onOpen);
85 ws.onmessage = fnMessage(o.onMessage);
86 ws.onclose = fnClose(o.onClose);
87 }
Simon Hunt584122a2015-01-21 15:32:40 -080088
89 function send(msg) {
90 if (msg) {
91 if (ws) {
92 ws.send(msg);
93 } else {
94 $log.warn('ws.send() no web socket open!',
95 fullUrl, msg);
96 }
97 }
98 }
99
100 function close() {
101 if (ws) {
102 ws.close();
103 ws = null;
Simon Hunt970e7fd2015-01-22 17:46:28 -0800104 api.meta.ws = null;
Simon Hunt584122a2015-01-21 15:32:40 -0800105 }
106 }
107
108 return api;
Simon Hunt1e4a0012015-01-21 11:36:08 -0800109 }
110
111 return {
112 createWebSocket: createWebSocket
113 };
114 }]);
115
116}());