You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/01/24 12:01:52 UTC

svn commit: r1437947 - in /tomcat/trunk/java/org/apache/tomcat/websocket: WsFrameBase.java WsSession.java WsWebSocketContainer.java server/WsProtocolHandler.java

Author: markt
Date: Thu Jan 24 11:01:52 2013
New Revision: 1437947

URL: http://svn.apache.org/viewvc?rev=1437947&view=rev
Log:
Refactor session creation and endpoint event firing

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.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/WsProtocolHandler.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java?rev=1437947&r1=1437946&r2=1437947&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java Thu Jan 24 11:01:52 2013
@@ -275,8 +275,7 @@ public abstract class WsFrameBase {
                     reason = controlBufferText.toString();
                 }
             }
-            wsSession.onClose(
-                    new CloseReason(Util.getCloseCode(code), reason));
+            wsSession.close(new CloseReason(Util.getCloseCode(code), reason));
         } else if (opCode == Constants.OPCODE_PING) {
             if (wsSession.isOpen()) {
                 wsSession.getRemote().sendPong(controlBufferBinary);
@@ -498,7 +497,6 @@ public abstract class WsFrameBase {
                                 Integer.valueOf(inputBuffer.length),
                                 Long.valueOf(payloadLength)));
                 wsSession.close(cr);
-                wsSession.onClose(cr);
                 throw new IOException(cr.getReasonPhrase());
             }
             makeRoom();

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java?rev=1437947&r1=1437946&r2=1437947&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Thu Jan 24 11:01:52 2013
@@ -43,14 +43,29 @@ public class WsSession implements Sessio
     private static final Charset UTF8 = Charset.forName("UTF8");
 
     private final Endpoint localEndpoint;
-    private WsRemoteEndpointBase wsRemoteEndpoint;
+    private final WsRemoteEndpointBase wsRemoteEndpoint;
+    private final ClassLoader applicationClassLoader;
+
     private MessageHandler textMessageHandler = null;
     private MessageHandler binaryMessageHandler = null;
     private MessageHandler.Basic<PongMessage> pongMessageHandler = null;
     private volatile boolean open = true;
 
-    public WsSession(Endpoint localEndpoint) {
+
+    /**
+     * Creates a new WebSocket session for communication between the two
+     * provided end points. The result of {@link Thread#getContextClassLoader()}
+     * at the time this constructor is called will be used when calling
+     * {@link Endpoint#onClose(Session, CloseReason)}.
+     *
+     * @param localEndpoint
+     * @param wsRemoteEndpoint
+     */
+    public WsSession(Endpoint localEndpoint,
+            WsRemoteEndpointBase wsRemoteEndpoint) {
         this.localEndpoint = localEndpoint;
+        this.wsRemoteEndpoint = wsRemoteEndpoint;
+        applicationClassLoader = Thread.currentThread().getContextClassLoader();
     }
 
 
@@ -201,17 +216,29 @@ public class WsSession implements Sessio
 
 
     @Override
-    public void close(CloseReason closeStatus) throws IOException {
+    public void close(CloseReason closeReason) throws IOException {
         open = false;
+
+        // Send the close message
         // 125 is maximum size for the payload of a control message
         ByteBuffer msg = ByteBuffer.allocate(125);
-        msg.putShort((short) closeStatus.getCloseCode().getCode());
-        String reason = closeStatus.getReasonPhrase();
+        msg.putShort((short) closeReason.getCloseCode().getCode());
+        String reason = closeReason.getReasonPhrase();
         if (reason != null && reason.length() > 0) {
             msg.put(reason.getBytes(UTF8));
         }
         msg.flip();
         wsRemoteEndpoint.sendMessageBlocking(Constants.OPCODE_CLOSE, msg, true);
+
+        // Fire the onClose event
+        Thread t = Thread.currentThread();
+        ClassLoader cl = t.getContextClassLoader();
+        t.setContextClassLoader(applicationClassLoader);
+        try {
+            localEndpoint.onClose(this, closeReason);
+        } finally {
+            t.setContextClassLoader(cl);
+        }
     }
 
 
@@ -250,11 +277,6 @@ public class WsSession implements Sessio
     }
 
 
-    public void setRemote(WsRemoteEndpointBase wsRemoteEndpoint) {
-        this.wsRemoteEndpoint = wsRemoteEndpoint;
-    }
-
-
     protected MessageHandler getTextMessageHandler() {
         return textMessageHandler;
     }
@@ -269,15 +291,6 @@ public class WsSession implements Sessio
         return pongMessageHandler;
     }
 
-    public void onClose(CloseReason closeReason) {
-        localEndpoint.onClose(this, closeReason);
-    }
-
-
-    public Endpoint getLocalEndpoint() {
-        return localEndpoint;
-    }
-
 
     // Protected so unit tests can use it
     protected static Class<?> getMessageType(MessageHandler listener) {

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java?rev=1437947&r1=1437946&r2=1437947&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java Thu Jan 24 11:01:52 2013
@@ -63,7 +63,7 @@ public class WsWebSocketContainer implem
 
 
     @Override
-    public Session connectToServer(Class<? extends Endpoint> endpoint,
+    public Session connectToServer(Class<? extends Endpoint> clazz,
             ClientEndpointConfiguration clientEndpointConfiguration, URI path)
             throws DeploymentException {
 
@@ -134,14 +134,16 @@ public class WsWebSocketContainer implem
         WsRemoteEndpointClient wsRemoteEndpointClient =
                 new WsRemoteEndpointClient(channel);
 
-        WsSession wsSession;
+        Endpoint endpoint;
         try {
-            wsSession = new WsSession(endpoint.newInstance());
+            endpoint = clazz.newInstance();
         } catch (InstantiationException | IllegalAccessException e) {
-            // TODO
-            throw new DeploymentException("TBD", e);
+            // TODO i18n
+            throw new DeploymentException("TDB", e);
         }
-        wsSession.setRemote(wsRemoteEndpointClient);
+        WsSession wsSession = new WsSession(endpoint, wsRemoteEndpointClient);
+
+        endpoint.onOpen(wsSession, clientEndpointConfiguration);
 
         try {
             // Object creation will trigger input processing

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java?rev=1437947&r1=1437946&r2=1437947&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/server/WsProtocolHandler.java Thu Jan 24 11:01:52 2013
@@ -49,14 +49,15 @@ public class WsProtocolHandler implement
     private final Endpoint ep;
     private final EndpointConfiguration endpointConfig;
     private final ClassLoader applicationClassLoader;
-    private final WsSession wsSession;
 
+    private WsSession wsSession;
 
-    public WsProtocolHandler(Endpoint ep, EndpointConfiguration endpointConfig) {
+
+    public WsProtocolHandler(Endpoint ep,
+            EndpointConfiguration endpointConfig) {
         this.ep = ep;
         this.endpointConfig = endpointConfig;
         applicationClassLoader = Thread.currentThread().getContextClassLoader();
-        wsSession = new WsSession(ep);
     }
 
 
@@ -82,7 +83,7 @@ public class WsProtocolHandler implement
             sis.setReadListener(new WsReadListener(this, wsFrame));
             WsRemoteEndpointServer wsRemoteEndpointServer =
                     new WsRemoteEndpointServer(sos);
-            wsSession.setRemote(wsRemoteEndpointServer);
+            wsSession = new WsSession(ep, wsRemoteEndpointServer);
             sos.setWriteListener(
                     new WsWriteListener(this, wsRemoteEndpointServer));
             ep.onOpen(wsSession, endpointConfig);
@@ -106,17 +107,6 @@ public class WsProtocolHandler implement
 
 
     private void close(CloseReason cr) {
-        // Need to call onClose using the web application's class loader
-        Thread t = Thread.currentThread();
-        ClassLoader cl = t.getContextClassLoader();
-        t.setContextClassLoader(applicationClassLoader);
-        try {
-            ep.onClose(wsSession, cr);
-        } finally {
-            t.setContextClassLoader(cl);
-        }
-        // Explicitly close the session if it wasn't closed during the
-        // onClose() event
         if (wsSession.isOpen()) {
             try {
                 wsSession.close(cr);



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org