mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-04 04:49:12 +00:00
Attempting to get client connection working
This commit is contained in:
parent
9f4375d1e5
commit
1f78fc6250
@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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,89 +41,19 @@ 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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketText(String message)
|
||||
{
|
||||
dataLatch.countDown();
|
||||
messageQueue.add(message);
|
||||
}
|
||||
}
|
||||
|
||||
private BlockheadServer server;
|
||||
private WebSocketClientFactory factory;
|
||||
|
||||
@Before
|
||||
public void startFactory() throws Exception
|
||||
{
|
||||
factory = new WebSocketClientFactory();
|
||||
factory.start();
|
||||
}
|
||||
|
||||
@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 testConnectionTimeout() throws Exception
|
||||
public void testConnectionRefused() 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() + "/");
|
||||
// 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
|
||||
{
|
||||
WebSocketClient client = factory.newWebSocketClient();
|
||||
|
||||
TrackingSocket wsocket = new TrackingSocket();
|
||||
|
||||
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();
|
||||
|
@ -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 flush()
|
||||
public void close() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
public InputStream getInputStream()
|
||||
public void flush() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
getOutputStream().flush();
|
||||
}
|
||||
|
||||
public void respond(String rawstr)
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
if (in == null)
|
||||
{
|
||||
in = socket.getInputStream();
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
public void setSoTimeout(int ms)
|
||||
private OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
if (out == null)
|
||||
{
|
||||
out = socket.getOutputStream();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public String readRequest() throws IOException
|
||||
{
|
||||
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 respond(String rawstr) throws IOException
|
||||
{
|
||||
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()
|
||||
private static final Logger LOG = Log.getLogger(BlockheadServer.class);
|
||||
private ServerSocket serverSocket;
|
||||
private URI wsUri;
|
||||
|
||||
public ServerConnection accept() throws IOException
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
LOG.debug(".accept()");
|
||||
assertIsStarted();
|
||||
Socket socket = serverSocket.accept();
|
||||
return new ServerConnection(socket);
|
||||
}
|
||||
|
||||
public void accept(Socket connection) throws IOException
|
||||
private void assertIsStarted()
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
Assert.assertThat("ServerSocket",serverSocket,notNullValue());
|
||||
Assert.assertThat("ServerSocket.isBound",serverSocket.isBound(),is(true));
|
||||
Assert.assertThat("ServerSocket.isClosed",serverSocket.isClosed(),is(false));
|
||||
|
||||
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());
|
||||
Assert.assertThat("WsUri",wsUri,notNullValue());
|
||||
}
|
||||
|
||||
public void close()
|
||||
public URI getWsUri()
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public int getPort()
|
||||
{
|
||||
// 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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user