Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.onosproject.ui.impl; |
| 17 | |
| 18 | import org.eclipse.jetty.websocket.WebSocket; |
| 19 | import org.eclipse.jetty.websocket.WebSocketServlet; |
| 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | |
| 23 | import javax.servlet.ServletException; |
| 24 | import javax.servlet.http.HttpServletRequest; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Iterator; |
| 27 | import java.util.Set; |
| 28 | import java.util.Timer; |
| 29 | import java.util.TimerTask; |
| 30 | |
| 31 | /** |
| 32 | * Web socket servlet capable of creating web sockets for the user interface. |
| 33 | */ |
| 34 | public class UiWebSocketServlet extends WebSocketServlet { |
| 35 | |
| 36 | private static final long PING_DELAY_MS = 5000; |
| 37 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 38 | private static UiWebSocketServlet instance; |
| 39 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 40 | private ServiceDirectory directory = new DefaultServiceDirectory(); |
| 41 | |
| 42 | private final Set<UiWebSocket> sockets = new HashSet<>(); |
| 43 | private final Timer timer = new Timer(); |
| 44 | private final TimerTask pruner = new Pruner(); |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 45 | private boolean isStopped = false; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 46 | |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Closes all currently open UI web-sockets. |
| 49 | */ |
| 50 | public static void closeAll() { |
| 51 | if (instance != null) { |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 52 | instance.isStopped = true; |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 53 | instance.sockets.forEach(UiWebSocket::close); |
| 54 | instance.sockets.clear(); |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 55 | instance.pruner.cancel(); |
| 56 | instance.timer.cancel(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 60 | @Override |
| 61 | public void init() throws ServletException { |
| 62 | super.init(); |
Thomas Vachuska | 51f540f | 2015-05-27 17:26:57 -0700 | [diff] [blame] | 63 | instance = this; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 64 | timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) { |
Thomas Vachuska | 4e0e093 | 2015-07-24 10:14:34 -0700 | [diff] [blame] | 69 | if (isStopped) { |
| 70 | return null; |
| 71 | } |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 72 | UiWebSocket socket = new UiWebSocket(directory); |
| 73 | synchronized (sockets) { |
| 74 | sockets.add(socket); |
| 75 | } |
| 76 | return socket; |
| 77 | } |
| 78 | |
| 79 | // Task for pruning web-sockets that are idle. |
| 80 | private class Pruner extends TimerTask { |
| 81 | @Override |
| 82 | public void run() { |
| 83 | synchronized (sockets) { |
| 84 | Iterator<UiWebSocket> it = sockets.iterator(); |
| 85 | while (it.hasNext()) { |
| 86 | UiWebSocket socket = it.next(); |
| 87 | if (socket.isIdle()) { |
| 88 | it.remove(); |
| 89 | socket.close(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |