blob: abe602501b64cd9109c555bf1c5761e3567a07ce [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) {
Simon Huntacf410b2015-01-23 10:05:48 -080026 // wrap the onOpen function; we will handle any housekeeping here...
27 if (!fs.isF(f)) {
28 return null;
29 }
30
31 return function (openEvent) {
32 // NOTE: nothing worth passing to the caller?
33 f();
34 };
Simon Hunt970e7fd2015-01-22 17:46:28 -080035 }
36
37 function fnMessage(f) {
38 // wrap the onMessage function; we will attempt to decode the
39 // message event payload as JSON and pass that in...
Simon Huntacf410b2015-01-23 10:05:48 -080040 if (!fs.isF(f)) {
Simon Hunt970e7fd2015-01-22 17:46:28 -080041 return null;
42 }
43
44 return function (msgEvent) {
45 var ev;
46 try {
47 ev = JSON.parse(msgEvent.data);
48 } catch (e) {
49 ev = {
50 error: 'Failed to parse JSON',
51 e: e
52 };
53 }
Simon Huntacf410b2015-01-23 10:05:48 -080054 f(ev);
55 };
Simon Hunt970e7fd2015-01-22 17:46:28 -080056 }
57
58 function fnClose(f) {
Simon Huntacf410b2015-01-23 10:05:48 -080059 // wrap the onClose function; we will handle any parameters to the
60 // close event here...
61 if (!fs.isF(f)) {
62 return null;
63 }
64
65 return function (closeEvent) {
66 // NOTE: only seen {reason == ""} so far, nevertheless...
67 f(closeEvent.reason);
68 };
Simon Hunt970e7fd2015-01-22 17:46:28 -080069 }
70
Simon Hunt584122a2015-01-21 15:32:40 -080071 angular.module('onosRemote')
Simon Huntbb920fd2015-01-22 17:06:32 -080072 .factory('WebSocketService',
73 ['$log', '$location', 'UrlFnService', 'FnService',
74
75 function ($log, $loc, ufs, _fs_) {
Simon Hunt584122a2015-01-21 15:32:40 -080076 fs = _fs_;
77
78 // creates a web socket for the given path, returning a "handle".
Simon Huntacf410b2015-01-23 10:05:48 -080079 // opts contains the event handler callbacks, etc.
Simon Huntbb920fd2015-01-22 17:06:32 -080080 function createWebSocket(path, opts) {
Simon Hunt970e7fd2015-01-22 17:46:28 -080081 var o = opts || {},
82 wsport = opts && opts.wsport,
Simon Huntbb920fd2015-01-22 17:06:32 -080083 fullUrl = ufs.wsUrl(path, wsport),
Simon Hunt584122a2015-01-21 15:32:40 -080084 api = {
Simon Hunt970e7fd2015-01-22 17:46:28 -080085 meta: { path: fullUrl, ws: null },
Simon Hunt584122a2015-01-21 15:32:40 -080086 send: send,
87 close: close
Simon Hunt970e7fd2015-01-22 17:46:28 -080088 },
89 ws;
90
91 try {
92 ws = new WebSocket(fullUrl);
93 api.meta.ws = ws;
94 } catch (e) {
95 }
Simon Hunt584122a2015-01-21 15:32:40 -080096
Simon Huntbb920fd2015-01-22 17:06:32 -080097 $log.debug('Attempting to open websocket to: ' + fullUrl);
98
Simon Hunt970e7fd2015-01-22 17:46:28 -080099 if (ws) {
100 ws.onopen = fnOpen(o.onOpen);
101 ws.onmessage = fnMessage(o.onMessage);
102 ws.onclose = fnClose(o.onClose);
103 }
Simon Hunt584122a2015-01-21 15:32:40 -0800104
Simon Huntacf410b2015-01-23 10:05:48 -0800105 // messages are expected to be event objects..
106 function send(ev) {
107 if (ev) {
Simon Hunt584122a2015-01-21 15:32:40 -0800108 if (ws) {
Simon Huntacf410b2015-01-23 10:05:48 -0800109 ws.send(JSON.stringify(ev));
Simon Hunt584122a2015-01-21 15:32:40 -0800110 } else {
111 $log.warn('ws.send() no web socket open!',
Simon Huntacf410b2015-01-23 10:05:48 -0800112 fullUrl, ev);
Simon Hunt584122a2015-01-21 15:32:40 -0800113 }
114 }
115 }
116
117 function close() {
118 if (ws) {
119 ws.close();
120 ws = null;
Simon Hunt970e7fd2015-01-22 17:46:28 -0800121 api.meta.ws = null;
Simon Hunt584122a2015-01-21 15:32:40 -0800122 }
123 }
124
125 return api;
Simon Hunt1e4a0012015-01-21 11:36:08 -0800126 }
127
128 return {
129 createWebSocket: createWebSocket
130 };
131 }]);
132
133}());