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