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); |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 83 | return null; |
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); |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 92 | return null; |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 93 | } |
| 94 | } else { |
| 95 | $log.warn('Unhandled event:', ev); |
| 96 | } |
| 97 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | function handleClose() { |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 101 | var gsucc; |
| 102 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 103 | $log.info('Web socket closed'); |
| 104 | wsUp = false; |
| 105 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 106 | if (gsucc = findGuiSuccessor()) { |
| 107 | createWebSocket(webSockOpts, gsucc); |
| 108 | } else { |
| 109 | // If no controllers left to contact, show the Veil... |
| 110 | vs.show([ |
| 111 | 'Oops!', |
| 112 | 'Web-socket connection to server closed...', |
| 113 | 'Try refreshing the page.' |
| 114 | ]); |
| 115 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
| 119 | // ============================== |
| 120 | // === Private Helper Functions |
| 121 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 122 | function findGuiSuccessor() { |
| 123 | var ncn = clusterNodes.length, |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 124 | ip, node; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 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) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 137 | angular.forEach(openListeners, function (lsnr) { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 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 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 147 | function noHandlersWarn(handlers, caller) { |
| 148 | if (!handlers || fs.isEmptyObject(handlers)) { |
| 149 | $log.warn('WSS.' + caller + '(): no event handlers'); |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 155 | // =================== |
| 156 | // === API Functions |
| 157 | |
| 158 | // Required for unit tests to set to known state |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 159 | function resetSid() { |
| 160 | sid = 0; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 161 | } |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 162 | function resetState() { |
| 163 | webSockOpts = undefined; |
| 164 | ws = null; |
| 165 | wsUp = false; |
| 166 | host = undefined; |
| 167 | url = undefined; |
| 168 | pendingEvents = []; |
| 169 | handlers = {}; |
| 170 | sid = 0; |
| 171 | clusterNodes = []; |
| 172 | clusterIndex = -1; |
| 173 | connectRetries = 0; |
| 174 | openListeners = {}; |
| 175 | nextListenerId = 1; |
| 176 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 177 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 178 | // Currently supported opts: |
| 179 | // wsport: web socket port (other than default 8181) |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 180 | // host: if defined, is the host address to use |
| 181 | function createWebSocket(opts, _host_) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 182 | var wsport = (opts && opts.wsport) || null; |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 183 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 184 | webSockOpts = opts; // preserved for future calls |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 185 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 186 | host = _host_ || $loc.host(); |
| 187 | url = ufs.wsUrl('core', wsport, _host_); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 188 | |
| 189 | $log.debug('Attempting to open websocket to: ' + url); |
| 190 | ws = wsock.newWebSocket(url); |
| 191 | if (ws) { |
| 192 | ws.onopen = handleOpen; |
| 193 | ws.onmessage = handleMessage; |
| 194 | ws.onclose = handleClose; |
| 195 | } |
| 196 | // Note: Wsock logs an error if the new WebSocket call fails |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 197 | return url; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 200 | // Binds the message handlers to their message type (event type) as |
| 201 | // specified in the given map. Note that keys are the event IDs; values |
| 202 | // are either: |
| 203 | // * the event handler function, or |
| 204 | // * an API object which has an event handler for the key |
| 205 | // |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 206 | function bindHandlers(handlerMap) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 207 | var m, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 208 | dups = []; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 209 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 210 | if (noHandlersWarn(handlerMap, 'bindHandlers')) { |
| 211 | return null; |
| 212 | } |
| 213 | m = d3.map(handlerMap); |
| 214 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 215 | m.forEach(function (eventId, api) { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 216 | var fn = fs.isF(api) || fs.isF(api[eventId]); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 217 | if (!fn) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 218 | $log.warn(eventId + ' handler not a function'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 219 | return; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 220 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 221 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 222 | if (handlers[eventId]) { |
| 223 | dups.push(eventId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 224 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 225 | handlers[eventId] = fn; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 226 | } |
| 227 | }); |
| 228 | if (dups.length) { |
| 229 | $log.warn('duplicate bindings ignored:', dups); |
| 230 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 233 | // Unbinds the specified message handlers. |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 234 | // 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] | 235 | function unbindHandlers(handlerMap) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 236 | var m; |
| 237 | |
| 238 | if (noHandlersWarn(handlerMap, 'unbindHandlers')) { |
| 239 | return null; |
| 240 | } |
| 241 | m = d3.map(handlerMap); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 242 | |
| 243 | m.forEach(function (eventId) { |
| 244 | delete handlers[eventId]; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 245 | }); |
| 246 | } |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 247 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 248 | function addOpenListener(callback) { |
| 249 | var id = nextListenerId++, |
| 250 | cb = fs.isF(callback), |
| 251 | o = { id: id, cb: cb }; |
| 252 | |
| 253 | if (cb) { |
| 254 | openListeners[id] = o; |
| 255 | } else { |
| 256 | $log.error('WSS.addOpenListener(): callback not a function'); |
| 257 | o.error = 'No callback defined'; |
| 258 | } |
| 259 | return o; |
| 260 | } |
| 261 | |
| 262 | function removeOpenListener(lsnr) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 263 | var id = fs.isO(lsnr) && lsnr.id, |
| 264 | o; |
| 265 | if (!id) { |
| 266 | $log.warn('WSS.removeOpenListener(): invalid listener', lsnr); |
| 267 | return null; |
| 268 | } |
| 269 | o = openListeners[id]; |
| 270 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 271 | if (o) { |
| 272 | delete openListeners[id]; |
| 273 | } |
| 274 | } |
| 275 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 276 | // Formulates an event message and sends it via the web-socket. |
| 277 | // 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] | 278 | function sendEvent(evType, payload) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 279 | var ev = { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 280 | event: evType, |
| 281 | sid: ++sid, |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 282 | payload: payload || {} |
| 283 | }; |
| 284 | |
| 285 | if (wsUp) { |
| 286 | _send(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 287 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 288 | pendingEvents.push(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 293 | // ============================ |
| 294 | // ===== Definition of module |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 295 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 296 | .factory('WebSocketService', |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 297 | ['$log', '$location', 'FnService', 'UrlFnService', 'WSock', |
| 298 | 'VeilService', |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 299 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 300 | function (_$log_, _$loc_, _fs_, _ufs_, _wsock_, _vs_) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 301 | $log = _$log_; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 302 | $loc = _$loc_; |
| 303 | fs = _fs_; |
| 304 | ufs = _ufs_; |
| 305 | wsock = _wsock_; |
| 306 | vs = _vs_; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 307 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 308 | bindHandlers(builtinHandlers); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 309 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 310 | return { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 311 | resetSid: resetSid, |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 312 | resetState: resetState, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 313 | createWebSocket: createWebSocket, |
| 314 | bindHandlers: bindHandlers, |
| 315 | unbindHandlers: unbindHandlers, |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 316 | addOpenListener: addOpenListener, |
| 317 | removeOpenListener: removeOpenListener, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 318 | sendEvent: sendEvent |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 319 | }; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 320 | } |
| 321 | ]); |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 322 | |
| 323 | }()); |