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) { |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 26 | // 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 Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 35 | } |
| 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 Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 40 | if (!fs.isF(f)) { |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 41 | 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 Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 54 | f(ev); |
| 55 | }; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | function fnClose(f) { |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 59 | // 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 Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 71 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 72 | .factory('WebSocketService', |
| 73 | ['$log', '$location', 'UrlFnService', 'FnService', |
| 74 | |
| 75 | function ($log, $loc, ufs, _fs_) { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 76 | fs = _fs_; |
| 77 | |
| 78 | // creates a web socket for the given path, returning a "handle". |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 79 | // opts contains the event handler callbacks, etc. |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 80 | function createWebSocket(path, opts) { |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 81 | var o = opts || {}, |
| 82 | wsport = opts && opts.wsport, |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 83 | fullUrl = ufs.wsUrl(path, wsport), |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 84 | api = { |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 85 | meta: { path: fullUrl, ws: null }, |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 86 | send: send, |
| 87 | close: close |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 88 | }, |
| 89 | ws; |
| 90 | |
| 91 | try { |
| 92 | ws = new WebSocket(fullUrl); |
| 93 | api.meta.ws = ws; |
| 94 | } catch (e) { |
| 95 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 96 | |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 97 | $log.debug('Attempting to open websocket to: ' + fullUrl); |
| 98 | |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 99 | if (ws) { |
| 100 | ws.onopen = fnOpen(o.onOpen); |
| 101 | ws.onmessage = fnMessage(o.onMessage); |
| 102 | ws.onclose = fnClose(o.onClose); |
| 103 | } |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 104 | |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 105 | // messages are expected to be event objects.. |
| 106 | function send(ev) { |
| 107 | if (ev) { |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 108 | if (ws) { |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 109 | ws.send(JSON.stringify(ev)); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 110 | } else { |
| 111 | $log.warn('ws.send() no web socket open!', |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 112 | fullUrl, ev); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | function close() { |
| 118 | if (ws) { |
| 119 | ws.close(); |
| 120 | ws = null; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 121 | api.meta.ws = null; |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | return api; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return { |
| 129 | createWebSocket: createWebSocket |
| 130 | }; |
| 131 | }]); |
| 132 | |
| 133 | }()); |