Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 19 | import org.eclipse.jetty.websocket.WebSocket; |
| 20 | import org.eclipse.jetty.websocket.WebSocketServlet; |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | |
| 24 | import javax.servlet.ServletException; |
| 25 | import javax.servlet.http.HttpServletRequest; |
Simon Hunt | ea32708 | 2016-04-11 16:49:07 -0700 | [diff] [blame] | 26 | import java.security.Principal; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 27 | import java.util.HashSet; |
| 28 | import java.util.Iterator; |
| 29 | import java.util.Set; |
| 30 | import java.util.Timer; |
| 31 | import java.util.TimerTask; |
| 32 | |
| 33 | /** |
| 34 | * Web socket servlet capable of creating web sockets for the user interface. |
| 35 | */ |
| 36 | public class UiWebSocketServlet extends WebSocketServlet { |
| 37 | |
| 38 | private static final long PING_DELAY_MS = 5000; |
| 39 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 40 | private static UiWebSocketServlet instance; |
| 41 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 42 | private ServiceDirectory directory = new DefaultServiceDirectory(); |
| 43 | |
| 44 | private final Set<UiWebSocket> sockets = new HashSet<>(); |
| 45 | private final Timer timer = new Timer(); |
| 46 | private final TimerTask pruner = new Pruner(); |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 47 | private boolean isStopped = false; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 48 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Closes all currently open UI web-sockets. |
| 51 | */ |
| 52 | public static void closeAll() { |
| 53 | if (instance != null) { |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 54 | instance.isStopped = true; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 55 | instance.sockets.forEach(UiWebSocket::close); |
| 56 | instance.sockets.clear(); |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 57 | instance.pruner.cancel(); |
| 58 | instance.timer.cancel(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 62 | @Override |
| 63 | public void init() throws ServletException { |
| 64 | super.init(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 65 | instance = this; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 66 | timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) { |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 71 | if (isStopped) { |
| 72 | return null; |
| 73 | } |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 74 | |
| 75 | // FIXME: Replace this with globally shared opaque token to allow secure failover |
Simon Hunt | ea32708 | 2016-04-11 16:49:07 -0700 | [diff] [blame] | 76 | Principal p = request.getUserPrincipal(); |
| 77 | String userName = p != null ? p.getName() : FAKE_USERNAME; |
| 78 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 79 | UiWebSocket socket = new UiWebSocket(directory, userName); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 80 | synchronized (sockets) { |
| 81 | sockets.add(socket); |
| 82 | } |
| 83 | return socket; |
| 84 | } |
| 85 | |
Simon Hunt | ea32708 | 2016-04-11 16:49:07 -0700 | [diff] [blame] | 86 | // FIXME: This should not be necessary |
| 87 | private static final String FAKE_USERNAME = "UI-user"; |
| 88 | |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Sends the specified message to all the GUI clients. |
| 91 | * |
| 92 | * @param type message type |
| 93 | * @param payload message payload |
| 94 | */ |
| 95 | static void sendToAll(String type, ObjectNode payload) { |
| 96 | if (instance != null) { |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 97 | instance.sockets.forEach(ws -> ws.sendMessage(type, payload)); |
Thomas Vachuska | fa74dd7 | 2016-03-20 19:11:12 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Sends the specified message to all the GUI clients of the specified user. |
| 103 | * |
| 104 | * @param userName user name |
| 105 | * @param type message type |
| 106 | * @param payload message payload |
| 107 | */ |
| 108 | static void sendToUser(String userName, String type, ObjectNode payload) { |
| 109 | if (instance != null) { |
| 110 | instance.sockets.stream().filter(ws -> userName.equals(ws.userName())) |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 111 | .forEach(ws -> ws.sendMessage(type, payload)); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 115 | // Task for pruning web-sockets that are idle. |
| 116 | private class Pruner extends TimerTask { |
| 117 | @Override |
| 118 | public void run() { |
| 119 | synchronized (sockets) { |
| 120 | Iterator<UiWebSocket> it = sockets.iterator(); |
| 121 | while (it.hasNext()) { |
| 122 | UiWebSocket socket = it.next(); |
| 123 | if (socket.isIdle()) { |
| 124 | it.remove(); |
| 125 | socket.close(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |