Simon Hunt

GUI -- Further work on web socket service. Still WIP.

Change-Id: Ib787a72abed3d8bd1cce7efe110c584a04fe0cd4
...@@ -20,16 +20,47 @@ ...@@ -20,16 +20,47 @@
20 (function () { 20 (function () {
21 'use strict'; 21 'use strict';
22 22
23 + var fs;
24 +
23 angular.module('onosRemote') 25 angular.module('onosRemote')
24 - .factory('WebSocketService', ['$location', 'UrlFnService', 26 + .factory('WebSocketService', ['$location', 'UrlFnService', 'FnService',
25 - function ($loc, ufs) { 27 + function ($loc, ufs, _fs_) {
26 - 28 + fs = _fs_;
27 - // creates a web socket for the given path, returning a "handle" 29 +
28 - function createWebSocket(path) { 30 + // creates a web socket for the given path, returning a "handle".
29 - return { 31 + // cb is the callbacks block.
30 - path: ufs.wsUrl(path) 32 + function createWebSocket(path, cb) {
31 - // TODO: complete implementation... 33 + var fullUrl = ufs.wsUrl(path),
32 - }; 34 + ws = new WebSocket(fullUrl),
35 + api = {
36 + meta: { path: fullUrl, ws: ws },
37 + send: send,
38 + close: close
39 + };
40 +
41 + ws.onopen = (cb && cb.onOpen) || null;
42 + ws.onmessage = (cb && cb.onMessage) || null;
43 + ws.onclose = (cb && cb.onClose) || null;
44 +
45 + function send(msg) {
46 + if (msg) {
47 + if (ws) {
48 + ws.send(msg);
49 + } else {
50 + $log.warn('ws.send() no web socket open!',
51 + fullUrl, msg);
52 + }
53 + }
54 + }
55 +
56 + function close() {
57 + if (ws) {
58 + ws.close();
59 + ws = null;
60 + }
61 + }
62 +
63 + return api;
33 } 64 }
34 65
35 return { 66 return {
......
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
33 } 33 }
34 34
35 .light #ov-topo svg #topo-map { 35 .light #ov-topo svg #topo-map {
36 - stroke: #eee; 36 + /*stroke: #eee;*/
37 - /*stroke: #88b;*/ 37 + stroke: #88b;
38 } 38 }
39 .dark #ov-topo svg #topo-map { 39 .dark #ov-topo svg #topo-map {
40 stroke: #444; 40 stroke: #444;
......
...@@ -131,10 +131,34 @@ ...@@ -131,10 +131,34 @@
131 131
132 // --- Web Socket Connection ----------------------------------------- 132 // --- Web Socket Connection -----------------------------------------
133 133
134 + function onWsOpen() {
135 + $log.log('web socket opened...');
136 +
137 + }
138 +
139 + function onWsMessage(msg) {
140 + $log.log('web socket message...', msg);
141 +
142 + }
143 +
144 + function onWsClose(msg) {
145 + $log.log('web socket closed...', msg);
146 +
147 + }
148 +
134 function setUpWebSocket() { 149 function setUpWebSocket() {
135 - var wsHandle = wss.createWebSocket('topology'); 150 + var wsHandle = wss.createWebSocket('topology', {
151 + onOpen: onWsOpen,
152 + onMessage: onWsMessage,
153 + onClose: onWsClose
154 + });
155 +
156 + // TODO: handle "guiSuccessor" functionality (replace host)
157 + // TODO: implement retry on close functionality
158 +
159 +
136 $log.log('created web socket', wsHandle); 160 $log.log('created web socket', wsHandle);
137 - // TODO: complete implementation 161 + // TODO: complete implementation...
138 162
139 } 163 }
140 164
...@@ -143,11 +167,11 @@ ...@@ -143,11 +167,11 @@
143 angular.module('ovTopo', moduleDependencies) 167 angular.module('ovTopo', moduleDependencies)
144 168
145 .controller('OvTopoCtrl', [ 169 .controller('OvTopoCtrl', [
146 - '$log', 170 + '$scope', '$log',
147 'KeyService', 'ZoomService', 'GlyphService', 'MapService', 171 'KeyService', 'ZoomService', 'GlyphService', 'MapService',
148 'WebSocketService', 172 'WebSocketService',
149 173
150 - function (_$log_, _ks_, _zs_, _gs_, _ms_, _wss_) { 174 + function ($scope, _$log_, _ks_, _zs_, _gs_, _ms_, _wss_) {
151 var self = this; 175 var self = this;
152 $log = _$log_; 176 $log = _$log_;
153 ks = _ks_; 177 ks = _ks_;
...@@ -160,6 +184,13 @@ ...@@ -160,6 +184,13 @@
160 svgResized(svg.style('width'), svg.style('height')); 184 svgResized(svg.style('width'), svg.style('height'));
161 }; 185 };
162 186
187 + $scope.$on('$destroy', function () {
188 + $log.log('OvTopoCtrl is saying Buh-Bye!');
189 + // TODO: cleanup when the scope is destroyed...
190 + // for example, closing the web socket.
191 +
192 + });
193 +
163 // svg layer and initialization of components 194 // svg layer and initialization of components
164 ovtopo = d3.select('#ov-topo'); 195 ovtopo = d3.select('#ov-topo');
165 svg = ovtopo.select('svg'); 196 svg = ovtopo.select('svg');
......
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 -- Remote -- Web Socket Service - Unit Tests
19 + */
20 +describe('factory: fw/remote/websocket.js', function () {
21 + var $log, fs, wss;
22 +
23 + beforeEach(module('onosRemote'));
24 +
25 + beforeEach(module(function($provide) {
26 + $provide.factory('$location', function (){
27 + return {
28 + protocol: function () { return 'http'; },
29 + host: function () { return 'foo'; },
30 + port: function () { return '80'; }
31 + };
32 + })
33 + }));
34 +
35 + beforeEach(inject(function (_$log_, FnService, WebSocketService) {
36 + $log = _$log_;
37 + fs = FnService;
38 + wss = WebSocketService;
39 + }));
40 +
41 +
42 + it('should define WebSocketService', function () {
43 + expect(wss).toBeDefined();
44 + });
45 +
46 + it('should define api functions', function () {
47 + expect(fs.areFunctions(wss, [
48 + 'createWebSocket'
49 + ])).toBeTruthy();
50 + });
51 +
52 + it('should use the appropriate URL', function () {
53 + var ws = wss.createWebSocket('foo/path');
54 + expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/foo/path');
55 + });
56 +
57 + it('should install the appropriate callbacks', function () {
58 + function oo() {}
59 + function om() {}
60 + function oc() {}
61 +
62 + var ws = wss.createWebSocket('foo', {
63 + onOpen: oo,
64 + onMessage: om,
65 + onClose: oc
66 + });
67 +
68 + expect(ws.meta.ws.onopen).toBe(oo);
69 + expect(ws.meta.ws.onmessage).toBe(om);
70 + expect(ws.meta.ws.onclose).toBe(oc);
71 + });
72 +
73 + it('should install partial callbacks', function () {
74 + function oo() {}
75 + function om() {}
76 +
77 + var ws = wss.createWebSocket('foo', {
78 + onOpen: oo,
79 + onMessage: om
80 + });
81 +
82 + expect(ws.meta.ws.onopen).toBe(oo);
83 + expect(ws.meta.ws.onmessage).toBe(om);
84 + expect(ws.meta.ws.onclose).toBeNull();
85 + });
86 +
87 + // TODO: more testing to be done.
88 +
89 +});