blob: 009c048124e8cbb6f3a86567ab13f756dc866f71 [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 Huntb0ec1e52015-01-28 18:13:49 -080026 var $log, wss, wes, tps;
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 Huntb0ec1e52015-01-28 18:13:49 -080050 $log.debug(' *** We got an ADD INSTANCE event: ', ev);
Simon Huntbb596362015-01-26 21:27:42 -080051 }
52
Simon Huntfd1231a2015-01-26 22:14:51 -080053 // ==========================
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 Huntbb596362015-01-26 21:27:42 -080087 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -080088 .factory('TopoEventService',
89 ['$log', '$location', 'WebSocketService', 'WsEventService',
Simon Huntb0ec1e52015-01-28 18:13:49 -080090 'TopoPanelService',
Simon Huntfd1231a2015-01-26 22:14:51 -080091
Simon Huntb0ec1e52015-01-28 18:13:49 -080092 function (_$log_, $loc, _wss_, _wes_, _tps_) {
Simon Huntbb596362015-01-26 21:27:42 -080093 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -080094 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -080095 wes = _wes_;
Simon Huntb0ec1e52015-01-28 18:13:49 -080096 tps = _tps_;
Simon Huntbb596362015-01-26 21:27:42 -080097
Simon Huntfd1231a2015-01-26 22:14:51 -080098 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 Huntbb596362015-01-26 21:27:42 -0800125
126 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800127 bindDispatcher: bindDispatcher,
128 openSock: openSock,
129 closeSock: closeSock
Simon Hunt737c89f2015-01-28 12:23:19 -0800130 };
Simon Huntbb596362015-01-26 21:27:42 -0800131 }]);
132}());