Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | package org.onlab.onos.gui; |
| 17 | |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 19 | import org.eclipse.jetty.websocket.WebSocket; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 20 | import org.onlab.onos.cluster.ClusterEvent; |
| 21 | import org.onlab.onos.cluster.ClusterEventListener; |
| 22 | import org.onlab.onos.cluster.ControllerNode; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 23 | import org.onlab.onos.core.ApplicationId; |
| 24 | import org.onlab.onos.core.CoreService; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 25 | import org.onlab.onos.mastership.MastershipEvent; |
| 26 | import org.onlab.onos.mastership.MastershipListener; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 27 | import org.onlab.onos.net.Device; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 28 | import org.onlab.onos.net.Host; |
| 29 | import org.onlab.onos.net.HostId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 30 | import org.onlab.onos.net.Link; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 31 | import org.onlab.onos.net.Path; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 32 | import org.onlab.onos.net.device.DeviceEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 34 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 35 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 36 | import org.onlab.onos.net.host.HostEvent; |
| 37 | import org.onlab.onos.net.host.HostListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 38 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 39 | import org.onlab.onos.net.intent.Intent; |
| 40 | import org.onlab.onos.net.intent.IntentEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 41 | import org.onlab.onos.net.intent.IntentId; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 42 | import org.onlab.onos.net.intent.IntentListener; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 43 | import org.onlab.onos.net.intent.PathIntent; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 44 | import org.onlab.onos.net.link.LinkEvent; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 45 | import org.onlab.onos.net.link.LinkListener; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 46 | import org.onlab.osgi.ServiceDirectory; |
| 47 | |
| 48 | import java.io.IOException; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 49 | import java.util.List; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 50 | import java.util.Map; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 51 | import java.util.concurrent.ConcurrentHashMap; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 52 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 53 | import static org.onlab.onos.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 54 | import static org.onlab.onos.net.DeviceId.deviceId; |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 55 | import static org.onlab.onos.net.HostId.hostId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 56 | import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 57 | import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 58 | import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 59 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Web socket capable of interacting with the GUI topology view. |
| 62 | */ |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 63 | public class TopologyWebSocket |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 64 | extends TopologyMessages |
| 65 | implements WebSocket.OnTextMessage, WebSocket.OnControl { |
| 66 | |
| 67 | private static final long MAX_AGE_MS = 15000; |
| 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}; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 72 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 73 | private static final String APP_ID = "org.onlab.onos.gui"; |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 74 | |
| 75 | private final ApplicationId appId; |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 76 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 77 | private Connection connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 78 | private FrameConnection control; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 79 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 80 | private final ClusterEventListener clusterListener = new InternalClusterListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 81 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 82 | private final LinkListener linkListener = new InternalLinkListener(); |
| 83 | private final HostListener hostListener = new InternalHostListener(); |
| 84 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 85 | private final IntentListener intentListener = new InternalIntentListener(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 86 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 87 | // Intents that are being monitored for the GUI |
| 88 | private static Map<IntentId, Long> intentsToMonitor = new ConcurrentHashMap<>(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 89 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 90 | private long lastActive = System.currentTimeMillis(); |
| 91 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Creates a new web-socket for serving data to GUI topology view. |
| 94 | * |
| 95 | * @param directory service directory |
| 96 | */ |
| 97 | public TopologyWebSocket(ServiceDirectory directory) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 98 | super(directory); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 99 | appId = directory.get(CoreService.class).registerApplication(APP_ID); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Issues a close on the connection. |
| 104 | */ |
| 105 | synchronized void close() { |
| 106 | if (connection.isOpen()) { |
| 107 | removeListeners(); |
| 108 | connection.close(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Indicates if this connection is idle. |
| 114 | */ |
| 115 | synchronized boolean isIdle() { |
| 116 | boolean idle = (System.currentTimeMillis() - lastActive) > MAX_AGE_MS; |
| 117 | if (idle || !connection.isOpen()) { |
| 118 | return true; |
| 119 | } |
| 120 | try { |
| 121 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 122 | } catch (IOException e) { |
| 123 | log.warn("Unable to send ping message due to: ", e); |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 128 | @Override |
| 129 | public void onOpen(Connection connection) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 130 | log.info("GUI client connected"); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 131 | this.connection = connection; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 132 | this.control = (FrameConnection) connection; |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 133 | addListeners(); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 134 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 135 | sendAllInstances(); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 136 | sendAllDevices(); |
| 137 | sendAllLinks(); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 138 | sendAllHosts(); |
| 139 | } |
| 140 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 141 | @Override |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 142 | public synchronized void onClose(int closeCode, String message) { |
| 143 | if (connection.isOpen()) { |
| 144 | removeListeners(); |
| 145 | } |
| 146 | log.info("GUI client disconnected"); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 151 | lastActive = System.currentTimeMillis(); |
| 152 | return true; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onMessage(String data) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 157 | lastActive = System.currentTimeMillis(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 158 | try { |
| 159 | ObjectNode event = (ObjectNode) mapper.reader().readTree(data); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 160 | String type = string(event, "event", "unknown"); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 161 | if (type.equals("requestDetails")) { |
| 162 | requestDetails(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 163 | } else if (type.equals("updateMeta")) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 164 | updateMetaUi(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 165 | } else if (type.equals("requestPath")) { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 166 | createHostIntent(event); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 167 | } else if (type.equals("requestTraffic")) { |
| 168 | sendTraffic(event); |
| 169 | } else if (type.equals("cancelTraffic")) { |
| 170 | cancelTraffic(event); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 171 | } |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 172 | } catch (Exception e) { |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 173 | log.warn("Unable to parse GUI request {} due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 174 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 177 | // Sends the specified data to the client. |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 178 | private synchronized void sendMessage(ObjectNode data) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 179 | try { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 180 | if (connection.isOpen()) { |
| 181 | connection.sendMessage(data.toString()); |
| 182 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 183 | } catch (IOException e) { |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 184 | log.warn("Unable to send message {} to GUI due to {}", data, e); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 185 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 188 | // Sends all controller nodes to the client as node-added messages. |
| 189 | private void sendAllInstances() { |
| 190 | for (ControllerNode node : clusterService.getNodes()) { |
| 191 | sendMessage(instanceMessage(new ClusterEvent(INSTANCE_ADDED, node))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 192 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 195 | // Sends all devices to the client as device-added messages. |
| 196 | private void sendAllDevices() { |
| 197 | for (Device device : deviceService.getDevices()) { |
| 198 | sendMessage(deviceMessage(new DeviceEvent(DEVICE_ADDED, device))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 199 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 202 | // Sends all links to the client as link-added messages. |
| 203 | private void sendAllLinks() { |
| 204 | for (Link link : linkService.getLinks()) { |
| 205 | sendMessage(linkMessage(new LinkEvent(LINK_ADDED, link))); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 206 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Sends all hosts to the client as host-added messages. |
| 210 | private void sendAllHosts() { |
| 211 | for (Host host : hostService.getHosts()) { |
| 212 | sendMessage(hostMessage(new HostEvent(HOST_ADDED, host))); |
| 213 | } |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Sends back device or host details. |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 217 | private void requestDetails(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 218 | ObjectNode payload = payload(event); |
Thomas Vachuska | f1fae00 | 2014-11-11 18:22:02 -0800 | [diff] [blame] | 219 | String type = string(payload, "class", "unknown"); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 220 | if (type.equals("device")) { |
| 221 | sendMessage(deviceDetails(deviceId(string(payload, "id")), |
| 222 | number(event, "sid"))); |
| 223 | } else if (type.equals("host")) { |
| 224 | sendMessage(hostDetails(hostId(string(payload, "id")), |
| 225 | number(event, "sid"))); |
| 226 | } |
| 227 | } |
| 228 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 229 | // Creates host-to-host intent. |
| 230 | private void createHostIntent(ObjectNode event) { |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 231 | ObjectNode payload = payload(event); |
| 232 | long id = number(event, "sid"); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 233 | // TODO: add protection against device ids and non-existent hosts. |
| 234 | HostId one = hostId(string(payload, "one")); |
| 235 | HostId two = hostId(string(payload, "two")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 236 | |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 237 | HostToHostIntent hostIntent = new HostToHostIntent(appId, one, two, |
| 238 | DefaultTrafficSelector.builder().build(), |
| 239 | DefaultTrafficTreatment.builder().build()); |
| 240 | intentsToMonitor.put(hostIntent.id(), number(event, "sid")); |
| 241 | intentService.submit(hostIntent); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Sends traffic message. |
| 245 | private void sendTraffic(ObjectNode event) { |
| 246 | ObjectNode payload = payload(event); |
| 247 | long id = number(event, "sid"); |
| 248 | IntentId intentId = IntentId.valueOf(payload.path("intentId").asLong()); |
| 249 | |
| 250 | if (payload != null) { |
| 251 | payload.put("traffic", true); |
| 252 | sendMessage(envelope("showPath", id, payload)); |
| 253 | } else { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 254 | sendMessage(warning(id, "No path found")); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | // Cancels sending traffic messages. |
| 259 | private void cancelTraffic(ObjectNode event) { |
| 260 | // TODO: implement this |
| 261 | } |
| 262 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 263 | |
| 264 | // Adds all internal listeners. |
| 265 | private void addListeners() { |
| 266 | clusterService.addListener(clusterListener); |
| 267 | deviceService.addListener(deviceListener); |
| 268 | linkService.addListener(linkListener); |
| 269 | hostService.addListener(hostListener); |
| 270 | mastershipService.addListener(mastershipListener); |
| 271 | intentService.addListener(intentListener); |
| 272 | } |
| 273 | |
| 274 | // Removes all internal listeners. |
| 275 | private void removeListeners() { |
| 276 | clusterService.removeListener(clusterListener); |
| 277 | deviceService.removeListener(deviceListener); |
| 278 | linkService.removeListener(linkListener); |
| 279 | hostService.removeListener(hostListener); |
| 280 | mastershipService.removeListener(mastershipListener); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 281 | intentService.removeListener(intentListener); |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Cluster event listener. |
| 285 | private class InternalClusterListener implements ClusterEventListener { |
| 286 | @Override |
| 287 | public void event(ClusterEvent event) { |
| 288 | sendMessage(instanceMessage(event)); |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 292 | // Device event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 293 | private class InternalDeviceListener implements DeviceListener { |
| 294 | @Override |
| 295 | public void event(DeviceEvent event) { |
| 296 | sendMessage(deviceMessage(event)); |
| 297 | } |
| 298 | } |
| 299 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 300 | // Link event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 301 | private class InternalLinkListener implements LinkListener { |
| 302 | @Override |
| 303 | public void event(LinkEvent event) { |
| 304 | sendMessage(linkMessage(event)); |
| 305 | } |
| 306 | } |
| 307 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 308 | // Host event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 309 | private class InternalHostListener implements HostListener { |
| 310 | @Override |
| 311 | public void event(HostEvent event) { |
| 312 | sendMessage(hostMessage(event)); |
| 313 | } |
| 314 | } |
| 315 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 316 | // Mastership event listener. |
Thomas Vachuska | 690e5f6 | 2014-11-09 08:26:47 -0800 | [diff] [blame] | 317 | private class InternalMastershipListener implements MastershipListener { |
| 318 | @Override |
| 319 | public void event(MastershipEvent event) { |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 320 | // TODO: Is DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED the same? |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 321 | |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 322 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 323 | } |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 324 | |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 325 | // Intent event listener. |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 326 | private class InternalIntentListener implements IntentListener { |
| 327 | @Override |
| 328 | public void event(IntentEvent event) { |
| 329 | Intent intent = event.subject(); |
| 330 | Long sid = intentsToMonitor.get(intent.id()); |
| 331 | if (sid != null) { |
| 332 | List<Intent> installable = intentService.getInstallableIntents(intent.id()); |
| 333 | if (installable != null && !installable.isEmpty()) { |
| 334 | PathIntent pathIntent = (PathIntent) installable.iterator().next(); |
| 335 | Path path = pathIntent.path(); |
Thomas Vachuska | 0f6baee | 2014-11-11 15:02:32 -0800 | [diff] [blame] | 336 | ObjectNode payload = pathMessage(path, "host") |
| 337 | .put("intentId", intent.id().toString()); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 338 | sendMessage(envelope("showPath", sid, payload)); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
Thomas Vachuska | a7c3dd1 | 2014-11-11 09:10:19 -0800 | [diff] [blame] | 343 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 344 | } |
| 345 | |