Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 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 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.eclipse.jetty.websocket.WebSocket; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 23 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.GlyphConstants; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 27 | import org.onosproject.ui.UiConnection; |
| 28 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiMessageHandlerFactory; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiTopo2OverlayFactory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.UiTopoOverlayFactory; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 34 | import org.onosproject.ui.impl.topo.Topo2Jsonifier; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.impl.topo.Topo2OverlayCache; |
| 36 | import org.onosproject.ui.impl.topo.Topo2ViewMessageHandler; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.impl.topo.UiTopoSession; |
| 38 | import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel; |
| 39 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
| 43 | import java.io.IOException; |
| 44 | import java.util.HashMap; |
| 45 | import java.util.Map; |
| 46 | |
| 47 | /** |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 48 | * Web socket capable of interacting with the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 49 | */ |
| 50 | public class UiWebSocket |
| 51 | implements UiConnection, WebSocket.OnTextMessage, WebSocket.OnControl { |
| 52 | |
| 53 | private static final Logger log = LoggerFactory.getLogger(UiWebSocket.class); |
| 54 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 55 | private static final String EVENT = "event"; |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 56 | private static final String PAYLOAD = "payload"; |
| 57 | private static final String UNKNOWN = "unknown"; |
| 58 | |
| 59 | private static final String ID = "id"; |
| 60 | private static final String IP = "ip"; |
| 61 | private static final String CLUSTER_NODES = "clusterNodes"; |
| 62 | private static final String USER = "user"; |
| 63 | private static final String BOOTSTRAP = "bootstrap"; |
| 64 | |
Simon Hunt | d7395c8 | 2016-10-20 17:54:01 -0700 | [diff] [blame] | 65 | private static final String TOPO = "topo"; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 66 | |
Thomas Vachuska | 1a989c1 | 2015-06-09 18:29:22 -0700 | [diff] [blame] | 67 | private static final long MAX_AGE_MS = 30_000; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 68 | |
| 69 | private static final byte PING = 0x9; |
| 70 | private static final byte PONG = 0xA; |
| 71 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 72 | |
Simon Hunt | 7092cc4 | 2016-04-06 18:40:17 -0700 | [diff] [blame] | 73 | private final ObjectMapper mapper = new ObjectMapper(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 74 | private final ServiceDirectory directory; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 75 | private final UiTopoSession topoSession; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 76 | |
| 77 | private Connection connection; |
| 78 | private FrameConnection control; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 79 | private String userName; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 80 | private String currentView; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 81 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 82 | private long lastActive = System.currentTimeMillis(); |
| 83 | |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 84 | private Map<String, UiMessageHandler> handlers; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 85 | private TopoOverlayCache overlayCache; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 86 | private Topo2OverlayCache overlay2Cache; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 87 | |
| 88 | /** |
Simon Hunt | cda9c03 | 2016-04-11 10:32:54 -0700 | [diff] [blame] | 89 | * Creates a new web-socket for serving data to the Web UI. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 90 | * |
| 91 | * @param directory service directory |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 92 | * @param userName user name of the logged-in user |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 93 | */ |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 94 | public UiWebSocket(ServiceDirectory directory, String userName) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 95 | this.directory = directory; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 96 | this.userName = userName; |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 97 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 98 | Topo2Jsonifier t2json = new Topo2Jsonifier(directory, userName); |
Simon Hunt | 537bc76 | 2016-12-20 12:15:13 -0800 | [diff] [blame] | 99 | UiSharedTopologyModel sharedModel = directory.get(UiSharedTopologyModel.class); |
| 100 | UiTopoLayoutService layoutService = directory.get(UiTopoLayoutService.class); |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 101 | |
Simon Hunt | 8eac4ae | 2017-01-20 12:56:45 -0800 | [diff] [blame] | 102 | sharedModel.injectJsonifier(t2json); |
| 103 | |
Simon Hunt | bbd0f46 | 2017-01-10 14:50:22 -0800 | [diff] [blame] | 104 | topoSession = new UiTopoSession(this, t2json, sharedModel, layoutService); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String userName() { |
| 109 | return userName; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public UiTopoLayout currentLayout() { |
| 114 | return topoSession.currentLayout(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void setCurrentLayout(UiTopoLayout topoLayout) { |
| 119 | topoSession.setCurrentLayout(topoLayout); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String currentView() { |
| 124 | return currentView; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setCurrentView(String viewId) { |
| 129 | currentView = viewId; |
| 130 | topoSession.enableEvent(viewId.equals(TOPO)); |
| 131 | } |
| 132 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 133 | /** |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 134 | * Provides a reference to the topology session. |
| 135 | * |
| 136 | * @return topo session reference |
| 137 | */ |
| 138 | public UiTopoSession topoSession() { |
| 139 | return topoSession; |
| 140 | } |
| 141 | |
| 142 | /** |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 143 | * Issues a close on the connection. |
| 144 | */ |
| 145 | synchronized void close() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 146 | destroyHandlersAndOverlays(); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 147 | if (connection.isOpen()) { |
| 148 | connection.close(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Indicates if this connection is idle. |
| 154 | * |
| 155 | * @return true if idle or closed |
| 156 | */ |
| 157 | synchronized boolean isIdle() { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 158 | long quietFor = System.currentTimeMillis() - lastActive; |
| 159 | boolean idle = quietFor > MAX_AGE_MS; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 160 | if (idle || (connection != null && !connection.isOpen())) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 161 | log.debug("IDLE (or closed) websocket [{} ms]", quietFor); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 162 | return true; |
| 163 | } else if (connection != null) { |
| 164 | try { |
| 165 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 166 | } catch (IOException e) { |
| 167 | log.warn("Unable to send ping message due to: ", e); |
| 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | @Override |
Satish K | 598c28d | 2015-11-24 17:20:40 +0530 | [diff] [blame] | 174 | public synchronized void onOpen(Connection connection) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 175 | this.connection = connection; |
| 176 | this.control = (FrameConnection) connection; |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 177 | try { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 178 | topoSession.init(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 179 | createHandlersAndOverlays(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 180 | sendBootstrapData(); |
| 181 | log.info("GUI client connected -- user <{}>", userName); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 182 | |
| 183 | } catch (ServiceNotFoundException e) { |
Brian O'Connor | 75deea6 | 2015-06-24 16:09:17 -0400 | [diff] [blame] | 184 | log.warn("Unable to open GUI connection; services have been shut-down", e); |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 185 | this.connection.close(); |
| 186 | this.connection = null; |
| 187 | this.control = null; |
| 188 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public synchronized void onClose(int closeCode, String message) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 193 | topoSession.destroy(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 194 | destroyHandlersAndOverlays(); |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 195 | log.info("GUI client disconnected [close-code={}, message={}]", |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 196 | closeCode, message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 201 | lastActive = System.currentTimeMillis(); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void onMessage(String data) { |
| 207 | lastActive = System.currentTimeMillis(); |
| 208 | try { |
| 209 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 210 | String type = message.path(EVENT).asText(UNKNOWN); |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 211 | UiMessageHandler handler = handlers.get(type); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 212 | if (handler != null) { |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 213 | log.debug("RX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 214 | handler.process(message); |
| 215 | } else { |
| 216 | log.warn("No GUI message handler for type {}", type); |
| 217 | } |
| 218 | } catch (Exception e) { |
| 219 | log.warn("Unable to parse GUI message {} due to {}", data, e); |
| 220 | log.debug("Boom!!!", e); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
Thomas Vachuska | 35fa3d4 | 2015-04-30 10:11:47 -0700 | [diff] [blame] | 225 | public synchronized void sendMessage(ObjectNode message) { |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 226 | try { |
| 227 | if (connection.isOpen()) { |
| 228 | connection.sendMessage(message.toString()); |
Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame] | 229 | log.debug("TX message: {}", message); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 230 | } |
| 231 | } catch (IOException e) { |
| 232 | log.warn("Unable to send message {} to GUI due to {}", message, e); |
| 233 | log.debug("Boom!!!", e); |
| 234 | } |
| 235 | } |
| 236 | |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 237 | @Override |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 238 | public synchronized void sendMessage(String type, ObjectNode payload) { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 239 | ObjectNode message = mapper.createObjectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 240 | message.put(EVENT, type); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 241 | message.set(PAYLOAD, payload != null ? payload : mapper.createObjectNode()); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 242 | sendMessage(message); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 245 | // Creates new message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 246 | private synchronized void createHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 247 | log.debug("Creating handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 248 | handlers = new HashMap<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 249 | overlayCache = new TopoOverlayCache(); |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 250 | overlay2Cache = new Topo2OverlayCache(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 251 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 252 | UiExtensionService service = directory.get(UiExtensionService.class); |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 253 | service.getExtensions().forEach(ext -> { |
| 254 | UiMessageHandlerFactory factory = ext.messageHandlerFactory(); |
| 255 | if (factory != null) { |
| 256 | factory.newHandlers().forEach(handler -> { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 257 | try { |
| 258 | handler.init(this, directory); |
| 259 | handler.messageTypes().forEach(type -> handlers.put(type, handler)); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 260 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 261 | // need to inject the overlay cache into topology message handler |
| 262 | if (handler instanceof TopologyViewMessageHandler) { |
| 263 | ((TopologyViewMessageHandler) handler).setOverlayCache(overlayCache); |
| 264 | } |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 265 | |
| 266 | if (handler instanceof Topo2ViewMessageHandler) { |
| 267 | ((Topo2ViewMessageHandler) handler).setOverlayCache(overlay2Cache); |
| 268 | } |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 269 | } catch (Exception e) { |
| 270 | log.warn("Unable to setup handler {} due to", handler, e); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 271 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 272 | }); |
| 273 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 274 | |
| 275 | UiTopoOverlayFactory overlayFactory = ext.topoOverlayFactory(); |
| 276 | if (overlayFactory != null) { |
| 277 | overlayFactory.newOverlays().forEach(overlayCache::add); |
| 278 | } |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 279 | |
| 280 | UiTopo2OverlayFactory overlay2Factory = ext.topo2OverlayFactory(); |
| 281 | if (overlay2Factory != null) { |
| 282 | overlay2Factory.newOverlays().forEach(overlay2Cache::add); |
| 283 | } |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 284 | }); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 285 | log.debug("#handlers = {}, #overlays = {}", handlers.size(), |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 286 | overlayCache.size()); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Destroys message handlers. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 290 | private synchronized void destroyHandlersAndOverlays() { |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 291 | log.debug("Destroying handlers and overlays..."); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 292 | handlers.forEach((type, handler) -> handler.destroy()); |
| 293 | handlers.clear(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 294 | |
| 295 | if (overlayCache != null) { |
| 296 | overlayCache.destroy(); |
| 297 | overlayCache = null; |
| 298 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 299 | } |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 300 | |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 301 | // Sends initial information (username and cluster member information) |
| 302 | // to allow GUI to display logged-in user, and to be able to |
| 303 | // fail-over to an alternate cluster member if necessary. |
| 304 | private void sendBootstrapData() { |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 305 | ClusterService service = directory.get(ClusterService.class); |
| 306 | ArrayNode instances = mapper.createArrayNode(); |
| 307 | |
| 308 | for (ControllerNode node : service.getNodes()) { |
| 309 | ObjectNode instance = mapper.createObjectNode() |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 310 | .put(ID, node.id().toString()) |
| 311 | .put(IP, node.ip().toString()) |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 312 | .put(GlyphConstants.UI_ATTACHED, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 313 | node.equals(service.getLocalNode())); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 314 | instances.add(instance); |
| 315 | } |
| 316 | |
| 317 | ObjectNode payload = mapper.createObjectNode(); |
Simon Hunt | 7715e89 | 2016-04-12 19:55:32 -0700 | [diff] [blame] | 318 | payload.set(CLUSTER_NODES, instances); |
| 319 | payload.put(USER, userName); |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 320 | sendMessage(BOOTSTRAP, payload); |
Thomas Vachuska | b6acc7b | 2015-03-11 09:11:21 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 323 | } |
| 324 | |