blob: 83dcae2d85d7eb64f03177045521ad47e17769fa [file] [log] [blame]
Simon Huntd5b96732016-07-08 13:22:27 -07001/*
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, t2fs;
31
32 // internal state
33 var handlerMap,
34 openListener;
35
36 // TODO: only add heartbeat timer etc. if we really need to be doing that..
37
38 // ========================== Helper Functions
39
40 function createHandlerMap() {
41 handlerMap = {
42 topo2AllInstances: t2fs,
43 topo2CurrentLayout: t2fs,
44 topo2CurrentRegion: t2fs,
45 topo2StartDone: t2fs
46
47 // Add further event names / module references as needed
48 };
49 }
50
51 function wsOpen(host, url) {
52 $log.debug('topo2Event: WSopen - cluster node:', host, 'URL:', url);
53 // tell the server we are ready to receive topo events
54 wss.sendEvent('topo2Start');
55 }
56
57 // bind our event handlers to the web socket service, so that our
58 // callbacks get invoked for incoming events
59 function bindHandlers() {
60 wss.bindHandlers(handlerMap);
61 $log.debug('topo2 event handlers bound');
62 }
63
64 // tell the server we are ready to receive topology events
65 function start() {
66 // in case we fail over to a new server,
67 // listen for wsock-open events
68 openListener = wss.addOpenListener(wsOpen);
69 wss.sendEvent('topo2Start');
70 $log.debug('topo2 comms started');
71 }
72
73 // tell the server we no longer wish to receive topology events
74 function stop() {
75 wss.sendEvent('topo2Stop');
76 wss.unbindHandlers(handlerMap);
77 wss.removeOpenListener(openListener);
78 openListener = null;
79 $log.debug('topo2 comms stopped');
80 }
81
82 // ========================== Main Service Definition
83
84 angular.module('ovTopo2')
85 .factory('Topo2EventService',
86 ['$log', 'WebSocketService', 'Topo2ForceService',
87
88 function (_$log_, _wss_, _t2fs_) {
89 $log = _$log_;
90 wss = _wss_;
91 t2fs = _t2fs_;
92
93 // deferred creation of handler map, so module references are good
94 createHandlerMap();
95
96 return {
97 bindHandlers: bindHandlers,
98 start: start,
99 stop: stop
100 };
101 }]);
102}());