blob: 37120d9167d4930da2cbd2ba5fba9d8c48d17574 [file] [log] [blame]
Thomas Vachuska7d638d32014-11-07 10:24:43 -08001/*
2 * Copyright 2014 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 */
16package org.onlab.onos.gui;
17
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 */
34public class GuiWebSocketServlet extends WebSocketServlet {
35
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080036 private static final long PING_DELAY_MS = 5000;
37
Thomas Vachuska7d638d32014-11-07 10:24:43 -080038 private ServiceDirectory directory = new DefaultServiceDirectory();
39
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080040 private final Set<TopologyViewWebSocket> sockets = new HashSet<>();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080041 private final Timer timer = new Timer();
42 private final TimerTask pruner = new Pruner();
43
44 @Override
45 public void init() throws ServletException {
46 super.init();
47 timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
48 }
49
Thomas Vachuska7d638d32014-11-07 10:24:43 -080050 @Override
51 public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080052 TopologyViewWebSocket socket = new TopologyViewWebSocket(directory);
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080053 synchronized (sockets) {
54 sockets.add(socket);
55 }
56 return socket;
Thomas Vachuska7d638d32014-11-07 10:24:43 -080057 }
58
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080059 // Task for pruning web-sockets that are idle.
60 private class Pruner extends TimerTask {
61 @Override
62 public void run() {
63 synchronized (sockets) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080064 Iterator<TopologyViewWebSocket> it = sockets.iterator();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080065 while (it.hasNext()) {
Thomas Vachuska7c27ad72014-11-14 16:20:10 -080066 TopologyViewWebSocket socket = it.next();
Thomas Vachuskaba5621e2014-11-12 01:47:19 -080067 if (socket.isIdle()) {
68 it.remove();
69 socket.close();
70 }
71 }
72 }
73 }
74 }
Thomas Vachuska7d638d32014-11-07 10:24:43 -080075}