Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2012-08-13 16:59:46 -0400
committerJoakim Erdfelt2012-08-13 16:59:46 -0400
commit1f78fc625026d2ee6cb1a5c5034bbb730d627ad0 (patch)
treeff5b98ed170c64fc02636ea4f3a84c453918ad5b /jetty-websocket
parent9f4375d1e58dfdf7c2de0bc1085f680420b6e7bf (diff)
downloadorg.eclipse.jetty.project-1f78fc625026d2ee6cb1a5c5034bbb730d627ad0.tar.gz
org.eclipse.jetty.project-1f78fc625026d2ee6cb1a5c5034bbb730d627ad0.tar.xz
org.eclipse.jetty.project-1f78fc625026d2ee6cb1a5c5034bbb730d627ad0.zip
Attempting to get client connection working
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java21
-rw-r--r--jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java2
-rw-r--r--jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/WebSocketClientSelectorManager.java49
-rw-r--r--jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/TrackingSocket.java89
-rw-r--r--jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/WebSocketClientTest.java184
-rw-r--r--jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/blockhead/BlockheadServer.java155
-rw-r--r--jetty-websocket/websocket-client/src/test/resources/jetty-logging.properties17
7 files changed, 299 insertions, 218 deletions
diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
index 6acf2aa7c4..a7c89eae6b 100644
--- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
+++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java
@@ -23,6 +23,8 @@ import java.nio.channels.SocketChannel;
import java.util.Map;
import org.eclipse.jetty.util.FutureCallback;
+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.WebSocketPolicy;
import org.eclipse.jetty.websocket.driver.WebSocketEventDriver;
@@ -31,6 +33,7 @@ public class WebSocketClient
{
public static class ConnectFuture extends FutureCallback<WebSocketConnection>
{
+ private static final Logger LOG = Log.getLogger(ConnectFuture.class);
private final WebSocketClient client;
private final URI websocketUri;
private final WebSocketEventDriver websocket;
@@ -45,6 +48,7 @@ public class WebSocketClient
@Override
public void completed(WebSocketConnection context)
{
+ LOG.debug("completed() - {}",context);
// TODO Auto-generated method stub
super.completed(context);
}
@@ -52,6 +56,8 @@ public class WebSocketClient
@Override
public void failed(WebSocketConnection context, Throwable cause)
{
+ LOG.debug("failed() - {}, {}",context,cause);
+ LOG.info(cause);
// TODO Auto-generated method stub
super.failed(context,cause);
}
@@ -89,15 +95,15 @@ public class WebSocketClient
}
}
+ private static final Logger LOG = Log.getLogger(WebSocketClient.class);
+
public static InetSocketAddress toSocketAddress(URI uri)
{
- // TODO Auto-generated method stub
- return null;
+ return new InetSocketAddress(uri.getHost(),uri.getPort());
}
private final WebSocketClientFactory factory;
private SocketAddress bindAddress;
-
private WebSocketPolicy policy;
public WebSocketClient(WebSocketClientFactory factory)
@@ -118,10 +124,12 @@ public class WebSocketClient
{
channel.bind(bindAddress);
}
- channel.socket().setTcpNoDelay(true);
- channel.configureBlocking(false);
- InetSocketAddress address = new InetSocketAddress(websocketUri.getHost(),websocketUri.getPort());
+ channel.socket().setTcpNoDelay(true); // disable nagle
+ channel.configureBlocking(false); // async all the way
+
+ InetSocketAddress address = toSocketAddress(websocketUri);
+ LOG.debug("Connect to {}",address);
WebSocketEventDriver websocket = this.factory.newWebSocketDriver(websocketPojo);
ConnectFuture result = new ConnectFuture(this,websocketUri,websocket);
@@ -159,6 +167,5 @@ public class WebSocketClient
public void setProtocol(String protocol)
{
// TODO Auto-generated method stub
-
}
}
diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java
index 950f4e37f5..1cd1225075 100644
--- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java
+++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClientFactory.java
@@ -148,7 +148,7 @@ public class WebSocketClientFactory extends AggregateLifeCycle
return new WebSocketClient(this);
}
- public WebSocketEventDriver newWebSocketDriver(Object websocketPojo)
+ protected WebSocketEventDriver newWebSocketDriver(Object websocketPojo)
{
return new WebSocketEventDriver(websocketPojo,methodsCache,policy,getBufferPool());
}
diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/WebSocketClientSelectorManager.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/WebSocketClientSelectorManager.java
index d8e3af710d..0fc42d0ccc 100644
--- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/WebSocketClientSelectorManager.java
+++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/WebSocketClientSelectorManager.java
@@ -58,7 +58,7 @@ public class WebSocketClientSelectorManager extends SelectorManager
@Override
protected void execute(Runnable task)
{
- // TODO Auto-generated method stub
+ this.executor.execute(task);
}
public SslContextFactory getSslContextFactory()
@@ -66,29 +66,10 @@ public class WebSocketClientSelectorManager extends SelectorManager
return sslContextFactory;
}
- public AbstractWebSocketConnection newWebSocketConnection(SocketChannel channel, EndPoint endPoint, Object attachment)
- {
- WebSocketClient.ConnectFuture confut = (WebSocketClient.ConnectFuture)attachment;
- WebSocketClientFactory factory = confut.getFactory();
- WebSocketEventDriver websocket = confut.getWebSocket();
-
- Executor executor = factory.getExecutor();
- WebSocketPolicy policy = factory.getPolicy();
- ByteBufferPool bufferPool = factory.getBufferPool();
- ScheduledExecutorService scheduler = factory.getScheduler();
-
- AbstractWebSocketConnection connection = new WebSocketClientConnection(endPoint,executor,scheduler,policy,bufferPool,factory);
- endPoint.setConnection(connection);
- connection.getParser().setIncomingFramesHandler(websocket);
-
- // TODO: track open websockets? bind open websocket to connection?
-
- return connection;
- }
-
@Override
- public Connection newConnection(SocketChannel channel, EndPoint endPoint, Object attachment)
+ public Connection newConnection(final SocketChannel channel, EndPoint endPoint, final Object attachment)
{
+ LOG.debug("newConnection({},{},{})",channel,endPoint,attachment);
WebSocketClient.ConnectFuture confut = (WebSocketClient.ConnectFuture)attachment;
try
@@ -136,7 +117,7 @@ public class WebSocketClientSelectorManager extends SelectorManager
}
@Override
- protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey selectionKey) throws IOException
+ protected EndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey selectionKey) throws IOException
{
return new SelectChannelEndPoint(channel,selectSet,selectionKey,scheduler,policy.getIdleTimeout());
}
@@ -145,11 +126,31 @@ public class WebSocketClientSelectorManager extends SelectorManager
{
String peerHost = channel.socket().getInetAddress().getHostAddress();
int peerPort = channel.socket().getPort();
- SSLEngine engine = sslContextFactory.newSSLEngine(peerHost, peerPort);
+ SSLEngine engine = sslContextFactory.newSSLEngine(peerHost,peerPort);
engine.setUseClientMode(true);
return engine;
}
+ public AbstractWebSocketConnection newWebSocketConnection(SocketChannel channel, EndPoint endPoint, Object attachment)
+ {
+ WebSocketClient.ConnectFuture confut = (WebSocketClient.ConnectFuture)attachment;
+ WebSocketClientFactory factory = confut.getFactory();
+ WebSocketEventDriver websocket = confut.getWebSocket();
+
+ Executor executor = factory.getExecutor();
+ WebSocketPolicy policy = factory.getPolicy();
+ ByteBufferPool bufferPool = factory.getBufferPool();
+ ScheduledExecutorService scheduler = factory.getScheduler();
+
+ AbstractWebSocketConnection connection = new WebSocketClientConnection(endPoint,executor,scheduler,policy,bufferPool,factory);
+ endPoint.setConnection(connection);
+ connection.getParser().setIncomingFramesHandler(websocket);
+
+ // TODO: track open websockets? bind open websocket to connection?
+
+ return connection;
+ }
+
public void setSslContextFactory(SslContextFactory sslContextFactory)
{
this.sslContextFactory = sslContextFactory;
diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/TrackingSocket.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/TrackingSocket.java
new file mode 100644
index 0000000000..089684b900
--- /dev/null
+++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/TrackingSocket.java
@@ -0,0 +1,89 @@
+package org.eclipse.jetty.websocket.client;
+
+import static org.hamcrest.Matchers.*;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.eclipse.jetty.util.BlockingArrayQueue;
+import org.eclipse.jetty.websocket.api.WebSocketAdapter;
+import org.eclipse.jetty.websocket.api.WebSocketConnection;
+import org.junit.Assert;
+
+public class TrackingSocket extends WebSocketAdapter
+{
+ public AtomicBoolean open = new AtomicBoolean(false);
+ public AtomicInteger close = new AtomicInteger(-1);
+ public StringBuilder closeMessage = new StringBuilder();
+ public CountDownLatch openLatch = new CountDownLatch(1);
+ public CountDownLatch closeLatch = new CountDownLatch(1);
+ public CountDownLatch dataLatch = new CountDownLatch(1);
+ public BlockingQueue<String> messageQueue = new BlockingArrayQueue<String>();
+
+ public void assertClose(int expectedStatusCode, String expectedReason)
+ {
+ assertCloseCode(expectedStatusCode);
+ assertCloseReason(expectedReason);
+ }
+
+ public void assertCloseCode(int expectedCode)
+ {
+ Assert.assertThat("Close Code",close.get(),is(expectedCode));
+ }
+
+ private void assertCloseReason(String expectedReason)
+ {
+ Assert.assertThat("Close Reaosn",closeMessage.toString(),is(expectedReason));
+ }
+
+ public void assertIsOpen()
+ {
+ assertWasOpened();
+ assertNotClosed();
+ }
+
+ public void assertNotClosed()
+ {
+ Assert.assertThat("Close Code",close.get(),is(-1));
+ }
+
+ public void assertNotOpened()
+ {
+ Assert.assertThat("Opened State",open.get(),is(false));
+ }
+
+ public void assertWasOpened()
+ {
+ Assert.assertThat("Opened State",open.get(),is(true));
+ }
+
+ @Override
+ public void onWebSocketBinary(byte[] payload, int offset, int len)
+ {
+ dataLatch.countDown();
+ }
+
+ @Override
+ public void onWebSocketClose(int statusCode, String reason)
+ {
+ close.set(statusCode);
+ closeMessage.append(reason);
+ closeLatch.countDown();
+ }
+
+ @Override
+ public void onWebSocketConnect(WebSocketConnection connection)
+ {
+ open.set(true);
+ openLatch.countDown();
+ }
+
+ @Override
+ public void onWebSocketText(String message)
+ {
+ dataLatch.countDown();
+ messageQueue.add(message);
+ }
+} \ No newline at end of file
diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/WebSocketClientTest.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/WebSocketClientTest.java
index d058393a23..4275b75a2b 100644
--- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/WebSocketClientTest.java
+++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/WebSocketClientTest.java
@@ -15,15 +15,12 @@
//========================================================================
package org.eclipse.jetty.websocket.client;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.greaterThan;
-import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.URI;
-import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutionException;
@@ -34,7 +31,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
-import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
@@ -45,90 +41,20 @@ import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnec
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
-@Ignore("Client not yet functional")
public class WebSocketClientTest
{
- public static class TrackingSocket extends WebSocketAdapter
- {
- public AtomicBoolean open = new AtomicBoolean(false);
- public AtomicInteger close = new AtomicInteger(-1);
- public StringBuilder closeMessage = new StringBuilder();
- public CountDownLatch openLatch = new CountDownLatch(1);
- public CountDownLatch closeLatch = new CountDownLatch(1);
- public CountDownLatch dataLatch = new CountDownLatch(1);
- public BlockingQueue<String> messageQueue = new BlockingArrayQueue<String>();
-
- public void assertClose(int expectedStatusCode, String expectedReason)
- {
- assertCloseCode(expectedStatusCode);
- assertCloseReason(expectedReason);
- }
-
- public void assertCloseCode(int expectedCode)
- {
- Assert.assertThat("Close Code",close.get(),is(expectedCode));
- }
-
- private void assertCloseReason(String expectedReason)
- {
- Assert.assertThat("Close Reaosn",closeMessage.toString(),is(expectedReason));
- }
-
- public void assertIsOpen()
- {
- assertWasOpened();
- assertNotClosed();
- }
-
- public void assertNotClosed()
- {
- Assert.assertThat("Close Code",close.get(),is(-1));
- }
-
- public void assertNotOpened()
- {
- Assert.assertThat("Opened State",open.get(),is(false));
- }
-
- public void assertWasOpened()
- {
- Assert.assertThat("Opened State",open.get(),is(true));
- }
-
- @Override
- public void onWebSocketBinary(byte[] payload, int offset, int len)
- {
- dataLatch.countDown();
- }
-
- @Override
- public void onWebSocketClose(int statusCode, String reason)
- {
- close.set(statusCode);
- closeMessage.append(reason);
- closeLatch.countDown();
- }
-
- @Override
- public void onWebSocketConnect(WebSocketConnection connection)
- {
- open.set(true);
- openLatch.countDown();
- }
+ private BlockheadServer server;
+ private WebSocketClientFactory factory;
- @Override
- public void onWebSocketText(String message)
- {
- dataLatch.countDown();
- messageQueue.add(message);
- }
+ @Before
+ public void startFactory() throws Exception
+ {
+ factory = new WebSocketClientFactory();
+ factory.start();
}
- private BlockheadServer server;
-
@Before
public void startServer() throws Exception
{
@@ -137,58 +63,37 @@ public class WebSocketClientTest
}
@After
- public void stopServer() throws Exception
+ public void stopFactory() throws Exception
{
- server.close();
+ factory.stop();
}
- @Test
- public void testConnectionRefused() throws Exception
+ @After
+ public void stopServer() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
- WebSocketClient client = factory.newWebSocketClient();
-
- TrackingSocket wsocket = new TrackingSocket();
-
- // Intentionally bad port
- URI wsUri = new URI("ws://127.0.0.1:1");
- Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
-
- Throwable error = null;
- try
- {
- future.get(1,TimeUnit.SECONDS);
- Assert.fail("Expected ExecutionException");
- }
- catch (ExecutionException e)
- {
- error = e.getCause();
- }
-
- wsocket.assertNotOpened();
- wsocket.assertCloseCode(StatusCode.NO_CLOSE);
- Assert.assertTrue(error instanceof ConnectException);
+ server.stop();
}
@Test
public void testBadHandshake() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection connection = server.accept();
+ String req = connection.readRequest();
+ // no upgrade, just fail with a 404 error
connection.respond("HTTP/1.1 404 NOT FOUND\r\n\r\n");
Throwable error = null;
try
{
future.get(250,TimeUnit.MILLISECONDS);
- Assert.fail();
+ Assert.fail("Should have resulted in an ExecutionException -> IOException");
}
catch (ExecutionException e)
{
@@ -205,12 +110,11 @@ public class WebSocketClientTest
@Test
public void testBadUpgrade() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection connection = server.accept();
@@ -236,7 +140,6 @@ public class WebSocketClientTest
@Test
public void testBadURL() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
@@ -259,7 +162,6 @@ public class WebSocketClientTest
@Test
public void testBlockReceiving() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
client.getPolicy().setIdleTimeout(60000);
@@ -299,7 +201,7 @@ public class WebSocketClientTest
}
};
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,socket);
ServerConnection sconnection = server.accept();
@@ -388,13 +290,12 @@ public class WebSocketClientTest
@Test
public void testBlockSending() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
client.getPolicy().setIdleTimeout(10000);
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
final ServerConnection ssocket = server.accept();
@@ -465,12 +366,11 @@ public class WebSocketClientTest
@Test
public void testConnectionNotAccepted() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
// Intentionally not accept incoming socket.
@@ -490,14 +390,40 @@ public class WebSocketClientTest
}
@Test
+ public void testConnectionRefused() throws Exception
+ {
+ WebSocketClient client = factory.newWebSocketClient();
+
+ TrackingSocket wsocket = new TrackingSocket();
+
+ // Intentionally bad port
+ URI wsUri = new URI("ws://127.0.0.1:1");
+ Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
+
+ Throwable error = null;
+ try
+ {
+ future.get(1,TimeUnit.SECONDS);
+ Assert.fail("Expected ExecutionException");
+ }
+ catch (ExecutionException e)
+ {
+ error = e.getCause();
+ }
+
+ wsocket.assertNotOpened();
+ wsocket.assertCloseCode(StatusCode.NO_CLOSE);
+ Assert.assertTrue(error instanceof ConnectException);
+ }
+
+ @Test
public void testConnectionTimeout() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection ssocket = server.accept();
@@ -521,13 +447,12 @@ public class WebSocketClientTest
@Test
public void testIdle() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
client.getPolicy().setIdleTimeout(500);
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection ssocket = server.accept();
@@ -547,14 +472,13 @@ public class WebSocketClientTest
@Test
public void testMessageBiggerThanBufferSize() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
int bufferSize = 512;
factory.getPolicy().setBufferSize(512);
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection ssocket = server.accept();
@@ -579,13 +503,12 @@ public class WebSocketClientTest
@Test
public void testNotIdle() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
client.getPolicy().setIdleTimeout(500);
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection ssocket = server.accept();
@@ -633,12 +556,11 @@ public class WebSocketClientTest
@Test
public void testUpgradeThenTCPClose() throws Exception
{
- WebSocketClientFactory factory = new WebSocketClientFactory();
WebSocketClient client = factory.newWebSocketClient();
TrackingSocket wsocket = new TrackingSocket();
- URI wsUri = new URI("ws://127.0.0.1:" + server.getPort() + "/");
+ URI wsUri = server.getWsUri();
Future<WebSocketConnection> future = client.connect(wsUri,wsocket);
ServerConnection ssocket = server.accept();
diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/blockhead/BlockheadServer.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/blockhead/BlockheadServer.java
index 4dc00cea2f..5097e25f55 100644
--- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/blockhead/BlockheadServer.java
+++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/blockhead/BlockheadServer.java
@@ -15,15 +15,25 @@
//========================================================================
package org.eclipse.jetty.websocket.client.blockhead;
+import static org.hamcrest.Matchers.*;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketException;
+import java.net.URI;
import org.eclipse.jetty.util.IO;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.protocol.AcceptHash;
+import org.junit.Assert;
/**
* A overly simplistic websocket server used during testing.
@@ -34,35 +44,72 @@ public class BlockheadServer
{
public static class ServerConnection
{
+ private final Socket socket;
+ private OutputStream out;
+ private InputStream in;
- public void close()
+ public ServerConnection(Socket socket)
{
- // TODO Auto-generated method stub
+ this.socket = socket;
+ }
+ public void close() throws IOException
+ {
+ this.socket.close();
}
- public void flush()
+ public void flush() throws IOException
{
- // TODO Auto-generated method stub
+ getOutputStream().flush();
+ }
+ public InputStream getInputStream() throws IOException
+ {
+ if (in == null)
+ {
+ in = socket.getInputStream();
+ }
+ return in;
}
- public InputStream getInputStream()
+ private OutputStream getOutputStream() throws IOException
{
- // TODO Auto-generated method stub
- return null;
+ if (out == null)
+ {
+ out = socket.getOutputStream();
+ }
+ return out;
}
- public void respond(String rawstr)
+ public String readRequest() throws IOException
{
- // TODO Auto-generated method stub
+ LOG.debug("Reading client request");
+ StringBuilder request = new StringBuilder();
+ BufferedReader in = new BufferedReader(new InputStreamReader(getInputStream()));
+ for (String line = in.readLine(); line != null; line = in.readLine())
+ {
+ if (line.length() == 0)
+ {
+ break;
+ }
+ request.append(line).append("\r\n");
+ LOG.debug("read line: {}",line);
+ }
+ LOG.debug("Client Request:{}{}","\n",request);
+ return request.toString();
}
- public void setSoTimeout(int ms)
+ public void respond(String rawstr) throws IOException
{
- // TODO Auto-generated method stub
+ LOG.debug("respond(){}{}","\n",rawstr);
+ getOutputStream().write(rawstr.getBytes());
+ flush();
+ }
+ public void setSoTimeout(int ms) throws SocketException
+ {
+ socket.setSoTimeout(ms);
}
public void upgrade() throws IOException
@@ -91,67 +138,46 @@ public class BlockheadServer
write(resp.toString().getBytes());
}
- private void write(byte[] bytes)
+ private void write(byte[] bytes) throws IOException
{
- // TODO Auto-generated method stub
-
+ getOutputStream().write(bytes);
}
- public void write(byte[] buf, int offset, int length)
+ public void write(byte[] buf, int offset, int length) throws IOException
{
- // TODO Auto-generated method stub
-
+ getOutputStream().write(buf,offset,length);
}
- public void write(int b)
+ public void write(int b) throws IOException
{
- // TODO Auto-generated method stub
-
+ getOutputStream().write(b);
}
}
- public ServerConnection accept()
- {
- // TODO Auto-generated method stub
- return null;
- }
+ private static final Logger LOG = Log.getLogger(BlockheadServer.class);
+ private ServerSocket serverSocket;
+ private URI wsUri;
- public void accept(Socket connection) throws IOException
+ public ServerConnection accept() throws IOException
{
- String key = "not sent";
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- for (String line = in.readLine(); line != null; line = in.readLine())
- {
- if (line.length() == 0)
- {
- break;
- }
- if (line.startsWith("Sec-WebSocket-Key:"))
- {
- key = line.substring(18).trim();
- }
- }
-
- StringBuilder resp = new StringBuilder();
- resp.append("HTTP/1.1 101 Upgrade\r\n");
- resp.append("Sec-WebSocket-Accept: ");
- resp.append(AcceptHash.hashKey(key));
- resp.append("\r\n");
- resp.append("\r\n");
-
- connection.getOutputStream().write(resp.toString().getBytes());
+ LOG.debug(".accept()");
+ assertIsStarted();
+ Socket socket = serverSocket.accept();
+ return new ServerConnection(socket);
}
- public void close()
+ private void assertIsStarted()
{
- // TODO Auto-generated method stub
+ Assert.assertThat("ServerSocket",serverSocket,notNullValue());
+ Assert.assertThat("ServerSocket.isBound",serverSocket.isBound(),is(true));
+ Assert.assertThat("ServerSocket.isClosed",serverSocket.isClosed(),is(false));
+ Assert.assertThat("WsUri",wsUri,notNullValue());
}
- public int getPort()
+ public URI getWsUri()
{
- // TODO Auto-generated method stub
- return -1;
+ return wsUri;
}
public void respondToClient(Socket connection, String serverResponse) throws IOException
@@ -190,9 +216,28 @@ public class BlockheadServer
}
}
- public void start()
+ public void start() throws IOException
{
- // TODO Auto-generated method stub
+ serverSocket = new ServerSocket();
+ InetAddress addr = InetAddress.getByName("localhost");
+ InetSocketAddress endpoint = new InetSocketAddress(addr,0);
+ serverSocket.bind(endpoint);
+ int port = serverSocket.getLocalPort();
+ String uri = String.format("ws://%s:%d/",addr.getHostAddress(),port);
+ wsUri = URI.create(uri);
+ LOG.debug("Server Started on {} -> {}",endpoint,wsUri);
+ }
+ public void stop()
+ {
+ LOG.debug("Stopping Server");
+ try
+ {
+ serverSocket.close();
+ }
+ catch (IOException ignore)
+ {
+ /* ignore */
+ }
}
}
diff --git a/jetty-websocket/websocket-client/src/test/resources/jetty-logging.properties b/jetty-websocket/websocket-client/src/test/resources/jetty-logging.properties
new file mode 100644
index 0000000000..ea04a9c962
--- /dev/null
+++ b/jetty-websocket/websocket-client/src/test/resources/jetty-logging.properties
@@ -0,0 +1,17 @@
+org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
+org.eclipse.jetty.LEVEL=WARN
+# org.eclipse.jetty.io.ChannelEndPoint.LEVEL=INFO
+# org.eclipse.jetty.websocket.LEVEL=WARN
+org.eclipse.jetty.websocket.LEVEL=DEBUG
+# See the read/write traffic
+# org.eclipse.jetty.websocket.io.Frames.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.io.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.io.WebSocketAsyncConnection.LEVEL=DEBUG
+# org.eclipse.jetty.io.SelectorManager.LEVEL=INFO
+# org.eclipse.jetty.websocket.io.FrameBytes.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.io.ControlFrameBytes.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.driver.WebSocketEventDriver.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.extensions.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.protocol.Generator.LEVEL=INFO
+# org.eclipse.jetty.websocket.protocol.Parser.LEVEL=DEBUG
+# org.eclipse.jetty.websocket.io.payload.LEVEL=DEBUG

Back to the top