blob: 5533e162959ff8572767acd1e2d5e07666a5759c [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
26 var $log, wss, wes;
27
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) {
45 $log.log(' **** Show Summary **** ', ev.payload);
46 }
47
48 function addInstance(ev) {
49 $log.log(' *** We got an ADD INSTANCE event: ', ev);
50 }
51
Simon Huntfd1231a2015-01-26 22:14:51 -080052 // ==========================
53
54 var dispatcher = {
55 handleEvent: function (ev) {
56 (evHandler[ev.event] || unknownEvent)(ev);
57 },
58 sendEvent: function (evType, payload) {
59 if (wsock) {
60 wes.sendEvent(wsock, evType, payload);
61 } else {
62 $log.warn('sendEvent: no websocket open:', evType, payload);
63 }
64 }
65 };
66
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;
82 }
83
84 // ==========================
85
Simon Huntbb596362015-01-26 21:27:42 -080086 angular.module('ovTopo')
Simon Huntfd1231a2015-01-26 22:14:51 -080087 .factory('TopoEventService',
88 ['$log', '$location', 'WebSocketService', 'WsEventService',
89
90 function (_$log_, $loc, _wss_, _wes_) {
Simon Huntbb596362015-01-26 21:27:42 -080091 $log = _$log_;
Simon Huntfd1231a2015-01-26 22:14:51 -080092 wss = _wss_;
Simon Huntbb596362015-01-26 21:27:42 -080093 wes = _wes_;
94
Simon Huntfd1231a2015-01-26 22:14:51 -080095 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 }
Simon Huntbb596362015-01-26 21:27:42 -0800122
123 return {
Simon Huntfd1231a2015-01-26 22:14:51 -0800124 bindDispatcher: bindDispatcher,
125 openSock: openSock,
126 closeSock: closeSock
Simon Hunt737c89f2015-01-28 12:23:19 -0800127 };
Simon Huntbb596362015-01-26 21:27:42 -0800128 }]);
129}());