Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 23 | var fs; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 24 | |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 25 | 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 Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 55 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 56 | .factory('WebSocketService', |
| 57 | ['$log', '$location', 'UrlFnService', 'FnService', |
| 58 | |
| 59 | function ($log, $loc, ufs, _fs_) { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 60 | fs = _fs_; |
| 61 | |
| 62 | // creates a web socket for the given path, returning a "handle". |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 63 | // opts contains the event handler callbacks. |
| 64 | function createWebSocket(path, opts) { |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 65 | var o = opts || {}, |
| 66 | wsport = opts && opts.wsport, |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 67 | fullUrl = ufs.wsUrl(path, wsport), |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 68 | api = { |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 69 | meta: { path: fullUrl, ws: null }, |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 70 | send: send, |
| 71 | close: close |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 72 | }, |
| 73 | ws; |
| 74 | |
| 75 | try { |
| 76 | ws = new WebSocket(fullUrl); |
| 77 | api.meta.ws = ws; |
| 78 | } catch (e) { |
| 79 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 80 | |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 81 | $log.debug('Attempting to open websocket to: ' + fullUrl); |
| 82 | |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 83 | if (ws) { |
| 84 | ws.onopen = fnOpen(o.onOpen); |
| 85 | ws.onmessage = fnMessage(o.onMessage); |
| 86 | ws.onclose = fnClose(o.onClose); |
| 87 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 88 | |
| 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 Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame^] | 104 | api.meta.ws = null; |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | return api; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return { |
| 112 | createWebSocket: createWebSocket |
| 113 | }; |
| 114 | }]); |
| 115 | |
| 116 | }()); |