From 1480f3d8fc60a310c957a0923fae850bc5821ed2 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 31 Jan 2013 12:09:04 -0700 Subject: [PATCH] 399669 - Remove WebSocketConnection in favor of websocket.api.Session --- .../websocket/api/WebSocketConnection.java | 133 ------------------ .../api/io/WebSocketOutputStream.java | 8 +- .../websocket/api/io/WebSocketWriter.java | 8 +- .../client/examples/SimpleEchoClient.java | 18 +-- .../websocket/common/WebSocketSession.java | 50 ++----- .../common/extensions/mux/MuxChannel.java | 62 +------- .../io/AbstractWebSocketConnection.java | 1 - .../examples/echo/AnnotatedEchoSocket.java | 8 +- .../annotations/BadBinarySignatureSocket.java | 4 +- .../annotations/BadTextSignatureSocket.java | 4 +- .../common/annotations/NotASocket.java | 4 +- .../common/io/LocalWebSocketConnection.java | 42 +----- .../jetty/websocket/server/LoadTest.java | 10 +- .../server/WebSocketOverSSLTest.java | 12 +- .../server/browser/BrowserSocket.java | 16 +-- .../server/examples/echo/BigEchoSocket.java | 16 ++- .../echo/EchoBroadcastPingSocket.java | 16 +-- .../examples/echo/EchoBroadcastSocket.java | 14 +- .../examples/echo/EchoFragmentSocket.java | 14 +- .../java/com/acme/WebSocketChatServlet.java | 7 +- 20 files changed, 94 insertions(+), 353 deletions(-) delete mode 100644 jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/WebSocketConnection.java diff --git a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/WebSocketConnection.java b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/WebSocketConnection.java deleted file mode 100644 index 472fa0112c7..00000000000 --- a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/WebSocketConnection.java +++ /dev/null @@ -1,133 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.websocket.api; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.URI; -import java.nio.ByteBuffer; -import java.util.concurrent.Future; - -/** - * Connection interface for WebSocket protocol RFC-6455. - * - * @deprecated replaced with more JSR friendly {@link Session} interface - */ -@Deprecated -public interface WebSocketConnection -{ - /** - * Send a websocket Close frame, without a status code or reason. - *

- * Basic usage: results in an non-blocking async write, then connection close. - * - * @see StatusCode - * @see #close(int, String) - */ - public void close() throws IOException; - - /** - * Send a websocket Close frame, with status code. - *

- * Advanced usage: results in an non-blocking async write, then connection close. - * - * @param statusCode - * the status code - * @param reason - * the (optional) reason. (can be null for no reason) - * @see StatusCode - */ - public void close(int statusCode, String reason) throws IOException; - - /** - * Get the address of the local side. - * - * @return the local side address - */ - public InetSocketAddress getLocalAddress(); - - /** - * Access the (now read-only) {@link WebSocketPolicy} in use for this connection. - * - * @return the policy in use - */ - WebSocketPolicy getPolicy(); - - /** - * Get the address of the remote side. - * - * @return the remote side address - */ - public InetSocketAddress getRemoteAddress(); - - /** - * Get the Request URI - * - * @return the requested URI - */ - public URI getRequestURI(); - - /** - * Get the SubProtocol in use for this connection. - * - * @return the negotiated sub protocol name in use for this connection, can be null if there is no sub-protocol negotiated. - */ - String getSubProtocol(); - - /** - * Is the connection open. - * - * @return true if open - */ - public boolean isOpen(); - - /** - * Send a single ping messages. - *

- * NIO style with callbacks, allows for knowledge of successful ping send. - *

- * Use @OnWebSocketFrame and monitor Pong frames - * - * @param the - * ping application data - */ - void ping(ByteBuffer buf) throws IOException; - - /** - * Suspend a the incoming read events on the connection. - * - * @return the suspend token suitable for resuming the reading of data on the connection. - */ - SuspendToken suspend(); - - /** - * Send an async binary message. - */ - Future write(byte buf[], int offset, int len); - - /** - * Send an async binary message. - */ - Future write(ByteBuffer buffer); - - /** - * Send an async text messages. - */ - Future write(String message); -} \ No newline at end of file diff --git a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketOutputStream.java b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketOutputStream.java index 54b9a6e76d1..42699bdb61d 100644 --- a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketOutputStream.java +++ b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketOutputStream.java @@ -21,15 +21,15 @@ package org.eclipse.jetty.websocket.api.io; import java.io.IOException; import java.io.OutputStream; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; public class WebSocketOutputStream extends OutputStream { - private final WebSocketConnection conn; + private final Session session; - public WebSocketOutputStream(WebSocketConnection conn) + public WebSocketOutputStream(Session session) { - this.conn = conn; + this.session = session; } @Override diff --git a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java index dc9e8a1acf9..abd194d24f8 100644 --- a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java +++ b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java @@ -22,16 +22,16 @@ import java.io.IOException; import java.io.Writer; import java.nio.charset.Charset; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; public class WebSocketWriter extends Writer { private final Charset charset = Charset.forName("UTF-8"); - private final WebSocketConnection conn; + private final Session session; - public WebSocketWriter(WebSocketConnection conn) + public WebSocketWriter(Session session) { - this.conn = conn; + this.session = session; } @Override diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/SimpleEchoClient.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/SimpleEchoClient.java index 65ea95c6944..91b77b257e3 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/SimpleEchoClient.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/SimpleEchoClient.java @@ -23,8 +23,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.StatusCode; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; @@ -42,7 +42,7 @@ public class SimpleEchoClient { private final CountDownLatch closeLatch; @SuppressWarnings("unused") - private WebSocketConnection conn; + private Session session; public SimpleEchoSocket() { @@ -58,25 +58,25 @@ public class SimpleEchoClient public void onClose(int statusCode, String reason) { System.out.printf("Connection closed: %d - %s%n",statusCode,reason); - this.conn = null; + this.session = null; this.closeLatch.countDown(); // trigger latch } @OnWebSocketConnect - public void onConnect(WebSocketConnection conn) + public void onConnect(Session session) { - System.out.printf("Got connect: %s%n",conn); - this.conn = conn; + System.out.printf("Got connect: %s%n",session); + this.session = session; try { Future fut; - fut = conn.write("Hello"); + fut = session.getRemote().sendStringByFuture("Hello"); fut.get(2,TimeUnit.SECONDS); // wait for send to complete. - fut = conn.write("Thanks for the conversation."); + fut = session.getRemote().sendStringByFuture("Thanks for the conversation."); fut.get(2,TimeUnit.SECONDS); // wait for send to complete. - conn.close(StatusCode.NORMAL,"I'm done"); + session.close(StatusCode.NORMAL,"I'm done"); } catch (Throwable t) { diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java index ae48935f443..07fb134239b 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java @@ -21,11 +21,9 @@ package org.eclipse.jetty.websocket.common; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URI; -import java.nio.ByteBuffer; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.Future; import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.StringUtil; @@ -43,7 +41,6 @@ import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.SuspendToken; import org.eclipse.jetty.websocket.api.UpgradeRequest; import org.eclipse.jetty.websocket.api.UpgradeResponse; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory; @@ -53,7 +50,7 @@ import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames; import org.eclipse.jetty.websocket.common.events.EventDriver; @ManagedObject -public class WebSocketSession extends ContainerLifeCycle implements Session, WebSocketConnection, IncomingFrames +public class WebSocketSession extends ContainerLifeCycle implements Session, IncomingFrames { private static final Logger LOG = Log.getLogger(WebSocketSession.class); private final URI requestURI; @@ -228,18 +225,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web return remote.getInetSocketAddress(); } - @Override - public URI getRequestURI() - { - return requestURI; - } - - @Override - public String getSubProtocol() - { - return upgradeResponse.getAcceptedSubProtocol(); - } - @Override public UpgradeRequest getUpgradeRequest() { @@ -290,7 +275,14 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web @Override public boolean isSecure() { - return getRequestURI().getScheme().equalsIgnoreCase("wss"); + if (upgradeRequest == null) + { + throw new IllegalStateException("No valid UpgradeRequest yet"); + } + + URI requestURI = upgradeRequest.getRequestURI(); + + return "wss".equalsIgnoreCase(requestURI.getScheme()); } /** @@ -320,12 +312,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web } } - @Override - public void ping(ByteBuffer buf) throws IOException - { - remote.sendPing(buf); - } - public void setActive(boolean active) { this.active = active; @@ -391,22 +377,4 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Web builder.append("]"); return builder.toString(); } - - @Override - public Future write(byte[] buf, int offset, int len) - { - return remote.sendBytesByFuture(ByteBuffer.wrap(buf,offset,len)); - } - - @Override - public Future write(ByteBuffer buffer) - { - return remote.sendBytesByFuture(buffer); - } - - @Override - public Future write(String message) - { - return remote.sendStringByFuture(message); - } } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxChannel.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxChannel.java index 336ab0b5486..3e56bbd56fc 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxChannel.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/extensions/mux/MuxChannel.java @@ -18,19 +18,14 @@ package org.eclipse.jetty.websocket.common.extensions.mux; -import java.io.IOException; import java.net.InetSocketAddress; -import java.net.URI; -import java.nio.ByteBuffer; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; -import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.SuspendToken; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WriteCallback; @@ -47,7 +42,7 @@ import org.eclipse.jetty.websocket.common.io.IOState; /** * MuxChannel, acts as WebSocketConnection for specific sub-channel. */ -public class MuxChannel implements WebSocketConnection, LogicalConnection, IncomingFrames, SuspendToken +public class MuxChannel implements LogicalConnection, IncomingFrames, SuspendToken { private static final Logger LOG = Log.getLogger(MuxChannel.class); @@ -128,25 +123,12 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom return muxer.getRemoteAddress(); } - @Override - public URI getRequestURI() - { - // TODO Auto-generated method stub - return null; - } - @Override public WebSocketSession getSession() { return session; } - @Override - public String getSubProtocol() - { - return this.subProtocol; - } - /** * Incoming exceptions from Muxer. */ @@ -214,15 +196,6 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom muxer.output(channelId,frame,callback); } - /** - * Ping frame destined for the Muxer - */ - @Override - public void ping(ByteBuffer buf) throws IOException - { - outgoingFrame(WebSocketFrame.ping().setPayload(buf),null); - } - @Override public void resume() { @@ -257,37 +230,4 @@ public class MuxChannel implements WebSocketConnection, LogicalConnection, Incom // TODO: how to suspend reading? return this; } - - /** - * Generate a binary message, destined for Muxer - */ - @Override - public Future write(byte[] buf, int offset, int len) - { - ByteBuffer bb = ByteBuffer.wrap(buf,offset,len); - return write(bb); - } - - /** - * Generate a binary message, destined for Muxer - */ - @Override - public Future write(ByteBuffer buffer) - { - if (LOG.isDebugEnabled()) - { - LOG.debug("write with {}",BufferUtil.toDetailString(buffer)); - } - WebSocketFrame frame = WebSocketFrame.binary().setPayload(buffer); - return outgoingAsyncFrame(frame); - } - - /** - * Generate a text message, destined for Muxer - */ - @Override - public Future write(String message) - { - return outgoingAsyncFrame(WebSocketFrame.text(message)); - } } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java index 42012246cbf..78a45e1ff40 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/io/AbstractWebSocketConnection.java @@ -40,7 +40,6 @@ import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.websocket.api.CloseException; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.SuspendToken; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WriteCallback; diff --git a/jetty-websocket/websocket-common/src/test/java/examples/echo/AnnotatedEchoSocket.java b/jetty-websocket/websocket-common/src/test/java/examples/echo/AnnotatedEchoSocket.java index c93444efee0..a2af9203e26 100644 --- a/jetty-websocket/websocket-common/src/test/java/examples/echo/AnnotatedEchoSocket.java +++ b/jetty-websocket/websocket-common/src/test/java/examples/echo/AnnotatedEchoSocket.java @@ -18,7 +18,7 @@ package examples.echo; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @@ -29,12 +29,12 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket; public class AnnotatedEchoSocket { @OnWebSocketMessage - public void onText(WebSocketConnection conn, String message) + public void onText(Session session, String message) { - if (conn.isOpen()) + if (session.isOpen()) { return; } - conn.write(message); + session.getRemote().sendStringByFuture(message); } } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadBinarySignatureSocket.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadBinarySignatureSocket.java index c8a3c8f6d13..114b4ce0e84 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadBinarySignatureSocket.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadBinarySignatureSocket.java @@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket.common.annotations; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @@ -32,7 +32,7 @@ public class BadBinarySignatureSocket * Declaring a non-void return type */ @OnWebSocketMessage - public boolean onBinary(WebSocketConnection conn, byte buf[], int offset, int len) + public boolean onBinary(Session session, byte buf[], int offset, int len) { return false; } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadTextSignatureSocket.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadTextSignatureSocket.java index b7d9bc58532..c543d152ae9 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadTextSignatureSocket.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/BadTextSignatureSocket.java @@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket.common.annotations; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @@ -32,7 +32,7 @@ public class BadTextSignatureSocket * Declaring a static method */ @OnWebSocketMessage - public static void onText(WebSocketConnection conn, String text) + public static void onText(Session session, String text) { /* do nothing */ } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/NotASocket.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/NotASocket.java index 923a95946a5..66981e9236e 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/NotASocket.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/annotations/NotASocket.java @@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket.common.annotations; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; /** @@ -29,7 +29,7 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; public class NotASocket { @OnWebSocketConnect - public void onConnect(WebSocketConnection conn) + public void onConnect(Session session) { /* do nothing */ } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java index f3d31e1c16d..f73ff8edd00 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/LocalWebSocketConnection.java @@ -18,14 +18,9 @@ package org.eclipse.jetty.websocket.common.io; -import java.io.IOException; import java.net.InetSocketAddress; -import java.net.URI; -import java.nio.ByteBuffer; -import java.util.concurrent.Future; import org.eclipse.jetty.websocket.api.SuspendToken; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WriteCallback; @@ -35,7 +30,7 @@ import org.eclipse.jetty.websocket.common.LogicalConnection; import org.eclipse.jetty.websocket.common.WebSocketSession; import org.junit.rules.TestName; -public class LocalWebSocketConnection implements WebSocketConnection, LogicalConnection, IncomingFrames +public class LocalWebSocketConnection implements LogicalConnection, IncomingFrames { private final String id; private WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); @@ -105,24 +100,12 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon return null; } - @Override - public URI getRequestURI() - { - return null; - } - @Override public WebSocketSession getSession() { return null; } - @Override - public String getSubProtocol() - { - return null; - } - @Override public void incomingError(WebSocketException e) { @@ -156,11 +139,6 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon { } - @Override - public void ping(ByteBuffer buf) throws IOException - { - } - @Override public void resume() { @@ -193,22 +171,4 @@ public class LocalWebSocketConnection implements WebSocketConnection, LogicalCon { return String.format("%s[%s]",LocalWebSocketConnection.class.getSimpleName(),id); } - - @Override - public Future write(byte[] buf, int offset, int len) - { - return null; - } - - @Override - public Future write(ByteBuffer buffer) - { - return null; - } - - @Override - public Future write(String message) - { - return null; - } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/LoadTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/LoadTest.java index bfea4855c65..cd688c78544 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/LoadTest.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/LoadTest.java @@ -26,8 +26,8 @@ import java.util.concurrent.atomic.AtomicLong; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.StatusCode; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @@ -56,19 +56,19 @@ public class LoadTest @WebSocket public static class LoadSocket { - private WebSocketConnection conn; + private Session session; public static AtomicLong count = new AtomicLong(0); @OnWebSocketConnect - public void onConnect(WebSocketConnection conn) + public void onConnect(Session session) { - this.conn = conn; + this.session = session; } @OnWebSocketMessage public void onWebSocketText(String message) { - conn.write(message); + session.getRemote().sendStringByFuture(message); long iter = count.incrementAndGet(); if ((iter % 100) == 0) { diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java index 920793a9e46..56218c50d91 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/WebSocketOverSSLTest.java @@ -29,7 +29,6 @@ import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; -import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.server.examples.MyEchoSocket; import org.junit.After; import org.junit.Assert; @@ -42,15 +41,14 @@ public class WebSocketOverSSLTest private int _port; private QueuedThreadPool _threadPool; - // private WebSocketClientFactory _wsFactory; - private WebSocketConnection _connection; + private Session _session; @After public void destroy() throws Exception { - if (_connection != null) + if (_session != null) { - _connection.close(); + _session.close(); } // if (_wsFactory != null) @@ -122,7 +120,7 @@ public class WebSocketOverSSLTest String message = new String(chars); for (int i = 0; i < count; ++i) { - _connection.write(message); + _session.getRemote().sendStringByFuture(message); } Assert.assertTrue(clientLatch.await(20,TimeUnit.SECONDS)); @@ -166,7 +164,7 @@ public class WebSocketOverSSLTest clientLatch.countDown(); } }); - _connection.write(message); + _session.getRemote().sendStringByFuture(message); Assert.assertTrue(serverLatch.await(5,TimeUnit.SECONDS)); Assert.assertTrue(clientLatch.await(5,TimeUnit.SECONDS)); diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/browser/BrowserSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/browser/BrowserSocket.java index 3356c9be98a..20a644ccebf 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/browser/BrowserSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/browser/BrowserSocket.java @@ -26,7 +26,7 @@ import java.util.Locale; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; @@ -36,7 +36,7 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket; public class BrowserSocket { private static final Logger LOG = Log.getLogger(BrowserSocket.class); - private WebSocketConnection connection; + private Session session; private final String userAgent; private final String requestedExtensions; @@ -47,15 +47,15 @@ public class BrowserSocket } @OnWebSocketConnect - public void onConnect(WebSocketConnection conn) + public void onConnect(Session session) { - this.connection = conn; + this.session = session; } @OnWebSocketClose public void onDisconnect(int statusCode, String reason) { - this.connection = null; + this.session = null; LOG.info("Closed [{}, {}]",statusCode,reason); } @@ -114,19 +114,19 @@ public class BrowserSocket private void writeMessage(String message) { - if (this.connection == null) + if (this.session == null) { LOG.debug("Not connected"); return; } - if (connection.isOpen() == false) + if (session.isOpen() == false) { LOG.debug("Not open"); return; } - connection.write(message); + session.getRemote().sendStringByFuture(message); } private void writeMessage(String format, Object... args) diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/BigEchoSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/BigEchoSocket.java index 6a6f6419033..7e009543257 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/BigEchoSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/BigEchoSocket.java @@ -18,7 +18,9 @@ package org.eclipse.jetty.websocket.server.examples.echo; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @@ -29,22 +31,22 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket; public class BigEchoSocket { @OnWebSocketMessage - public void onBinary(WebSocketConnection conn, byte buf[], int offset, int length) + public void onBinary(Session session, byte buf[], int offset, int length) { - if (conn.isOpen()) + if (session.isOpen()) { return; } - conn.write(buf,offset,length); + session.getRemote().sendBytesByFuture(ByteBuffer.wrap(buf,offset,length)); } @OnWebSocketMessage - public void onText(WebSocketConnection conn, String message) + public void onText(Session session, String message) { - if (conn.isOpen()) + if (session.isOpen()) { return; } - conn.write(message); + session.getRemote().sendStringByFuture(message); } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java index 031f90ab11a..5205e0557e5 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastPingSocket.java @@ -22,7 +22,7 @@ import java.nio.ByteBuffer; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @WebSocket @@ -31,11 +31,11 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket private static class KeepAlive extends Thread { private CountDownLatch latch; - private WebSocketConnection conn; + private Session session; - public KeepAlive(WebSocketConnection conn) + public KeepAlive(Session session) { - this.conn = conn; + this.session = session; } @Override @@ -50,7 +50,7 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket data.put(new byte[] { (byte)1, (byte)2, (byte)3 }); data.flip(); - conn.ping(data); + session.getRemote().sendPing(data); } } catch (Exception e) @@ -89,13 +89,13 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket } @Override - public void onOpen(WebSocketConnection conn) + public void onOpen(Session session) { if (keepAlive == null) { - keepAlive = new KeepAlive(conn); + keepAlive = new KeepAlive(session); } keepAlive.start(); - super.onOpen(conn); + super.onOpen(session); } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastSocket.java index 9c87292e51b..6cbe5d4c89f 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoBroadcastSocket.java @@ -18,9 +18,10 @@ package org.eclipse.jetty.websocket.server.examples.echo; +import java.nio.ByteBuffer; import java.util.concurrent.ConcurrentLinkedQueue; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; @@ -31,14 +32,15 @@ public class EchoBroadcastSocket { private static final ConcurrentLinkedQueue BROADCAST = new ConcurrentLinkedQueue(); - protected WebSocketConnection conn; + protected Session session; @OnWebSocketMessage public void onBinary(byte buf[], int offset, int len) { + ByteBuffer data = ByteBuffer.wrap(buf,offset,len); for (EchoBroadcastSocket sock : BROADCAST) { - sock.conn.write(buf,offset,len); + sock.session.getRemote().sendBytesByFuture(data.slice()); } } @@ -49,9 +51,9 @@ public class EchoBroadcastSocket } @OnWebSocketConnect - public void onOpen(WebSocketConnection conn) + public void onOpen(Session session) { - this.conn = conn; + this.session = session; BROADCAST.add(this); } @@ -60,7 +62,7 @@ public class EchoBroadcastSocket { for (EchoBroadcastSocket sock : BROADCAST) { - sock.conn.write(text); + sock.session.getRemote().sendStringByFuture(text); } } } diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoFragmentSocket.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoFragmentSocket.java index 8991cd9645a..1a5bdae2eec 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoFragmentSocket.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/examples/echo/EchoFragmentSocket.java @@ -22,7 +22,8 @@ import java.io.IOException; import java.nio.ByteBuffer; import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.api.RemoteEndpoint; +import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame; import org.eclipse.jetty.websocket.api.annotations.WebSocket; import org.eclipse.jetty.websocket.api.extensions.Frame; @@ -34,7 +35,7 @@ import org.eclipse.jetty.websocket.api.extensions.Frame; public class EchoFragmentSocket { @OnWebSocketFrame - public void onFrame(WebSocketConnection conn, Frame frame) + public void onFrame(Session session, Frame frame) { if (frame.getType().isData()) { @@ -51,18 +52,19 @@ public class EchoFragmentSocket buf1.limit(half); buf2.position(half); + RemoteEndpoint remote = session.getRemote(); try { switch (frame.getType()) { case BINARY: - conn.write(buf1); - conn.write(buf2); + remote.sendBytesByFuture(buf1); + remote.sendBytesByFuture(buf2); break; case TEXT: // NOTE: This impl is not smart enough to split on a UTF8 boundary - conn.write(BufferUtil.toUTF8String(buf1)); - conn.write(BufferUtil.toUTF8String(buf2)); + remote.sendStringByFuture(BufferUtil.toUTF8String(buf1)); + remote.sendStringByFuture(BufferUtil.toUTF8String(buf2)); break; default: throw new IOException("Unexpected frame type: " + frame.getType()); diff --git a/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java b/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java index 836aa0cdfe7..03671464aa5 100644 --- a/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java +++ b/tests/test-webapps/test-jetty-webapp/src/main/java/com/acme/WebSocketChatServlet.java @@ -46,6 +46,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.websocket.api.RemoteEndpoint; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.UpgradeRequest; import org.eclipse.jetty.websocket.api.UpgradeResponse; @@ -90,11 +91,13 @@ public class WebSocketChatServlet extends WebSocketServlet implements WebSocketC public class ChatWebSocket { volatile Session session; + volatile RemoteEndpoint remote; @OnWebSocketConnect public void onOpen(Session sess) { - session = sess; + this.session = sess; + this.remote = sess.getRemote(); members.add(this); } @@ -127,7 +130,7 @@ public class WebSocketChatServlet extends WebSocketServlet implements WebSocketC } // Async write the message back. - member.session.getRemote().sendStringByFuture(data); + member.remote.sendStringByFuture(data); } }