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 | |
| 18 | import org.eclipse.jetty.websocket.WebSocket; |
| 19 | import org.onlab.onos.net.device.DeviceService; |
| 20 | import org.onlab.onos.net.topology.Topology; |
| 21 | import org.onlab.onos.net.topology.TopologyEdge; |
| 22 | import org.onlab.onos.net.topology.TopologyEvent; |
| 23 | import org.onlab.onos.net.topology.TopologyGraph; |
| 24 | import org.onlab.onos.net.topology.TopologyListener; |
| 25 | import org.onlab.onos.net.topology.TopologyService; |
| 26 | import org.onlab.onos.net.topology.TopologyVertex; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
| 28 | |
| 29 | import java.io.IOException; |
| 30 | |
| 31 | /** |
| 32 | * Web socket capable of interacting with the GUI topology view. |
| 33 | */ |
| 34 | public class TopologyWebSocket implements WebSocket.OnTextMessage, TopologyListener { |
| 35 | |
| 36 | private final ServiceDirectory directory; |
| 37 | private final TopologyService topologyService; |
| 38 | private final DeviceService deviceService; |
| 39 | |
| 40 | private Connection connection; |
| 41 | |
| 42 | /** |
| 43 | * Creates a new web-socket for serving data to GUI topology view. |
| 44 | * |
| 45 | * @param directory service directory |
| 46 | */ |
| 47 | public TopologyWebSocket(ServiceDirectory directory) { |
| 48 | this.directory = directory; |
| 49 | topologyService = directory.get(TopologyService.class); |
| 50 | deviceService = directory.get(DeviceService.class); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onOpen(Connection connection) { |
| 55 | this.connection = connection; |
| 56 | |
| 57 | // Register for topology events... |
| 58 | if (topologyService != null && deviceService != null) { |
| 59 | topologyService.addListener(this); |
| 60 | |
| 61 | sendMessage("Yo!!!"); |
| 62 | |
| 63 | Topology topology = topologyService.currentTopology(); |
| 64 | TopologyGraph graph = topologyService.getGraph(topology); |
| 65 | for (TopologyVertex vertex : graph.getVertexes()) { |
| 66 | sendMessage(deviceService.getDevice(vertex.deviceId()).toString()); |
| 67 | } |
| 68 | |
| 69 | for (TopologyEdge edge : graph.getEdges()) { |
| 70 | sendMessage(edge.link().toString()); |
| 71 | } |
| 72 | |
| 73 | sendMessage("That's what we're starting with..."); |
| 74 | |
| 75 | } else { |
| 76 | sendMessage("No topology service!!!"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onClose(int closeCode, String message) { |
| 82 | TopologyService topologyService = directory.get(TopologyService.class); |
| 83 | if (topologyService != null) { |
| 84 | topologyService.removeListener(this); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onMessage(String data) { |
| 90 | System.out.println("Received: " + data); |
| 91 | } |
| 92 | |
| 93 | public void sendMessage(String data) { |
| 94 | try { |
| 95 | connection.sendMessage(data); |
| 96 | } catch (IOException e) { |
| 97 | e.printStackTrace(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void event(TopologyEvent event) { |
| 103 | sendMessage(event.toString()); |
| 104 | } |
| 105 | } |
| 106 | |