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