Simon Hunt | bb59636 | 2015-01-26 21:27:42 -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 -- Topology Event Module. |
| 19 | Defines event handling for events received from the server. |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 25 | // injected refs |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame^] | 26 | var $log, wss, wes, tps, tis; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 27 | |
| 28 | // internal state |
| 29 | var wsock; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 30 | |
| 31 | var evHandler = { |
| 32 | showSummary: showSummary, |
| 33 | addInstance: addInstance |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 34 | // TODO: implement remaining handlers.. |
| 35 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | function unknownEvent(ev) { |
| 39 | $log.warn('Unknown event (ignored):', ev); |
| 40 | } |
| 41 | |
| 42 | // === Event Handlers === |
| 43 | |
| 44 | function showSummary(ev) { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 45 | $log.debug(' **** Show Summary **** ', ev.payload); |
| 46 | tps.showSummary(ev.payload); |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | function addInstance(ev) { |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame^] | 50 | $log.debug(' **** Add Instance **** ', ev.payload); |
| 51 | tis.addInstance(ev.payload); |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 54 | // ========================== |
| 55 | |
| 56 | var dispatcher = { |
| 57 | handleEvent: function (ev) { |
| 58 | (evHandler[ev.event] || unknownEvent)(ev); |
| 59 | }, |
| 60 | sendEvent: function (evType, payload) { |
| 61 | if (wsock) { |
| 62 | wes.sendEvent(wsock, evType, payload); |
| 63 | } else { |
| 64 | $log.warn('sendEvent: no websocket open:', evType, payload); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | // === Web Socket functions === |
| 70 | |
| 71 | function onWsOpen() { |
| 72 | $log.debug('web socket opened...'); |
| 73 | // kick off request for periodic summary data... |
| 74 | dispatcher.sendEvent('requestSummary'); |
| 75 | } |
| 76 | |
| 77 | function onWsMessage(ev) { |
| 78 | dispatcher.handleEvent(ev); |
| 79 | } |
| 80 | |
| 81 | function onWsClose(reason) { |
| 82 | $log.log('web socket closed; reason=', reason); |
| 83 | wsock = null; |
| 84 | } |
| 85 | |
| 86 | // ========================== |
| 87 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 88 | angular.module('ovTopo') |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 89 | .factory('TopoEventService', |
| 90 | ['$log', '$location', 'WebSocketService', 'WsEventService', |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame^] | 91 | 'TopoPanelService', 'TopoInstService', |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 92 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame^] | 93 | function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_) { |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 94 | $log = _$log_; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 95 | wss = _wss_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 96 | wes = _wes_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 97 | tps = _tps_; |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame^] | 98 | tis = _tis_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 99 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 100 | function bindDispatcher(TopoDomElementsPassedHere) { |
| 101 | // TODO: store refs to topo DOM elements... |
| 102 | |
| 103 | return dispatcher; |
| 104 | } |
| 105 | |
| 106 | // TODO: handle "guiSuccessor" functionality (replace host) |
| 107 | // TODO: implement retry on close functionality |
| 108 | function openSock() { |
| 109 | wsock = wss.createWebSocket('topology', { |
| 110 | onOpen: onWsOpen, |
| 111 | onMessage: onWsMessage, |
| 112 | onClose: onWsClose, |
| 113 | wsport: $loc.search().wsport |
| 114 | }); |
| 115 | $log.debug('web socket opened:', wsock); |
| 116 | } |
| 117 | |
| 118 | function closeSock() { |
| 119 | var path; |
| 120 | if (wsock) { |
| 121 | path = wsock.meta.path; |
| 122 | wsock.close(); |
| 123 | wsock = null; |
| 124 | $log.debug('web socket closed. path:', path); |
| 125 | } |
| 126 | } |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 127 | |
| 128 | return { |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 129 | bindDispatcher: bindDispatcher, |
| 130 | openSock: openSock, |
| 131 | closeSock: closeSock |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 132 | }; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 133 | }]); |
| 134 | }()); |