Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 20 | Defines the conduit between the client and the server: |
| 21 | - provides a clean API for sending events to the server |
| 22 | - dispatches incoming events from the server to the appropriate sub-module |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | (function () { |
| 27 | 'use strict'; |
| 28 | |
| 29 | // injected refs |
| 30 | var $log, wss, tXfs; |
| 31 | |
| 32 | // internal state |
| 33 | var handlerMap, |
| 34 | openListener; |
| 35 | |
| 36 | // ========================== Helper Functions |
| 37 | |
| 38 | function createHandlerMap() { |
| 39 | handlerMap = { |
| 40 | topo2AllInstances: tXfs, |
| 41 | topo2CurrentLayout: tXfs, |
| 42 | topo2CurrentRegion: tXfs, |
| 43 | topo2PeerRegions: tXfs, |
| 44 | topo2StartDone: tXfs |
| 45 | |
| 46 | // Add further event names / module references as needed |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | function wsOpen(host, url) { |
| 51 | $log.debug('topoXEvent: WSopen - cluster node:', host, 'URL:', url); |
| 52 | // tell the server we are ready to receive topo events |
| 53 | wss.sendEvent('topo2Start'); |
| 54 | } |
| 55 | |
| 56 | // bind our event handlers to the web socket service, so that our |
| 57 | // callbacks get invoked for incoming events |
| 58 | function bindHandlers() { |
| 59 | wss.bindHandlers(handlerMap); |
| 60 | $log.debug('topoX event handlers bound'); |
| 61 | } |
| 62 | |
| 63 | // tell the server we are ready to receive topology events |
| 64 | function start() { |
| 65 | // in case we fail over to a new server, |
| 66 | // listen for wsock-open events |
| 67 | openListener = wss.addOpenListener(wsOpen); |
| 68 | wss.sendEvent('topo2Start'); |
| 69 | $log.debug('topoX comms started'); |
| 70 | } |
| 71 | |
| 72 | // tell the server we no longer wish to receive topology events |
| 73 | function stop() { |
| 74 | wss.sendEvent('topo2Stop'); |
| 75 | wss.unbindHandlers(handlerMap); |
| 76 | wss.removeOpenListener(openListener); |
| 77 | openListener = null; |
| 78 | $log.debug('topoX comms stopped'); |
| 79 | } |
| 80 | |
| 81 | // ========================== Main Service Definition |
| 82 | |
| 83 | angular.module('ovTopoX') |
| 84 | .factory('TopoXEventService', |
| 85 | ['$log', 'WebSocketService', 'TopoXForceService', |
| 86 | |
| 87 | function (_$log_, _wss_, _tXfs_) { |
| 88 | $log = _$log_; |
| 89 | wss = _wss_; |
| 90 | tXfs = _tXfs_; |
| 91 | |
| 92 | // deferred creation of handler map, so module references are good |
| 93 | createHandlerMap(); |
| 94 | |
| 95 | return { |
| 96 | bindHandlers: bindHandlers, |
| 97 | start: start, |
| 98 | stop: stop |
| 99 | }; |
| 100 | }]); |
| 101 | }()); |