tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1416836 - in /tomcat/trunk: java/org/apache/tomcat/websocket/WsProtocolHandler.java java/org/apache/tomcat/websocket/WsSession.java webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java
Date Tue, 04 Dec 2012 09:33:40 GMT
Author: markt
Date: Tue Dec  4 09:33:39 2012
New Revision: 1416836

URL: http://svn.apache.org/viewvc?rev=1416836&view=rev
Log:
WebSocket 1.0 implementation part 8 of many
Start to join up the upgrade ProtocolHandler, the WebSocket session and the Echo end point
implementation

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/WsProtocolHandler.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java
    tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsProtocolHandler.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsProtocolHandler.java?rev=1416836&r1=1416835&r2=1416836&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsProtocolHandler.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsProtocolHandler.java Tue Dec  4 09:33:39
2012
@@ -16,6 +16,12 @@
  */
 package org.apache.tomcat.websocket;
 
+import java.io.IOException;
+
+import javax.servlet.ReadListener;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.WriteListener;
 import javax.servlet.http.ProtocolHandler;
 import javax.servlet.http.WebConnection;
 import javax.websocket.Endpoint;
@@ -26,16 +32,98 @@ import javax.websocket.Endpoint;
 public class WsProtocolHandler implements ProtocolHandler {
 
     private final Endpoint ep;
+    private final ClassLoader applicationClassLoader;
+    private final WsSession session;
 
     public WsProtocolHandler(Endpoint ep) {
         this.ep = ep;
+        applicationClassLoader = Thread.currentThread().getContextClassLoader();
+        session = new WsSession();
     }
 
     @Override
     public void init(WebConnection connection) {
 
-        ep.onOpen(new WsSession());
+        // Need to call onOpen using the web application's class loader
+        Thread t = Thread.currentThread();
+        ClassLoader cl = t.getContextClassLoader();
+        t.setContextClassLoader(applicationClassLoader);
+        try {
+            ep.onOpen(session);
+        } finally {
+            t.setContextClassLoader(cl);
+        }
+
+        ServletInputStream sis;
+        ServletOutputStream sos;
+        try {
+            sis = connection.getInputStream();
+            sos = connection.getOutputStream();
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+
+        sis.setReadListener(new WsReadListener(this));
+        sos.setWriteListener(new WsWriteListener(this));
+    }
+
+
+    private void onError(Throwable throwable) {
+        // Need to call onError using the web application's class loader
+        Thread t = Thread.currentThread();
+        ClassLoader cl = t.getContextClassLoader();
+        t.setContextClassLoader(applicationClassLoader);
+        try {
+            ep.onError(throwable);
+        } finally {
+            t.setContextClassLoader(cl);
+        }
+    }
+
+    private static class WsReadListener implements ReadListener {
+
+        private final WsProtocolHandler wsProtocolHandler;
+
+        private WsReadListener(WsProtocolHandler wsProtocolHandler) {
+            this.wsProtocolHandler = wsProtocolHandler;
+        }
+
+        @Override
+        public void onDataAvailable() {
+            // TODO Auto-generated method stub
+
+        }
+
+        @Override
+        public void onAllDataRead() {
+            // Will never happen with WebSocket
+            throw new IllegalStateException();
+        }
+
+        @Override
+        public void onError(Throwable throwable) {
+            wsProtocolHandler.onError(throwable);
+        }
+    }
+
+
+    private static class WsWriteListener implements WriteListener {
+
+        private final WsProtocolHandler wsProtocolHandler;
+
+        private WsWriteListener(WsProtocolHandler wsProtocolHandler) {
+            this.wsProtocolHandler = wsProtocolHandler;
+        }
+
+        @Override
+        public void onWritePossible() {
+            // TODO Auto-generated method stub
+
+        }
 
-        // TODO Message handling
+        @Override
+        public void onError(Throwable throwable) {
+            wsProtocolHandler.onError(throwable);
+        }
     }
 }

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=1416836&r1=1416835&r2=1416836&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsSession.java Tue Dec  4 09:33:39 2012
@@ -18,12 +18,14 @@ package org.apache.tomcat.websocket;
 
 import java.io.IOException;
 import java.net.URI;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import javax.websocket.ClientContainer;
 import javax.websocket.CloseReason;
+import javax.websocket.CloseReason.CloseCodes;
 import javax.websocket.Encoder;
 import javax.websocket.MessageHandler;
 import javax.websocket.RemoteEndpoint;
@@ -31,6 +33,8 @@ import javax.websocket.Session;
 
 public class WsSession implements Session {
 
+    private final Set<MessageHandler> messageHandlers = new LinkedHashSet<>();
+
     @Override
     public ClientContainer getContainer() {
         // TODO Auto-generated method stub
@@ -45,20 +49,17 @@ public class WsSession implements Sessio
 
     @Override
     public void addMessageHandler(MessageHandler listener) {
-        // TODO Auto-generated method stub
-
+        messageHandlers.add(listener);
     }
 
     @Override
     public Set<MessageHandler> getMessageHandlers() {
-        // TODO Auto-generated method stub
-        return null;
+        return messageHandlers;
     }
 
     @Override
     public void removeMessageHandler(MessageHandler listener) {
-        // TODO Auto-generated method stub
-
+        messageHandlers.remove(listener);
     }
 
     @Override
@@ -129,8 +130,7 @@ public class WsSession implements Sessio
 
     @Override
     public void close() throws IOException {
-        // TODO Auto-generated method stub
-
+        close(new CloseReason(CloseCodes.GOING_AWAY, ""));
     }
 
     @Override

Modified: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java?rev=1416836&r1=1416835&r2=1416836&view=diff
==============================================================================
--- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java (original)
+++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo/EchoEndpoint.java Tue Dec
 4 09:33:39 2012
@@ -16,9 +16,12 @@
  */
 package websocket.echo;
 
+import java.io.IOException;
+
 import javax.websocket.DefaultServerConfiguration;
 import javax.websocket.Endpoint;
 import javax.websocket.EndpointConfiguration;
+import javax.websocket.RemoteEndpoint;
 import javax.websocket.ServerEndpointConfiguration;
 import javax.websocket.Session;
 
@@ -41,9 +44,27 @@ public class EchoEndpoint extends Endpoi
 
     @Override
     public void onOpen(Session session) {
-        // TODO - Review this debug hack
-        System.out.println("EchoEndpoint onOpen() called");
-        // TODO Auto-generated method stub
+        RemoteEndpoint remoteEndpoint = session.getRemote();
+        session.addMessageHandler(new EchoMessageHandler<>(remoteEndpoint));
+    }
+
+    private static class EchoMessageHandler<T>
+            implements javax.websocket.MessageHandler.Basic<String> {
+
+        private final RemoteEndpoint remoteEndpoint;
 
+        private EchoMessageHandler(RemoteEndpoint remoteEndpoint) {
+            this.remoteEndpoint = remoteEndpoint;
+        }
+
+        @Override
+        public void onMessage(String message) {
+            try {
+                remoteEndpoint.sendString(message);
+            } catch (IOException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
     }
 }



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


Mime
View raw message