Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [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 | 750ab04 | 2015-06-17 10:42:15 -0700 | [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.onlab.stc; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.eclipse.jetty.websocket.WebSocket; |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 21 | |
| 22 | import java.io.IOException; |
| 23 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 24 | import static org.onlab.stc.Coordinator.print; |
| 25 | |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Web socket capable of interacting with the STC monitor GUI. |
| 28 | */ |
| 29 | public class MonitorWebSocket implements WebSocket.OnTextMessage, WebSocket.OnControl { |
| 30 | |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 31 | private static final long MAX_AGE_MS = 30_000; |
| 32 | |
| 33 | private static final byte PING = 0x9; |
| 34 | private static final byte PONG = 0xA; |
| 35 | private static final byte[] PING_DATA = new byte[]{(byte) 0xde, (byte) 0xad}; |
| 36 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 37 | private final Monitor monitor; |
| 38 | |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 39 | private Connection connection; |
| 40 | private FrameConnection control; |
| 41 | |
| 42 | private final ObjectMapper mapper = new ObjectMapper(); |
| 43 | |
| 44 | private long lastActive = System.currentTimeMillis(); |
| 45 | |
| 46 | /** |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 47 | * Creates a new monitor client GUI web-socket. |
| 48 | * |
| 49 | * @param monitor shared process flow monitor |
| 50 | */ |
| 51 | MonitorWebSocket(Monitor monitor) { |
| 52 | this.monitor = monitor; |
| 53 | } |
| 54 | |
| 55 | /** |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 56 | * Issues a close on the connection. |
| 57 | */ |
| 58 | synchronized void close() { |
| 59 | destroyHandlers(); |
| 60 | if (connection.isOpen()) { |
| 61 | connection.close(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Indicates if this connection is idle. |
| 67 | * |
| 68 | * @return true if idle or closed |
| 69 | */ |
| 70 | synchronized boolean isIdle() { |
| 71 | long quietFor = System.currentTimeMillis() - lastActive; |
| 72 | boolean idle = quietFor > MAX_AGE_MS; |
| 73 | if (idle || (connection != null && !connection.isOpen())) { |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 74 | return true; |
| 75 | } else if (connection != null) { |
| 76 | try { |
| 77 | control.sendControl(PING, PING_DATA, 0, PING_DATA.length); |
| 78 | } catch (IOException e) { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 79 | print("Unable to send ping message due to: %s", e); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onOpen(Connection connection) { |
| 87 | this.connection = connection; |
| 88 | this.control = (FrameConnection) connection; |
| 89 | try { |
| 90 | createHandlers(); |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 91 | sendMessage(message("flow", monitor.scenarioData())); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 92 | |
| 93 | } catch (Exception e) { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 94 | print("Unable to open monitor connection: %s", e); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 95 | this.connection.close(); |
| 96 | this.connection = null; |
| 97 | this.control = null; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public synchronized void onClose(int closeCode, String message) { |
| 103 | destroyHandlers(); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean onControl(byte controlCode, byte[] data, int offset, int length) { |
| 108 | lastActive = System.currentTimeMillis(); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onMessage(String data) { |
| 114 | lastActive = System.currentTimeMillis(); |
| 115 | try { |
| 116 | ObjectNode message = (ObjectNode) mapper.reader().readTree(data); |
| 117 | // TODO: |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 118 | print("Got message: %s", message); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 119 | } catch (Exception e) { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 120 | print("Unable to parse GUI message %s due to %s", data, e); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | public synchronized void sendMessage(ObjectNode message) { |
| 125 | try { |
| 126 | if (connection.isOpen()) { |
| 127 | connection.sendMessage(message.toString()); |
| 128 | } |
| 129 | } catch (IOException e) { |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 130 | print("Unable to send message %s to GUI due to %s", message, e); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 134 | public ObjectNode message(String type, ObjectNode payload) { |
| 135 | ObjectNode message = mapper.createObjectNode().put("event", type); |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 136 | message.set("payload", payload); |
Thomas Vachuska | b51b8bc | 2015-07-27 08:37:12 -0700 | [diff] [blame] | 137 | return message; |
Thomas Vachuska | 750ab04 | 2015-06-17 10:42:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Creates new message handlers. |
| 141 | private synchronized void createHandlers() { |
| 142 | } |
| 143 | |
| 144 | // Destroys message handlers. |
| 145 | private synchronized void destroyHandlers() { |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |