blob: 835058f3eab8cf538da31aaa191bfb47468b6aeb [file] [log] [blame]
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska7d638d32014-11-07 10:24:43 -08003 *
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 Vachuskafe8c98a2015-02-04 01:24:32 -080016package org.onosproject.ui.impl;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080017
18import org.eclipse.jetty.websocket.WebSocket;
19import org.eclipse.jetty.websocket.WebSocketServlet;
20import org.onlab.osgi.DefaultServiceDirectory;
21import org.onlab.osgi.ServiceDirectory;
22
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080023import javax.servlet.ServletException;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080024import javax.servlet.http.HttpServletRequest;
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080025import java.util.HashSet;
26import java.util.Iterator;
27import java.util.Set;
28import java.util.Timer;
29import java.util.TimerTask;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080030
31/**
32 * Web socket servlet capable of creating various sockets for the user interface.
33 */
Thomas Vachuska329af532015-03-10 02:08:33 -070034@Deprecated
Thomas Vachuska7d638d32014-11-07 10:24:43 -080035public class GuiWebSocketServlet extends WebSocketServlet {
36
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080037 private static final long PING_DELAY_MS = 5000;
38
Thomas Vachuska7d638d32014-11-07 10:24:43 -080039 private ServiceDirectory directory = new DefaultServiceDirectory();
40
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080041 private final Set<TopologyViewWebSocket> sockets = new HashSet<>();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080042 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 Vachuska7d638d32014-11-07 10:24:43 -080051 @Override
52 public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080053 TopologyViewWebSocket socket = new TopologyViewWebSocket(directory);
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080054 synchronized (sockets) {
55 sockets.add(socket);
56 }
57 return socket;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080058 }
59
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080060 // Task for pruning web-sockets that are idle.
61 private class Pruner extends TimerTask {
62 @Override
63 public void run() {
64 synchronized (sockets) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080065 Iterator<TopologyViewWebSocket> it = sockets.iterator();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080066 while (it.hasNext()) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080067 TopologyViewWebSocket socket = it.next();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080068 if (socket.isIdle()) {
69 it.remove();
70 socket.close();
71 }
72 }
73 }
74 }
75 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -080076}