2013/2/14 <[email protected]>:
> Author: markt
> Date: Wed Feb 13 20:02:38 2013
> New Revision: 1445893
>
> URL: http://svn.apache.org/r1445893
> Log:
> Implement getOpenSessions
>
> Modified:
> tomcat/trunk/java/javax/websocket/Session.java
> tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
> tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
>
> tomcat/trunk/java/org/apache/tomcat/websocket/server/ServerContainerImpl.java
>
> tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
> tomcat/trunk/test/org/apache/tomcat/websocket/TestWsSession.java
>
> tomcat/trunk/test/org/apache/tomcat/websocket/TestWsWebSocketContainer.java
>
> --- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
> +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Wed Feb 13
> 20:02:38 2013
(...)
> @@ -240,8 +240,7 @@ public class WsSession implements Sessio
>
> @Override
> public Set<Session> getOpenSessions() {
> - // TODO Auto-generated method stub
> - return null;
> + return webSocketContainer.getOpenSession(localEndpoint.getClass());
Inconsistent method names. s/getOpenSession/getOpenSessions/
> }
>
>
(...)
> --- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
> (original)
> +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
> Wed Feb 13 20:02:38 2013
> @@ -25,6 +25,7 @@ import java.nio.channels.AsynchronousSoc
> import java.nio.charset.Charset;
> import java.util.ArrayList;
> import java.util.HashMap;
> +import java.util.HashSet;
> import java.util.Iterator;
> import java.util.List;
> import java.util.Map;
> @@ -53,6 +54,10 @@ public class WsWebSocketContainer implem
> private static final Charset iso88591 = Charset.forName("ISO-8859-1");
> private static final byte[] crlf = new byte[] {13, 10};
>
> + private final Map<Class<?>, Set<WsSession>> endpointSessionMap =
> + new HashMap<>();
> + private final Object endPointSessionMapLock = new Object();
> +
> private long defaultAsyncTimeout = -1;
> private int maxBinaryMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
> private int maxTextMessageBufferSize = Constants.DEFAULT_BUFFER_SIZE;
> @@ -147,8 +152,8 @@ public class WsWebSocketContainer implem
> }
> WsSession wsSession =
> new WsSession(endpoint, wsRemoteEndpointClient, this);
> -
> endpoint.onOpen(wsSession, clientEndpointConfiguration);
> + registerSession(clazz, wsSession);
>
> // Object creation will trigger input processing
> @SuppressWarnings("unused")
> @@ -159,6 +164,37 @@ public class WsWebSocketContainer implem
> }
>
>
> + protected void registerSession(Class<?> endpoint, WsSession wsSession) {
> + synchronized (endPointSessionMapLock) {
> + Set<WsSession> wsSessions = endpointSessionMap.get(endpoint);
> + if (wsSessions == null) {
> + wsSessions = new HashSet<>();
> + endpointSessionMap.put(endpoint, wsSessions);
> + }
> + wsSessions.add(wsSession);
> + }
> + }
> +
> +
> + protected void unregisterSession(Class<?> endpoint, WsSession wsSession)
> {
> + synchronized (endPointSessionMapLock) {
> + Set<WsSession> wsSessions = endpointSessionMap.get(endpoint);
> + if (wsSessions != null) {
> + wsSessions.remove(wsSession);
> + if (wsSessions.size() == 0) {
> + endpointSessionMap.remove(endpoint);
> + }
> + }
> + }
> + }
> +
> +
> + Set<Session> getOpenSession(Class<?> endpoint) {
> + HashSet<Session> result = new HashSet<>();
> + result.addAll(endpointSessionMap.get(endpoint));
> + return result;
endpointSessionMap is a mere HashMap.
I think a lock is needed here, or a ConcurrentHashMap.
(It can be a ReadWriteLock as this is not a WeakHashMap, but I doubt
that will improve much).
> + }
> +
> private Map<String,List<String>> createRequestHeaders(String host,
> int port) {
>
Best regards,
Konstantin Kolinko
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]