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 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 23 | // injected refs |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 24 | var $log, $loc, fs, ufs, wsock, vs; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 25 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 26 | // internal state |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 27 | var ws = null, // web socket reference |
| 28 | wsUp = false, // web socket is good to go |
| 29 | sid = 0, // event sequence identifier |
| 30 | handlers = {}, // event handler bindings |
| 31 | pendingEvents = [], // events TX'd while socket not up |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame^] | 32 | url, // web socket URL |
| 33 | instances = []; |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 34 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame^] | 35 | var builtinHandlers = { |
| 36 | onosInstances: function (data) { |
| 37 | instances = data.instances; |
| 38 | } |
| 39 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 40 | |
| 41 | // ========================== |
| 42 | // === Web socket callbacks |
| 43 | |
| 44 | function handleOpen() { |
| 45 | $log.info('Web socket open'); |
| 46 | $log.debug('Sending ' + pendingEvents.length + ' pending event(s)...'); |
| 47 | pendingEvents.forEach(function (ev) { |
| 48 | _send(ev); |
| 49 | }); |
| 50 | pendingEvents = []; |
| 51 | wsUp = true; |
| 52 | } |
| 53 | |
| 54 | // Handles the specified (incoming) message using handler bindings. |
| 55 | function handleMessage(msgEvent) { |
| 56 | var ev, h; |
| 57 | |
| 58 | try { |
| 59 | ev = JSON.parse(msgEvent.data); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 60 | } catch (e) { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 61 | $log.error('Message.data is not valid JSON', msgEvent.data, e); |
| 62 | return; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 63 | } |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 64 | $log.debug(' *Rx* >> ', ev.event, ev.payload); |
| 65 | |
| 66 | if (h = handlers[ev.event]) { |
| 67 | try { |
| 68 | h(ev.payload); |
| 69 | } catch (e) { |
| 70 | $log.error('Problem handling event:', ev, e); |
| 71 | } |
| 72 | } else { |
| 73 | $log.warn('Unhandled event:', ev); |
| 74 | } |
| 75 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | function handleClose() { |
| 79 | $log.info('Web socket closed'); |
| 80 | wsUp = false; |
| 81 | |
| 82 | // FIXME: implement controller failover logic |
| 83 | |
| 84 | // If no controllers left to contact, show the Veil... |
| 85 | vs.show([ |
| 86 | 'Oops!', |
| 87 | 'Web-socket connection to server closed...', |
| 88 | 'Try refreshing the page.' |
| 89 | ]); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // ============================== |
| 94 | // === Private Helper Functions |
| 95 | |
| 96 | function _send(ev) { |
| 97 | $log.debug(' *Tx* >> ', ev.event, ev.payload); |
| 98 | ws.send(JSON.stringify(ev)); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | // =================== |
| 103 | // === API Functions |
| 104 | |
| 105 | // Required for unit tests to set to known state |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 106 | function resetSid() { |
| 107 | sid = 0; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 110 | // Currently supported opts: |
| 111 | // wsport: web socket port (other than default 8181) |
| 112 | function createWebSocket(opts) { |
| 113 | var wsport = (opts && opts.wsport) || null; |
| 114 | |
| 115 | url = ufs.wsUrl('core', wsport); |
| 116 | |
| 117 | $log.debug('Attempting to open websocket to: ' + url); |
| 118 | ws = wsock.newWebSocket(url); |
| 119 | if (ws) { |
| 120 | ws.onopen = handleOpen; |
| 121 | ws.onmessage = handleMessage; |
| 122 | ws.onclose = handleClose; |
| 123 | } |
| 124 | // Note: Wsock logs an error if the new WebSocket call fails |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 125 | return url; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 128 | // Binds the specified message handlers. |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 129 | // keys are the event IDs |
| 130 | // values are the API on which the handler function is a property |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 131 | function bindHandlers(handlerMap) { |
| 132 | var m = d3.map(handlerMap), |
| 133 | dups = []; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 134 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 135 | m.forEach(function (eventId, api) { |
| 136 | var fn = fs.isF(api[eventId]); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 137 | if (!fn) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 138 | $log.warn(eventId + ' handler not a function'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 139 | return; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 140 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 141 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 142 | if (handlers[eventId]) { |
| 143 | dups.push(eventId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 144 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 145 | handlers[eventId] = fn; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 146 | } |
| 147 | }); |
| 148 | if (dups.length) { |
| 149 | $log.warn('duplicate bindings ignored:', dups); |
| 150 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 153 | // Unbinds the specified message handlers. |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 154 | // Expected that the same map will be used, but we only care about keys |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 155 | function unbindHandlers(handlerMap) { |
| 156 | var m = d3.map(handlerMap); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 157 | |
| 158 | m.forEach(function (eventId) { |
| 159 | delete handlers[eventId]; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 160 | }); |
| 161 | } |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 162 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 163 | // Formulates an event message and sends it via the web-socket. |
| 164 | // If the websocket is not up yet, we store it in a pending list. |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 165 | function sendEvent(evType, payload) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 166 | var ev = { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 167 | event: evType, |
| 168 | sid: ++sid, |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 169 | payload: payload || {} |
| 170 | }; |
| 171 | |
| 172 | if (wsUp) { |
| 173 | _send(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 174 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 175 | pendingEvents.push(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 180 | // ============================ |
| 181 | // ===== Definition of module |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 182 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 183 | .factory('WebSocketService', |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 184 | ['$log', '$location', 'FnService', 'UrlFnService', 'WSock', |
| 185 | 'VeilService', |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 186 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 187 | function (_$log_, _$loc_, _fs_, _ufs_, _wsock_, _vs_) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 188 | $log = _$log_; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 189 | $loc = _$loc_; |
| 190 | fs = _fs_; |
| 191 | ufs = _ufs_; |
| 192 | wsock = _wsock_; |
| 193 | vs = _vs_; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 194 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame^] | 195 | // Bind instance handlers |
| 196 | bindHandlers({ |
| 197 | onosInstances: builtinHandlers |
| 198 | }); |
| 199 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 200 | return { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 201 | resetSid: resetSid, |
| 202 | createWebSocket: createWebSocket, |
| 203 | bindHandlers: bindHandlers, |
| 204 | unbindHandlers: unbindHandlers, |
| 205 | sendEvent: sendEvent |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 206 | }; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 207 | } |
| 208 | ]); |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 209 | |
| 210 | }()); |