Simon Hunt

GUI -- First pass at TopoEventService - to encapsulate sending/receiving events from server.

Change-Id: I604d63a715f1ee25ca7ed05bacccb9eb8d65a0f4
...@@ -73,6 +73,7 @@ ...@@ -73,6 +73,7 @@
73 <!-- {INJECTED-JAVASCRIPT} --> 73 <!-- {INJECTED-JAVASCRIPT} -->
74 <script src="view/sample/sample.js"></script> 74 <script src="view/sample/sample.js"></script>
75 <script src="view/topo/topo.js"></script> 75 <script src="view/topo/topo.js"></script>
76 + <script src="view/topo/topoEvent.js"></script>
76 <script src="view/device/device.js"></script> 77 <script src="view/device/device.js"></script>
77 <!-- TODO: inject javascript refs server-side --> 78 <!-- TODO: inject javascript refs server-side -->
78 79
......
...@@ -28,13 +28,13 @@ ...@@ -28,13 +28,13 @@
28 ]; 28 ];
29 29
30 // references to injected services etc. 30 // references to injected services etc.
31 - var $log, ks, zs, gs, ms, wss, ps; 31 + var $log, ks, zs, gs, ms, ps, wss, tes;
32 32
33 // DOM elements 33 // DOM elements
34 var ovtopo, svg, defs, zoomLayer, map; 34 var ovtopo, svg, defs, zoomLayer, map;
35 35
36 // Internal state 36 // Internal state
37 - var zoomer, wsock; 37 + var zoomer, wsock, evDispatcher;
38 38
39 // Note: "exported" state should be properties on 'self' variable 39 // Note: "exported" state should be properties on 'self' variable
40 40
...@@ -130,20 +130,21 @@ ...@@ -130,20 +130,21 @@
130 } 130 }
131 131
132 // --- Web Socket Connection ----------------------------------------- 132 // --- Web Socket Connection -----------------------------------------
133 + // TODO: migrate this code to be encapsulated by TopoEventService
133 134
134 function onWsOpen() { 135 function onWsOpen() {
135 $log.log('web socket opened...'); 136 $log.log('web socket opened...');
136 - 137 + evDispatcher.sendEvent('requestSummary');
137 } 138 }
138 139
139 function onWsMessage(ev) { 140 function onWsMessage(ev) {
140 - $log.log('got JSON event: ', ev); 141 + evDispatcher.handleEvent(ev);
141 -
142 } 142 }
143 143
144 function onWsClose(reason) { 144 function onWsClose(reason) {
145 $log.log('web socket closed; reason=', reason); 145 $log.log('web socket closed; reason=', reason);
146 - 146 + wsock = null;
147 + tes.bindSock(null);
147 } 148 }
148 149
149 // wsport indicates web-socket-server port other than the default. 150 // wsport indicates web-socket-server port other than the default.
...@@ -156,6 +157,8 @@ ...@@ -156,6 +157,8 @@
156 wsport: wsport 157 wsport: wsport
157 }); 158 });
158 159
160 + tes.bindSock(wsock);
161 +
159 // TODO: handle "guiSuccessor" functionality (replace host) 162 // TODO: handle "guiSuccessor" functionality (replace host)
160 // TODO: implement retry on close functionality 163 // TODO: implement retry on close functionality
161 164
...@@ -172,18 +175,19 @@ ...@@ -172,18 +175,19 @@
172 .controller('OvTopoCtrl', [ 175 .controller('OvTopoCtrl', [
173 '$scope', '$log', '$location', '$timeout', 176 '$scope', '$log', '$location', '$timeout',
174 'KeyService', 'ZoomService', 'GlyphService', 'MapService', 177 'KeyService', 'ZoomService', 'GlyphService', 'MapService',
175 - 'WebSocketService', 'PanelService', 178 + 'PanelService', 'WebSocketService', 'TopoEventService',
176 179
177 function ($scope, _$log_, $loc, $timeout, 180 function ($scope, _$log_, $loc, $timeout,
178 - _ks_, _zs_, _gs_, _ms_, _wss_, _ps_) { 181 + _ks_, _zs_, _gs_, _ms_, _ps_, _wss_, _tes_) {
179 var self = this; 182 var self = this;
180 $log = _$log_; 183 $log = _$log_;
181 ks = _ks_; 184 ks = _ks_;
182 zs = _zs_; 185 zs = _zs_;
183 gs = _gs_; 186 gs = _gs_;
184 ms = _ms_; 187 ms = _ms_;
185 - wss = _wss_;
186 ps = _ps_; 188 ps = _ps_;
189 + wss = _wss_;
190 + tes = _tes_;
187 191
188 self.notifyResize = function () { 192 self.notifyResize = function () {
189 svgResized(svg.style('width'), svg.style('height')); 193 svgResized(svg.style('width'), svg.style('height'));
...@@ -194,6 +198,7 @@ ...@@ -194,6 +198,7 @@
194 $log.log('OvTopoCtrl is saying Buh-Bye!'); 198 $log.log('OvTopoCtrl is saying Buh-Bye!');
195 wsock && wsock.close(); 199 wsock && wsock.close();
196 wsock = null; 200 wsock = null;
201 + tes.bindSock(null);
197 ps.destroyPanel('topo-p-summary'); 202 ps.destroyPanel('topo-p-summary');
198 }); 203 });
199 204
...@@ -201,6 +206,9 @@ ...@@ -201,6 +206,9 @@
201 ovtopo = d3.select('#ov-topo'); 206 ovtopo = d3.select('#ov-topo');
202 svg = ovtopo.select('svg'); 207 svg = ovtopo.select('svg');
203 208
209 + // bind to topo event service..
210 + evDispatcher = tes.dispatcher;
211 +
204 setUpKeys(); 212 setUpKeys();
205 setUpDefs(); 213 setUpDefs();
206 setUpZoom(); 214 setUpZoom();
......
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 +
25 + var $log, wes;
26 +
27 + var evHandler = {
28 + showSummary: showSummary,
29 + addInstance: addInstance
30 + };
31 +
32 + function unknownEvent(ev) {
33 + $log.warn('Unknown event (ignored):', ev);
34 + }
35 +
36 + // === Event Handlers ===
37 +
38 + function showSummary(ev) {
39 + $log.log(' **** Show Summary **** ', ev.payload);
40 + }
41 +
42 + function addInstance(ev) {
43 + $log.log(' *** We got an ADD INSTANCE event: ', ev);
44 + }
45 +
46 + angular.module('ovTopo')
47 + .factory('TopoEventService', ['$log', 'WsEventService',
48 + function (_$log_, _wes_) {
49 + $log = _$log_;
50 + wes = _wes_;
51 +
52 + var wsock;
53 +
54 + return {
55 + 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:',
64 + evType, payload);
65 + }
66 + }
67 + },
68 + bindSock: function (ws) {
69 + wsock = ws;
70 + }
71 + }
72 + }]);
73 +}());