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 | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 27 | var webSockOpts, // web socket options |
| 28 | ws = null, // web socket reference |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 29 | wsUp = false, // web socket is good to go |
| 30 | sid = 0, // event sequence identifier |
| 31 | handlers = {}, // event handler bindings |
| 32 | pendingEvents = [], // events TX'd while socket not up |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 33 | host, // web socket host |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 34 | url, // web socket URL |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 35 | clusterNodes = [], // ONOS instances data for failover |
| 36 | clusterIndex = -1, // the instance to which we are connected |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 37 | connectRetries = 0, // limit our attempts at reconnecting |
| 38 | openListeners = {}, // registered listeners for websocket open() |
| 39 | nextListenerId = 1; // internal ID for open listeners |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 40 | |
| 41 | // ======================= |
| 42 | // === Bootstrap Handler |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 43 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 44 | var builtinHandlers = { |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 45 | bootstrap: function (data) { |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 46 | clusterNodes = data.clusterNodes; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 47 | clusterNodes.forEach(function (d, i) { |
| 48 | if (d.uiAttached) { |
| 49 | clusterIndex = i; |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 50 | $log.info('Connected to cluster node ' + d.ip); |
| 51 | // TODO: add connect info to masthead somewhere |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 52 | } |
| 53 | }); |
| 54 | } |
| 55 | }; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 56 | |
| 57 | // ========================== |
| 58 | // === Web socket callbacks |
| 59 | |
| 60 | function handleOpen() { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 61 | $log.info('Web socket open - ', url); |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 62 | vs.hide(); |
| 63 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 64 | $log.debug('Sending ' + pendingEvents.length + ' pending event(s)...'); |
| 65 | pendingEvents.forEach(function (ev) { |
| 66 | _send(ev); |
| 67 | }); |
| 68 | pendingEvents = []; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 69 | |
| 70 | connectRetries = 0; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 71 | wsUp = true; |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 72 | informListeners(host, url); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Handles the specified (incoming) message using handler bindings. |
| 76 | function handleMessage(msgEvent) { |
| 77 | var ev, h; |
| 78 | |
| 79 | try { |
| 80 | ev = JSON.parse(msgEvent.data); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 81 | } catch (e) { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 82 | $log.error('Message.data is not valid JSON', msgEvent.data, e); |
| 83 | return; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 84 | } |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 85 | $log.debug(' << *Rx* ', ev.event, ev.payload); |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 86 | |
| 87 | if (h = handlers[ev.event]) { |
| 88 | try { |
| 89 | h(ev.payload); |
| 90 | } catch (e) { |
| 91 | $log.error('Problem handling event:', ev, e); |
| 92 | } |
| 93 | } else { |
| 94 | $log.warn('Unhandled event:', ev); |
| 95 | } |
| 96 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | function handleClose() { |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 100 | var gsucc; |
| 101 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 102 | $log.info('Web socket closed'); |
| 103 | wsUp = false; |
| 104 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 105 | if (gsucc = findGuiSuccessor()) { |
| 106 | createWebSocket(webSockOpts, gsucc); |
| 107 | } else { |
| 108 | // If no controllers left to contact, show the Veil... |
| 109 | vs.show([ |
| 110 | 'Oops!', |
| 111 | 'Web-socket connection to server closed...', |
| 112 | 'Try refreshing the page.' |
| 113 | ]); |
| 114 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | // ============================== |
| 119 | // === Private Helper Functions |
| 120 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 121 | function findGuiSuccessor() { |
| 122 | var ncn = clusterNodes.length, |
| 123 | ip = undefined, |
| 124 | node; |
| 125 | |
| 126 | while (connectRetries < ncn && !ip) { |
| 127 | connectRetries++; |
| 128 | clusterIndex = (clusterIndex + 1) % ncn; |
| 129 | node = clusterNodes[clusterIndex]; |
| 130 | ip = node && node.ip; |
| 131 | } |
| 132 | |
| 133 | return ip; |
| 134 | } |
| 135 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 136 | function informListeners(host, url) { |
| 137 | angular.forEach(openListeners, function(lsnr) { |
| 138 | lsnr.cb(host, url); |
| 139 | }); |
| 140 | } |
| 141 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 142 | function _send(ev) { |
| 143 | $log.debug(' *Tx* >> ', ev.event, ev.payload); |
| 144 | ws.send(JSON.stringify(ev)); |
| 145 | } |
| 146 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 147 | // =================== |
| 148 | // === API Functions |
| 149 | |
| 150 | // Required for unit tests to set to known state |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 151 | function resetSid() { |
| 152 | sid = 0; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 155 | // Currently supported opts: |
| 156 | // wsport: web socket port (other than default 8181) |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 157 | // host: if defined, is the host address to use |
| 158 | function createWebSocket(opts, _host_) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 159 | var wsport = (opts && opts.wsport) || null; |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 160 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 161 | webSockOpts = opts; // preserved for future calls |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 162 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 163 | host = _host_ || $loc.host(); |
| 164 | url = ufs.wsUrl('core', wsport, _host_); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 165 | |
| 166 | $log.debug('Attempting to open websocket to: ' + url); |
| 167 | ws = wsock.newWebSocket(url); |
| 168 | if (ws) { |
| 169 | ws.onopen = handleOpen; |
| 170 | ws.onmessage = handleMessage; |
| 171 | ws.onclose = handleClose; |
| 172 | } |
| 173 | // Note: Wsock logs an error if the new WebSocket call fails |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 174 | return url; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 177 | // Binds the message handlers to their message type (event type) as |
| 178 | // specified in the given map. Note that keys are the event IDs; values |
| 179 | // are either: |
| 180 | // * the event handler function, or |
| 181 | // * an API object which has an event handler for the key |
| 182 | // |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 183 | function bindHandlers(handlerMap) { |
| 184 | var m = d3.map(handlerMap), |
| 185 | dups = []; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 186 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 187 | m.forEach(function (eventId, api) { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 188 | var fn = fs.isF(api) || fs.isF(api[eventId]); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 189 | if (!fn) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 190 | $log.warn(eventId + ' handler not a function'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 191 | return; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 192 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 193 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 194 | if (handlers[eventId]) { |
| 195 | dups.push(eventId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 196 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 197 | handlers[eventId] = fn; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 198 | } |
| 199 | }); |
| 200 | if (dups.length) { |
| 201 | $log.warn('duplicate bindings ignored:', dups); |
| 202 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 205 | // Unbinds the specified message handlers. |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 206 | // 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] | 207 | function unbindHandlers(handlerMap) { |
| 208 | var m = d3.map(handlerMap); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 209 | |
| 210 | m.forEach(function (eventId) { |
| 211 | delete handlers[eventId]; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 212 | }); |
| 213 | } |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 214 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 215 | function addOpenListener(callback) { |
| 216 | var id = nextListenerId++, |
| 217 | cb = fs.isF(callback), |
| 218 | o = { id: id, cb: cb }; |
| 219 | |
| 220 | if (cb) { |
| 221 | openListeners[id] = o; |
| 222 | } else { |
| 223 | $log.error('WSS.addOpenListener(): callback not a function'); |
| 224 | o.error = 'No callback defined'; |
| 225 | } |
| 226 | return o; |
| 227 | } |
| 228 | |
| 229 | function removeOpenListener(lsnr) { |
| 230 | var id = lsnr && lsnr.id, |
| 231 | o = openListeners[id]; |
| 232 | if (o) { |
| 233 | delete openListeners[id]; |
| 234 | } |
| 235 | } |
| 236 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 237 | // Formulates an event message and sends it via the web-socket. |
| 238 | // 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] | 239 | function sendEvent(evType, payload) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 240 | var ev = { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 241 | event: evType, |
| 242 | sid: ++sid, |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 243 | payload: payload || {} |
| 244 | }; |
| 245 | |
| 246 | if (wsUp) { |
| 247 | _send(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 248 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 249 | pendingEvents.push(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 254 | // ============================ |
| 255 | // ===== Definition of module |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 256 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 257 | .factory('WebSocketService', |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 258 | ['$log', '$location', 'FnService', 'UrlFnService', 'WSock', |
| 259 | 'VeilService', |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 260 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 261 | function (_$log_, _$loc_, _fs_, _ufs_, _wsock_, _vs_) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 262 | $log = _$log_; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 263 | $loc = _$loc_; |
| 264 | fs = _fs_; |
| 265 | ufs = _ufs_; |
| 266 | wsock = _wsock_; |
| 267 | vs = _vs_; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 268 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 269 | bindHandlers(builtinHandlers); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 270 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 271 | return { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 272 | resetSid: resetSid, |
| 273 | createWebSocket: createWebSocket, |
| 274 | bindHandlers: bindHandlers, |
| 275 | unbindHandlers: unbindHandlers, |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 276 | addOpenListener: addOpenListener, |
| 277 | removeOpenListener: removeOpenListener, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 278 | sendEvent: sendEvent |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 279 | }; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 280 | } |
| 281 | ]); |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 282 | |
| 283 | }()); |