blob: fcff4e4f890c235fea10a09c3b5766bbc18a6dde [file] [log] [blame]
Simon Huntbb596362015-01-26 21:27:42 -08001/*
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 Huntfd1231a2015-01-26 22:14:51 -080025 // injected refs
Simon Hunt4b668592015-01-29 17:33:53 -080026 var $log, wss, wes, tps, tis;
Simon Huntfd1231a2015-01-26 22:14:51 -080027
28 // internal state
29 var wsock;
Simon Huntbb596362015-01-26 21:27:42 -080030
31 var evHandler = {
32 showSummary: showSummary,
33 addInstance: addInstance
Simon Huntfd1231a2015-01-26 22:14:51 -080034 // TODO: implement remaining handlers..
35
Simon Huntbb596362015-01-26 21:27:42 -080036 };
37
38 function unknownEvent(ev) {
39 $log.warn('Unknown event (ignored):', ev);
40 }
41
42 // === Event Handlers ===
43
44 function showSummary(ev) {
Simon Huntb0ec1e52015-01-28 18:13:49 -080045 $log.debug(' **** Show Summary **** ', ev.payload);
46 tps.showSummary(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080047 }
48
49 function addInstance(ev) {
Simon Hunt4b668592015-01-29 17:33:53 -080050 $log.debug(' **** Add Instance **** ', ev.payload);
51 tis.addInstance(ev.payload);
Simon Huntbb596362015-01-26 21:27:42 -080052 }
53
Simon Huntfd1231a2015-01-26 22:14:51 -080054 // ==========================
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 Huntbb596362015-01-26 21:27:42 -080088 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -080089 .factory('TopoEventService',
90 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Hunt4b668592015-01-29 17:33:53 -080091 'TopoPanelService', 'TopoInstService',
Simon Huntfd1231a2015-01-26 22:14:51 -080092
Simon Hunt4b668592015-01-29 17:33:53 -080093 function (_$log_, $loc, _wss_, _wes_, _tps_, _tis_) {
Simon Huntbb596362015-01-26 21:27:42 -080094 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -080095 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -080096 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -080097 tps = _tps_;
Simon Hunt4b668592015-01-29 17:33:53 -080098 tis = _tis_;
Simon Huntbb596362015-01-26 21:27:42 -080099
Simon Huntfd1231a2015-01-26 22:14:51 -0800100 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 Huntbb596362015-01-26 21:27:42 -0800127
128 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800129 bindDispatcher: bindDispatcher,
130 openSock: openSock,
131 closeSock: closeSock
Simon Hunt737c89f2015-01-28 12:23:19 -0800132 };
Simon Huntbb596362015-01-26 21:27:42 -0800133 }]);
134}());