GUI -- Refactored Topo View to move websocket code into TopoEventService.
- now it is more properly encapsulated. Change-Id: Ib5cb88acf63e78be648ff45e737c76041b53e44b
Showing
2 changed files
with
81 additions
and
64 deletions
... | @@ -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, ps, wss, tes; | 31 | + var $log, ks, zs, gs, ms, ps, 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, evDispatcher; | 37 | + var zoomer, 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 | ||
... | @@ -129,44 +129,6 @@ | ... | @@ -129,44 +129,6 @@ |
129 | //showCallibrationPoints(); | 129 | //showCallibrationPoints(); |
130 | } | 130 | } |
131 | 131 | ||
132 | - // --- Web Socket Connection ----------------------------------------- | ||
133 | - // TODO: migrate this code to be encapsulated by TopoEventService | ||
134 | - | ||
135 | - function onWsOpen() { | ||
136 | - $log.log('web socket opened...'); | ||
137 | - evDispatcher.sendEvent('requestSummary'); | ||
138 | - } | ||
139 | - | ||
140 | - function onWsMessage(ev) { | ||
141 | - evDispatcher.handleEvent(ev); | ||
142 | - } | ||
143 | - | ||
144 | - function onWsClose(reason) { | ||
145 | - $log.log('web socket closed; reason=', reason); | ||
146 | - wsock = null; | ||
147 | - tes.bindSock(null); | ||
148 | - } | ||
149 | - | ||
150 | - // wsport indicates web-socket-server port other than the default. | ||
151 | - // Used for testing with the mock-web-socket-server. | ||
152 | - function setUpWebSocket(wsport) { | ||
153 | - wsock = wss.createWebSocket('topology', { | ||
154 | - onOpen: onWsOpen, | ||
155 | - onMessage: onWsMessage, | ||
156 | - onClose: onWsClose, | ||
157 | - wsport: wsport | ||
158 | - }); | ||
159 | - | ||
160 | - tes.bindSock(wsock); | ||
161 | - | ||
162 | - // TODO: handle "guiSuccessor" functionality (replace host) | ||
163 | - // TODO: implement retry on close functionality | ||
164 | - | ||
165 | - | ||
166 | - $log.log('created web socket', wsock); | ||
167 | - // TODO: complete implementation... | ||
168 | - | ||
169 | - } | ||
170 | 132 | ||
171 | // --- Controller Definition ----------------------------------------- | 133 | // --- Controller Definition ----------------------------------------- |
172 | 134 | ||
... | @@ -175,10 +137,10 @@ | ... | @@ -175,10 +137,10 @@ |
175 | .controller('OvTopoCtrl', [ | 137 | .controller('OvTopoCtrl', [ |
176 | '$scope', '$log', '$location', '$timeout', | 138 | '$scope', '$log', '$location', '$timeout', |
177 | 'KeyService', 'ZoomService', 'GlyphService', 'MapService', | 139 | 'KeyService', 'ZoomService', 'GlyphService', 'MapService', |
178 | - 'PanelService', 'WebSocketService', 'TopoEventService', | 140 | + 'PanelService', 'TopoEventService', |
179 | 141 | ||
180 | function ($scope, _$log_, $loc, $timeout, | 142 | function ($scope, _$log_, $loc, $timeout, |
181 | - _ks_, _zs_, _gs_, _ms_, _ps_, _wss_, _tes_) { | 143 | + _ks_, _zs_, _gs_, _ms_, _ps_, _tes_) { |
182 | var self = this; | 144 | var self = this; |
183 | $log = _$log_; | 145 | $log = _$log_; |
184 | ks = _ks_; | 146 | ks = _ks_; |
... | @@ -186,7 +148,6 @@ | ... | @@ -186,7 +148,6 @@ |
186 | gs = _gs_; | 148 | gs = _gs_; |
187 | ms = _ms_; | 149 | ms = _ms_; |
188 | ps = _ps_; | 150 | ps = _ps_; |
189 | - wss = _wss_; | ||
190 | tes = _tes_; | 151 | tes = _tes_; |
191 | 152 | ||
192 | self.notifyResize = function () { | 153 | self.notifyResize = function () { |
... | @@ -196,9 +157,7 @@ | ... | @@ -196,9 +157,7 @@ |
196 | // Cleanup on destroyed scope.. | 157 | // Cleanup on destroyed scope.. |
197 | $scope.$on('$destroy', function () { | 158 | $scope.$on('$destroy', function () { |
198 | $log.log('OvTopoCtrl is saying Buh-Bye!'); | 159 | $log.log('OvTopoCtrl is saying Buh-Bye!'); |
199 | - wsock && wsock.close(); | 160 | + tes.closeSock(); |
200 | - wsock = null; | ||
201 | - tes.bindSock(null); | ||
202 | ps.destroyPanel('topo-p-summary'); | 161 | ps.destroyPanel('topo-p-summary'); |
203 | }); | 162 | }); |
204 | 163 | ||
... | @@ -206,14 +165,16 @@ | ... | @@ -206,14 +165,16 @@ |
206 | ovtopo = d3.select('#ov-topo'); | 165 | ovtopo = d3.select('#ov-topo'); |
207 | svg = ovtopo.select('svg'); | 166 | svg = ovtopo.select('svg'); |
208 | 167 | ||
209 | - // bind to topo event service.. | 168 | + // bind to topo event dispatcher.. |
210 | - evDispatcher = tes.dispatcher; | 169 | + evDispatcher = tes.bindDispatcher('TODO: topo-DOM-elements-here'); |
211 | 170 | ||
212 | setUpKeys(); | 171 | setUpKeys(); |
213 | setUpDefs(); | 172 | setUpDefs(); |
214 | setUpZoom(); | 173 | setUpZoom(); |
215 | setUpMap(); | 174 | setUpMap(); |
216 | - setUpWebSocket($loc.search().wsport); | 175 | + |
176 | + // open up a connection to the server... | ||
177 | + tes.openSock(); | ||
217 | 178 | ||
218 | // TODO: remove this temporary code.... | 179 | // TODO: remove this temporary code.... |
219 | var p = ps.createPanel('topo-p-summary'); | 180 | var p = ps.createPanel('topo-p-summary'); | ... | ... |
... | @@ -22,11 +22,17 @@ | ... | @@ -22,11 +22,17 @@ |
22 | (function () { | 22 | (function () { |
23 | 'use strict'; | 23 | 'use strict'; |
24 | 24 | ||
25 | - var $log, wes; | 25 | + // injected refs |
26 | + var $log, wss, wes; | ||
27 | + | ||
28 | + // internal state | ||
29 | + var wsock; | ||
26 | 30 | ||
27 | var evHandler = { | 31 | var evHandler = { |
28 | showSummary: showSummary, | 32 | showSummary: showSummary, |
29 | addInstance: addInstance | 33 | addInstance: addInstance |
34 | + // TODO: implement remaining handlers.. | ||
35 | + | ||
30 | }; | 36 | }; |
31 | 37 | ||
32 | function unknownEvent(ev) { | 38 | function unknownEvent(ev) { |
... | @@ -43,16 +49,9 @@ | ... | @@ -43,16 +49,9 @@ |
43 | $log.log(' *** We got an ADD INSTANCE event: ', ev); | 49 | $log.log(' *** We got an ADD INSTANCE event: ', ev); |
44 | } | 50 | } |
45 | 51 | ||
46 | - angular.module('ovTopo') | 52 | + // ========================== |
47 | - .factory('TopoEventService', ['$log', 'WsEventService', | ||
48 | - function (_$log_, _wes_) { | ||
49 | - $log = _$log_; | ||
50 | - wes = _wes_; | ||
51 | - | ||
52 | - var wsock; | ||
53 | 53 | ||
54 | - return { | 54 | + var dispatcher = { |
55 | - dispatcher: { | ||
56 | handleEvent: function (ev) { | 55 | handleEvent: function (ev) { |
57 | (evHandler[ev.event] || unknownEvent)(ev); | 56 | (evHandler[ev.event] || unknownEvent)(ev); |
58 | }, | 57 | }, |
... | @@ -60,14 +59,71 @@ | ... | @@ -60,14 +59,71 @@ |
60 | if (wsock) { | 59 | if (wsock) { |
61 | wes.sendEvent(wsock, evType, payload); | 60 | wes.sendEvent(wsock, evType, payload); |
62 | } else { | 61 | } else { |
63 | - $log.warn('sendEvent: no websocket open:', | 62 | + $log.warn('sendEvent: no websocket open:', evType, payload); |
64 | - evType, payload); | ||
65 | } | 63 | } |
66 | } | 64 | } |
67 | - }, | 65 | + }; |
68 | - bindSock: function (ws) { | 66 | + |
69 | - wsock = ws; | 67 | + // === Web Socket functions === |
68 | + | ||
69 | + function onWsOpen() { | ||
70 | + $log.debug('web socket opened...'); | ||
71 | + // kick off request for periodic summary data... | ||
72 | + dispatcher.sendEvent('requestSummary'); | ||
73 | + } | ||
74 | + | ||
75 | + function onWsMessage(ev) { | ||
76 | + dispatcher.handleEvent(ev); | ||
77 | + } | ||
78 | + | ||
79 | + function onWsClose(reason) { | ||
80 | + $log.log('web socket closed; reason=', reason); | ||
81 | + wsock = null; | ||
70 | } | 82 | } |
83 | + | ||
84 | + // ========================== | ||
85 | + | ||
86 | + angular.module('ovTopo') | ||
87 | + .factory('TopoEventService', | ||
88 | + ['$log', '$location', 'WebSocketService', 'WsEventService', | ||
89 | + | ||
90 | + function (_$log_, $loc, _wss_, _wes_) { | ||
91 | + $log = _$log_; | ||
92 | + wss = _wss_; | ||
93 | + wes = _wes_; | ||
94 | + | ||
95 | + function bindDispatcher(TopoDomElementsPassedHere) { | ||
96 | + // TODO: store refs to topo DOM elements... | ||
97 | + | ||
98 | + return dispatcher; | ||
99 | + } | ||
100 | + | ||
101 | + // TODO: handle "guiSuccessor" functionality (replace host) | ||
102 | + // TODO: implement retry on close functionality | ||
103 | + function openSock() { | ||
104 | + wsock = wss.createWebSocket('topology', { | ||
105 | + onOpen: onWsOpen, | ||
106 | + onMessage: onWsMessage, | ||
107 | + onClose: onWsClose, | ||
108 | + wsport: $loc.search().wsport | ||
109 | + }); | ||
110 | + $log.debug('web socket opened:', wsock); | ||
111 | + } | ||
112 | + | ||
113 | + function closeSock() { | ||
114 | + var path; | ||
115 | + if (wsock) { | ||
116 | + path = wsock.meta.path; | ||
117 | + wsock.close(); | ||
118 | + wsock = null; | ||
119 | + $log.debug('web socket closed. path:', path); | ||
120 | + } | ||
121 | + } | ||
122 | + | ||
123 | + return { | ||
124 | + bindDispatcher: bindDispatcher, | ||
125 | + openSock: openSock, | ||
126 | + closeSock: closeSock | ||
71 | } | 127 | } |
72 | }]); | 128 | }]); |
73 | }()); | 129 | }()); | ... | ... |
-
Please register or login to post a comment