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