blob: 0ca3b05fea0f2ecac18289db3ce753e84df3b3a5 [file] [log] [blame]
Thomas Vachuska3553b302015-03-07 14:49:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska3553b302015-03-07 14:49: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 */
16package org.onosproject.ui.impl;
17
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska3553b302015-03-07 14:49:43 -080019import org.eclipse.jetty.websocket.WebSocket;
20import org.eclipse.jetty.websocket.WebSocketServlet;
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
23
24import javax.servlet.ServletException;
25import javax.servlet.http.HttpServletRequest;
Simon Huntea327082016-04-11 16:49:07 -070026import java.security.Principal;
Thomas Vachuska3553b302015-03-07 14:49:43 -080027import java.util.HashSet;
28import java.util.Iterator;
29import java.util.Set;
30import java.util.Timer;
31import java.util.TimerTask;
32
33/**
34 * Web socket servlet capable of creating web sockets for the user interface.
35 */
36public class UiWebSocketServlet extends WebSocketServlet {
37
38 private static final long PING_DELAY_MS = 5000;
39
Thomas Vachuska51f540f2015-05-27 17:26:57 -070040 private static UiWebSocketServlet instance;
41
Thomas Vachuska3553b302015-03-07 14:49:43 -080042 private ServiceDirectory directory = new DefaultServiceDirectory();
43
44 private final Set<UiWebSocket> sockets = new HashSet<>();
45 private final Timer timer = new Timer();
46 private final TimerTask pruner = new Pruner();
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070047 private boolean isStopped = false;
Thomas Vachuska3553b302015-03-07 14:49:43 -080048
Thomas Vachuska51f540f2015-05-27 17:26:57 -070049 /**
50 * Closes all currently open UI web-sockets.
51 */
52 public static void closeAll() {
53 if (instance != null) {
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070054 instance.isStopped = true;
Thomas Vachuska51f540f2015-05-27 17:26:57 -070055 instance.sockets.forEach(UiWebSocket::close);
56 instance.sockets.clear();
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070057 instance.pruner.cancel();
58 instance.timer.cancel();
Thomas Vachuska51f540f2015-05-27 17:26:57 -070059 }
60 }
61
Thomas Vachuska3553b302015-03-07 14:49:43 -080062 @Override
63 public void init() throws ServletException {
64 super.init();
Thomas Vachuska51f540f2015-05-27 17:26:57 -070065 instance = this;
Thomas Vachuska3553b302015-03-07 14:49:43 -080066 timer.schedule(pruner, PING_DELAY_MS, PING_DELAY_MS);
67 }
68
69 @Override
70 public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
Thomas Vachuska4e0e0932015-07-24 10:14:34 -070071 if (isStopped) {
72 return null;
73 }
Thomas Vachuska0af26912016-03-21 21:37:30 -070074
75 // FIXME: Replace this with globally shared opaque token to allow secure failover
Simon Huntea327082016-04-11 16:49:07 -070076 Principal p = request.getUserPrincipal();
77 String userName = p != null ? p.getName() : FAKE_USERNAME;
78
Thomas Vachuska0af26912016-03-21 21:37:30 -070079 UiWebSocket socket = new UiWebSocket(directory, userName);
Thomas Vachuska3553b302015-03-07 14:49:43 -080080 synchronized (sockets) {
81 sockets.add(socket);
82 }
83 return socket;
84 }
85
Simon Huntea327082016-04-11 16:49:07 -070086 // FIXME: This should not be necessary
87 private static final String FAKE_USERNAME = "UI-user";
88
Thomas Vachuskafa74dd72016-03-20 19:11:12 -070089 /**
90 * Sends the specified message to all the GUI clients.
91 *
92 * @param type message type
93 * @param payload message payload
94 */
95 static void sendToAll(String type, ObjectNode payload) {
96 if (instance != null) {
97 instance.sockets.forEach(ws -> ws.sendMessage(type, 0, payload));
98 }
99 }
100
Thomas Vachuska0af26912016-03-21 21:37:30 -0700101 /**
102 * Sends the specified message to all the GUI clients of the specified user.
103 *
104 * @param userName user name
105 * @param type message type
106 * @param payload message payload
107 */
108 static void sendToUser(String userName, String type, ObjectNode payload) {
109 if (instance != null) {
110 instance.sockets.stream().filter(ws -> userName.equals(ws.userName()))
111 .forEach(ws -> ws.sendMessage(type, 0, payload));
112 }
113 }
114
Thomas Vachuska3553b302015-03-07 14:49:43 -0800115 // Task for pruning web-sockets that are idle.
116 private class Pruner extends TimerTask {
117 @Override
118 public void run() {
119 synchronized (sockets) {
120 Iterator<UiWebSocket> it = sockets.iterator();
121 while (it.hasNext()) {
122 UiWebSocket socket = it.next();
123 if (socket.isIdle()) {
124 it.remove();
125 socket.close();
126 }
127 }
128 }
129 }
130 }
131}