blob: 6796c6b87fe366aedc7279188e4c5cd9b97c0d46 [file] [log] [blame]
Thomas Vachuska750ab042015-06-17 10:42:15 -07001/*
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 */
16package org.onlab.stc;
17
18import com.google.common.io.ByteStreams;
19import com.google.common.net.MediaType;
20import org.eclipse.jetty.websocket.WebSocket;
21import org.eclipse.jetty.websocket.WebSocketServlet;
22
23import javax.servlet.ServletException;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
26import java.io.IOException;
27import java.io.InputStream;
28import java.util.HashSet;
29import java.util.Iterator;
30import java.util.Set;
31import java.util.Timer;
32import java.util.TimerTask;
33
34/**
35 * Web socket servlet capable of creating web sockets for the STC monitor.
36 */
37public class MonitorWebSocketServlet extends WebSocketServlet {
38
39 private static final long PING_DELAY_MS = 5000;
40 private static final String DOT = ".";
41
42 private static MonitorWebSocketServlet instance;
43
44 private final Set<MonitorWebSocket> sockets = new HashSet<>();
45 private final Timer timer = new Timer();
46 private final TimerTask pruner = new Pruner();
47
48 /**
49 * Closes all currently open monitor web-sockets.
50 */
51 public static void closeAll() {
52 if (instance != null) {
53 instance.sockets.forEach(MonitorWebSocket::close);
54 instance.sockets.clear();
55 }
56 }
57
58 @Override
59 public void init() throws ServletException {
60 super.init();
61 instance = this;
62 System.out.println("Yo!!!!");
63 timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
64 }
65
66 @Override
67 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
68 throws ServletException, IOException {
69 String uri = req.getRequestURI();
70 uri = uri.length() <= 1 ? "/index.html" : uri;
71 InputStream resource = getClass().getResourceAsStream(uri);
72 if (resource == null) {
73 resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
74 } else {
75 byte[] entity = ByteStreams.toByteArray(resource);
76 resp.setStatus(HttpServletResponse.SC_OK);
77 resp.setContentType(contentType(uri).toString());
78 resp.setContentLength(entity.length);
79 resp.getOutputStream().write(entity);
80 }
81 }
82
83 private MediaType contentType(String uri) {
84 int sep = uri.lastIndexOf(DOT);
85 String ext = sep > 0 ? uri.substring(sep + 1) : null;
86 return ext == null ? MediaType.APPLICATION_BINARY :
87 ext.equals("html") ? MediaType.HTML_UTF_8 :
88 ext.equals("js") ? MediaType.JAVASCRIPT_UTF_8 :
89 ext.equals("css") ? MediaType.CSS_UTF_8 :
90 MediaType.APPLICATION_BINARY;
91 }
92
93 @Override
94 public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
95 System.out.println("Wazup????");
96 MonitorWebSocket socket = new MonitorWebSocket();
97 synchronized (sockets) {
98 sockets.add(socket);
99 }
100 return socket;
101 }
102
103 // Task for pruning web-sockets that are idle.
104 private class Pruner extends TimerTask {
105 @Override
106 public void run() {
107 synchronized (sockets) {
108 Iterator<MonitorWebSocket> it = sockets.iterator();
109 while (it.hasNext()) {
110 MonitorWebSocket socket = it.next();
111 if (socket.isIdle()) {
112 it.remove();
113 socket.close();
114 }
115 }
116 }
117 }
118 }
119}