Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24: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 | */ |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 16 | package org.onosproject.ui.impl; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 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 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 23 | import javax.servlet.ServletException; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 24 | import javax.servlet.http.HttpServletRequest; |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 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; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Web socket servlet capable of creating various sockets for the user interface. |
| 33 | */ |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 34 | @Deprecated |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 35 | public class GuiWebSocketServlet extends WebSocketServlet { |
| 36 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 37 | private static final long PING_DELAY_MS = 5000; |
| 38 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 39 | private ServiceDirectory directory = new DefaultServiceDirectory(); |
| 40 | |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 41 | private final Set<TopologyViewWebSocket> sockets = new HashSet<>(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 42 | private final Timer timer = new Timer(); |
| 43 | private final TimerTask pruner = new Pruner(); |
| 44 | |
| 45 | @Override |
| 46 | public void init() throws ServletException { |
| 47 | super.init(); |
| 48 | timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS); |
| 49 | } |
| 50 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 51 | @Override |
| 52 | public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 53 | TopologyViewWebSocket socket = new TopologyViewWebSocket(directory); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 54 | synchronized (sockets) { |
| 55 | sockets.add(socket); |
| 56 | } |
| 57 | return socket; |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 60 | // Task for pruning web-sockets that are idle. |
| 61 | private class Pruner extends TimerTask { |
| 62 | @Override |
| 63 | public void run() { |
| 64 | synchronized (sockets) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 65 | Iterator<TopologyViewWebSocket> it = sockets.iterator(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 66 | while (it.hasNext()) { |
Thomas Vachuska | 7c27ad7 | 2014-11-14 16:20:10 -0800 | [diff] [blame] | 67 | TopologyViewWebSocket socket = it.next(); |
Thomas Vachuska | ba5621e | 2014-11-12 01:47:19 -0800 | [diff] [blame] | 68 | if (socket.isIdle()) { |
| 69 | it.remove(); |
| 70 | socket.close(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 76 | } |