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 | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 26 | var $log, wss, wes, tps; |
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 | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 50 | $log.debug(' *** We got an ADD INSTANCE event: ', ev); |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 53 | // ========================== |
| 54 | |
| 55 | var dispatcher = { |
| 56 | handleEvent: function (ev) { |
| 57 | (evHandler[ev.event] || unknownEvent)(ev); |
| 58 | }, |
| 59 | sendEvent: function (evType, payload) { |
| 60 | if (wsock) { |
| 61 | wes.sendEvent(wsock, evType, payload); |
| 62 | } else { |
| 63 | $log.warn('sendEvent: no websocket open:', evType, payload); |
| 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | // === Web Socket functions === |
| 69 | |
| 70 | function onWsOpen() { |
| 71 | $log.debug('web socket opened...'); |
| 72 | // kick off request for periodic summary data... |
| 73 | dispatcher.sendEvent('requestSummary'); |
| 74 | } |
| 75 | |
| 76 | function onWsMessage(ev) { |
| 77 | dispatcher.handleEvent(ev); |
| 78 | } |
| 79 | |
| 80 | function onWsClose(reason) { |
| 81 | $log.log('web socket closed; reason=', reason); |
| 82 | wsock = null; |
| 83 | } |
| 84 | |
| 85 | // ========================== |
| 86 | |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 87 | angular.module('ovTopo') |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 88 | .factory('TopoEventService', |
| 89 | ['$log', '$location', 'WebSocketService', 'WsEventService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 90 | 'TopoPanelService', |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 91 | |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 92 | function (_$log_, $loc, _wss_, _wes_, _tps_) { |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 93 | $log = _$log_; |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 94 | wss = _wss_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 95 | wes = _wes_; |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 96 | tps = _tps_; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 97 | |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 98 | function bindDispatcher(TopoDomElementsPassedHere) { |
| 99 | // TODO: store refs to topo DOM elements... |
| 100 | |
| 101 | return dispatcher; |
| 102 | } |
| 103 | |
| 104 | // TODO: handle "guiSuccessor" functionality (replace host) |
| 105 | // TODO: implement retry on close functionality |
| 106 | function openSock() { |
| 107 | wsock = wss.createWebSocket('topology', { |
| 108 | onOpen: onWsOpen, |
| 109 | onMessage: onWsMessage, |
| 110 | onClose: onWsClose, |
| 111 | wsport: $loc.search().wsport |
| 112 | }); |
| 113 | $log.debug('web socket opened:', wsock); |
| 114 | } |
| 115 | |
| 116 | function closeSock() { |
| 117 | var path; |
| 118 | if (wsock) { |
| 119 | path = wsock.meta.path; |
| 120 | wsock.close(); |
| 121 | wsock = null; |
| 122 | $log.debug('web socket closed. path:', path); |
| 123 | } |
| 124 | } |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 125 | |
| 126 | return { |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 127 | bindDispatcher: bindDispatcher, |
| 128 | openSock: openSock, |
| 129 | closeSock: closeSock |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 130 | }; |
Simon Hunt | bb59636 | 2015-01-26 21:27:42 -0800 | [diff] [blame] | 131 | }]); |
| 132 | }()); |