Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 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 |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 24 | var $log, $loc, fs, gs, ufs, wsock, vs, ls; |
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 |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 27 | var webSockOpts, // web socket options |
| 28 | ws = null, // web socket reference |
| 29 | wsUp = false, // web socket is good to go |
| 30 | handlers = {}, // event handler bindings |
| 31 | pendingEvents = [], // events TX'd while socket not up |
| 32 | host, // web socket host |
| 33 | url, // web socket URL |
| 34 | clusterNodes = [], // ONOS instances data for failover |
| 35 | clusterIndex = -1, // the instance to which we are connected |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 36 | glyphs = [], |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [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 |
| 40 | loggedInUser = null; // name of logged-in user |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 41 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 42 | // built-in handlers |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 43 | var builtinHandlers = { |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 45 | bootstrap: function (data) { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 46 | $log.debug('bootstrap data', data); |
| 47 | loggedInUser = data.user; |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 48 | clusterNodes = data.clusterNodes; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 49 | clusterNodes.forEach(function (d, i) { |
| 50 | if (d.uiAttached) { |
| 51 | clusterIndex = i; |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 52 | $log.info('Connected to cluster node ' + d.ip); |
| 53 | // TODO: add connect info to masthead somewhere |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 54 | } |
| 55 | }); |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 56 | glyphs = data.glyphs; |
| 57 | glyphs.forEach(function (d, i) { |
| 58 | var gdata = {}; |
| 59 | gdata['_' + d.id] = d.viewbox; |
| 60 | gdata[d.id] = d.path; |
| 61 | gs.registerGlyphs(gdata); |
| 62 | }); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 63 | }, |
| 64 | |
| 65 | error: function (data) { |
| 66 | var m = data.message || 'error from server'; |
| 67 | $log.error(m, data); |
| 68 | |
| 69 | // Unrecoverable error - throw up the veil... |
| 70 | vs && vs.show([ |
| 71 | 'Oops!', |
| 72 | 'Server reports error...', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 73 | m, |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 74 | ]); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 75 | }, |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 76 | }; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 77 | |
Steven Burrows | 530cf46 | 2016-04-12 16:04:37 +0100 | [diff] [blame] | 78 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 79 | // ========================== |
| 80 | // === Web socket callbacks |
| 81 | |
| 82 | function handleOpen() { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 83 | $log.info('Web socket open - ', url); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 84 | vs && vs.hide(); |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 85 | |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 86 | if (fs.debugOn('txrx')) { |
| 87 | $log.debug('Sending ' + pendingEvents.length + ' pending event(s)...'); |
| 88 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 89 | pendingEvents.forEach(function (ev) { |
| 90 | _send(ev); |
| 91 | }); |
| 92 | pendingEvents = []; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 93 | |
| 94 | connectRetries = 0; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 95 | wsUp = true; |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 96 | informListeners(host, url); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Handles the specified (incoming) message using handler bindings. |
| 100 | function handleMessage(msgEvent) { |
| 101 | var ev, h; |
| 102 | |
| 103 | try { |
| 104 | ev = JSON.parse(msgEvent.data); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 105 | } catch (e) { |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 106 | $log.error('Message.data is not valid JSON', msgEvent.data, e); |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 107 | return null; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 108 | } |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 109 | if (fs.debugOn('txrx')) { |
| 110 | $log.debug(' << *Rx* ', ev.event, ev.payload); |
| 111 | } |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 112 | |
| 113 | if (h = handlers[ev.event]) { |
| 114 | try { |
| 115 | h(ev.payload); |
| 116 | } catch (e) { |
| 117 | $log.error('Problem handling event:', ev, e); |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 118 | return null; |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | $log.warn('Unhandled event:', ev); |
| 122 | } |
| 123 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | function handleClose() { |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 127 | var gsucc; |
| 128 | |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 129 | $log.warn('Web socket closed'); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 130 | ls && ls.stop(); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 131 | wsUp = false; |
| 132 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 133 | if (gsucc = findGuiSuccessor()) { |
| 134 | createWebSocket(webSockOpts, gsucc); |
| 135 | } else { |
| 136 | // If no controllers left to contact, show the Veil... |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 137 | vs && vs.show([ |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 138 | 'Oops!', |
| 139 | 'Web-socket connection to server closed...', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 140 | 'Try refreshing the page.', |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 141 | ]); |
| 142 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
| 146 | // ============================== |
| 147 | // === Private Helper Functions |
| 148 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 149 | function findGuiSuccessor() { |
| 150 | var ncn = clusterNodes.length, |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 151 | ip, node; |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 152 | |
| 153 | while (connectRetries < ncn && !ip) { |
| 154 | connectRetries++; |
| 155 | clusterIndex = (clusterIndex + 1) % ncn; |
| 156 | node = clusterNodes[clusterIndex]; |
| 157 | ip = node && node.ip; |
| 158 | } |
| 159 | |
| 160 | return ip; |
| 161 | } |
| 162 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 163 | function informListeners(host, url) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 164 | angular.forEach(openListeners, function (lsnr) { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 165 | lsnr.cb(host, url); |
| 166 | }); |
| 167 | } |
| 168 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 169 | function _send(ev) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 170 | if (fs.debugOn('txrx')) { |
| 171 | $log.debug(' *Tx* >> ', ev.event, ev.payload); |
| 172 | } |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 173 | ws.send(JSON.stringify(ev)); |
| 174 | } |
| 175 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 176 | function noHandlersWarn(handlers, caller) { |
| 177 | if (!handlers || fs.isEmptyObject(handlers)) { |
| 178 | $log.warn('WSS.' + caller + '(): no event handlers'); |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 184 | // =================== |
| 185 | // === API Functions |
| 186 | |
| 187 | // Required for unit tests to set to known state |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 188 | function resetState() { |
| 189 | webSockOpts = undefined; |
| 190 | ws = null; |
| 191 | wsUp = false; |
| 192 | host = undefined; |
| 193 | url = undefined; |
| 194 | pendingEvents = []; |
| 195 | handlers = {}; |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 196 | clusterNodes = []; |
| 197 | clusterIndex = -1; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 198 | glyphs = []; |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 199 | connectRetries = 0; |
| 200 | openListeners = {}; |
| 201 | nextListenerId = 1; |
| 202 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 203 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 204 | // Currently supported opts: |
| 205 | // wsport: web socket port (other than default 8181) |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 206 | // host: if defined, is the host address to use |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 207 | function createWebSocket(opts, _host_) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 208 | var wsport = (opts && opts.wsport) || null; |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 209 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 210 | webSockOpts = opts; // preserved for future calls |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 211 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 212 | host = _host_ || $loc.host(); |
| 213 | url = ufs.wsUrl('core', wsport, _host_); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 214 | |
| 215 | $log.debug('Attempting to open websocket to: ' + url); |
| 216 | ws = wsock.newWebSocket(url); |
| 217 | if (ws) { |
| 218 | ws.onopen = handleOpen; |
| 219 | ws.onmessage = handleMessage; |
| 220 | ws.onclose = handleClose; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 221 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 222 | sendEvent('authentication', { token: onosAuth }); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 223 | } |
| 224 | // Note: Wsock logs an error if the new WebSocket call fails |
Simon Hunt | 2d16fc8 | 2015-03-10 20:19:52 -0700 | [diff] [blame] | 225 | return url; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 228 | // Binds the message handlers to their message type (event type) as |
| 229 | // specified in the given map. Note that keys are the event IDs; values |
| 230 | // are either: |
| 231 | // * the event handler function, or |
| 232 | // * an API object which has an event handler for the key |
| 233 | // |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 234 | function bindHandlers(handlerMap) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 235 | var m, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 236 | dups = []; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 237 | |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 238 | if (noHandlersWarn(handlerMap, 'bindHandlers')) { |
| 239 | return null; |
| 240 | } |
| 241 | m = d3.map(handlerMap); |
| 242 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 243 | m.forEach(function (eventId, api) { |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 244 | var fn = fs.isF(api) || fs.isF(api[eventId]); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 245 | if (!fn) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 246 | $log.warn(eventId + ' handler not a function'); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 247 | return; |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 248 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 249 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 250 | if (handlers[eventId]) { |
| 251 | dups.push(eventId); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 252 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 253 | handlers[eventId] = fn; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 254 | } |
| 255 | }); |
| 256 | if (dups.length) { |
| 257 | $log.warn('duplicate bindings ignored:', dups); |
| 258 | } |
Simon Hunt | 970e7fd | 2015-01-22 17:46:28 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 261 | // Unbinds the specified message handlers. |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 262 | // 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] | 263 | function unbindHandlers(handlerMap) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 264 | var m; |
| 265 | |
| 266 | if (noHandlersWarn(handlerMap, 'unbindHandlers')) { |
| 267 | return null; |
| 268 | } |
| 269 | m = d3.map(handlerMap); |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 270 | |
| 271 | m.forEach(function (eventId) { |
| 272 | delete handlers[eventId]; |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 273 | }); |
| 274 | } |
Simon Hunt | acf410b | 2015-01-23 10:05:48 -0800 | [diff] [blame] | 275 | |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 276 | // TODO: simplify listener handling (see theme.js for sample code) |
| 277 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 278 | function addOpenListener(callback) { |
| 279 | var id = nextListenerId++, |
| 280 | cb = fs.isF(callback), |
| 281 | o = { id: id, cb: cb }; |
| 282 | |
| 283 | if (cb) { |
| 284 | openListeners[id] = o; |
| 285 | } else { |
| 286 | $log.error('WSS.addOpenListener(): callback not a function'); |
| 287 | o.error = 'No callback defined'; |
| 288 | } |
| 289 | return o; |
| 290 | } |
| 291 | |
| 292 | function removeOpenListener(lsnr) { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 293 | var id = fs.isO(lsnr) && lsnr.id, |
| 294 | o; |
| 295 | if (!id) { |
| 296 | $log.warn('WSS.removeOpenListener(): invalid listener', lsnr); |
| 297 | return null; |
| 298 | } |
| 299 | o = openListeners[id]; |
| 300 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 301 | if (o) { |
| 302 | delete openListeners[id]; |
| 303 | } |
| 304 | } |
| 305 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 306 | // Formulates an event message and sends it via the web-socket. |
| 307 | // 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] | 308 | function sendEvent(evType, payload) { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 309 | var ev = { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 310 | event: evType, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 311 | payload: payload || {}, |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | if (wsUp) { |
| 315 | _send(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 316 | } else { |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 317 | pendingEvents.push(ev); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 321 | // Binds the veil service as a delegate |
| 322 | function setVeilDelegate(vd) { |
| 323 | vs = vd; |
| 324 | } |
| 325 | |
| 326 | // Binds the loading service as a delegate |
| 327 | function setLoadingDelegate(ld) { |
| 328 | ls = ld; |
| 329 | } |
| 330 | |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 331 | |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 332 | // ============================ |
| 333 | // ===== Definition of module |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 334 | angular.module('onosRemote') |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 335 | .factory('WebSocketService', |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 336 | ['$log', '$location', 'FnService', 'GlyphService', 'UrlFnService', 'WSock', |
Simon Hunt | bb920fd | 2015-01-22 17:06:32 -0800 | [diff] [blame] | 337 | |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 338 | function (_$log_, _$loc_, _fs_, _gs_, _ufs_, _wsock_) { |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 339 | $log = _$log_; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 340 | $loc = _$loc_; |
| 341 | fs = _fs_; |
Laszlo Papp | 759f0d3 | 2018-03-05 13:24:30 +0000 | [diff] [blame^] | 342 | gs = _gs_; |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 343 | ufs = _ufs_; |
| 344 | wsock = _wsock_; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 345 | |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 346 | bindHandlers(builtinHandlers); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 347 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 348 | return { |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 349 | resetState: resetState, |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 350 | createWebSocket: createWebSocket, |
| 351 | bindHandlers: bindHandlers, |
| 352 | unbindHandlers: unbindHandlers, |
Simon Hunt | 3b9ad04d | 2015-03-11 15:26:02 -0700 | [diff] [blame] | 353 | addOpenListener: addOpenListener, |
| 354 | removeOpenListener: removeOpenListener, |
Simon Hunt | 412adc8 | 2015-12-11 15:56:20 -0800 | [diff] [blame] | 355 | sendEvent: sendEvent, |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 356 | isConnected: function () { return wsUp; }, |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 357 | loggedInUser: function () { return loggedInUser || '(no-one)'; }, |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 358 | |
| 359 | _setVeilDelegate: setVeilDelegate, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 360 | _setLoadingDelegate: setLoadingDelegate, |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 361 | }; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 362 | }, |
Simon Hunt | 20207df | 2015-03-10 18:30:14 -0700 | [diff] [blame] | 363 | ]); |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 364 | |
| 365 | }()); |