Changing to ClientUpgrade(Request|Response)

This commit is contained in:
Joakim Erdfelt 2012-11-27 15:00:22 -07:00
parent acaeb7663a
commit d8b1444609
10 changed files with 43 additions and 91 deletions

View File

@ -22,15 +22,13 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.masks.Masker; import org.eclipse.jetty.websocket.client.masks.Masker;
import org.eclipse.jetty.websocket.common.events.EventDriver; import org.eclipse.jetty.websocket.common.events.EventDriver;
public interface WebSocketClient public interface WebSocketClient
{ {
public Future<UpgradeResponse> connect(URI websocketUri) throws IOException; public Future<ClientUpgradeResponse> connect(URI websocketUri) throws IOException;
public WebSocketClientFactory getFactory(); public WebSocketClientFactory getFactory();
@ -38,9 +36,9 @@ public interface WebSocketClient
public WebSocketPolicy getPolicy(); public WebSocketPolicy getPolicy();
public UpgradeRequest getUpgradeRequest(); public ClientUpgradeRequest getUpgradeRequest();
public UpgradeResponse getUpgradeResponse(); public ClientUpgradeResponse getUpgradeResponse();
public EventDriver getWebSocket(); public EventDriver getWebSocket();

View File

@ -32,14 +32,13 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.ClientUpgradeResponse;
import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.internal.io.WebSocketClientSelectorManager; import org.eclipse.jetty.websocket.client.internal.io.WebSocketClientSelectorManager;
@ -115,7 +114,7 @@ public class ConnectionManager extends ContainerLifeCycle
} }
} }
public Future<UpgradeResponse> connectPhysical(DefaultWebSocketClient client) throws IOException public Future<ClientUpgradeResponse> connectPhysical(DefaultWebSocketClient client) throws IOException
{ {
SocketChannel channel = SocketChannel.open(); SocketChannel channel = SocketChannel.open();
SocketAddress bindAddress = client.getFactory().getBindAddress(); SocketAddress bindAddress = client.getFactory().getBindAddress();
@ -138,7 +137,7 @@ public class ConnectionManager extends ContainerLifeCycle
return client; return client;
} }
public Future<UpgradeResponse> connectVirtual(WebSocketClient client) public Future<ClientUpgradeResponse> connectVirtual(WebSocketClient client)
{ {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;

View File

@ -23,13 +23,10 @@ import java.net.URI;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.FuturePromise; import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
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.WebSocketConnection;
import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
@ -43,7 +40,7 @@ import org.eclipse.jetty.websocket.common.events.EventDriver;
/** /**
* WebSocketClient for working with Upgrade (request and response), and establishing connections to the websocket URI of your choice. * WebSocketClient for working with Upgrade (request and response), and establishing connections to the websocket URI of your choice.
*/ */
public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> implements WebSocketClient public class DefaultWebSocketClient extends FuturePromise<ClientUpgradeResponse> implements WebSocketClient
{ {
private static final Logger LOG = Log.getLogger(DefaultWebSocketClient.class); private static final Logger LOG = Log.getLogger(DefaultWebSocketClient.class);
@ -74,19 +71,7 @@ public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> imple
} }
@Override @Override
public void succeeded(UpgradeResponse response) public Future<ClientUpgradeResponse> connect(URI websocketUri) throws IOException
{
LOG.debug("completed() - {}",response);
super.succeeded(response);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#connect(java.net.URI)
*/
@Override
public Future<UpgradeResponse> connect(URI websocketUri) throws IOException
{ {
if (!factory.isStarted()) if (!factory.isStarted())
{ {
@ -113,7 +98,7 @@ public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> imple
this.websocketUri = websocketUri; this.websocketUri = websocketUri;
// Validate websocket URI // Validate websocket URI
Future<UpgradeResponse> result = null; Future<ClientUpgradeResponse> result = null;
LOG.debug("connect({})",websocketUri); LOG.debug("connect({})",websocketUri);
@ -147,11 +132,6 @@ public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> imple
return this.connection; return this.connection;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getFactory()
*/
@Override @Override
public WebSocketClientFactory getFactory() public WebSocketClientFactory getFactory()
{ {
@ -164,55 +144,30 @@ public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> imple
return masker; return masker;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getPolicy()
*/
@Override @Override
public WebSocketPolicy getPolicy() public WebSocketPolicy getPolicy()
{ {
return this.policy; return this.policy;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getUpgradeRequest()
*/
@Override @Override
public UpgradeRequest getUpgradeRequest() public ClientUpgradeRequest getUpgradeRequest()
{ {
return upgradeRequest; return upgradeRequest;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getUpgradeResponse()
*/
@Override @Override
public UpgradeResponse getUpgradeResponse() public ClientUpgradeResponse getUpgradeResponse()
{ {
return upgradeResponse; return upgradeResponse;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getWebSocket()
*/
@Override @Override
public EventDriver getWebSocket() public EventDriver getWebSocket()
{ {
return websocket; return websocket;
} }
/*
* (non-Javadoc)
*
* @see org.eclipse.jetty.websocket.client.internal.WebSocketClient#getWebSocketUri()
*/
@Override @Override
public URI getWebSocketUri() public URI getWebSocketUri()
{ {
@ -229,4 +184,11 @@ public class DefaultWebSocketClient extends FuturePromise<UpgradeResponse> imple
{ {
this.upgradeResponse = response; this.upgradeResponse = response;
} }
@Override
public void succeeded(ClientUpgradeResponse response)
{
LOG.debug("completed() - {}",response);
super.succeeded(response);
}
} }

View File

@ -30,13 +30,11 @@ import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.QuotedStringTokenizer; import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.UpgradeException; import org.eclipse.jetty.websocket.api.UpgradeException;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.Extension; import org.eclipse.jetty.websocket.api.extensions.Extension;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig; import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
@ -57,15 +55,6 @@ public class UpgradeConnection extends AbstractConnection
{ {
public class SendUpgradeRequest extends FutureCallback implements Runnable public class SendUpgradeRequest extends FutureCallback implements Runnable
{ {
@Override
public void succeeded()
{
// Writing the request header is complete.
super.succeeded();
// start the interest in fill
fillInterested();
}
@Override @Override
public void run() public void run()
{ {
@ -76,6 +65,15 @@ public class UpgradeConnection extends AbstractConnection
ByteBuffer buf = BufferUtil.toBuffer(rawRequest,StringUtil.__UTF8_CHARSET); ByteBuffer buf = BufferUtil.toBuffer(rawRequest,StringUtil.__UTF8_CHARSET);
getEndPoint().write(this,buf); getEndPoint().write(this,buf);
} }
@Override
public void succeeded()
{
// Writing the request header is complete.
super.succeeded();
// start the interest in fill
fillInterested();
}
} }
private static final Logger LOG = Log.getLogger(UpgradeConnection.class); private static final Logger LOG = Log.getLogger(UpgradeConnection.class);
@ -93,7 +91,7 @@ public class UpgradeConnection extends AbstractConnection
try try
{ {
this.request = (ClientUpgradeRequest)client.getUpgradeRequest(); this.request = client.getUpgradeRequest();
} }
catch (ClassCastException e) catch (ClassCastException e)
{ {
@ -115,7 +113,7 @@ public class UpgradeConnection extends AbstractConnection
} }
} }
private void notifyConnect(UpgradeResponse response) private void notifyConnect(ClientUpgradeResponse response)
{ {
client.succeeded(response); client.succeeded(response);
} }

View File

@ -26,7 +26,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketConnection; import org.eclipse.jetty.websocket.api.WebSocketConnection;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection;
@ -84,7 +83,7 @@ public class BadNetworkTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection ssocket = server.accept(); ServerConnection ssocket = server.accept();
ssocket.upgrade(); ssocket.upgrade();
@ -115,7 +114,7 @@ public class BadNetworkTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection ssocket = server.accept(); ServerConnection ssocket = server.accept();
ssocket.upgrade(); ssocket.upgrade();

View File

@ -28,7 +28,6 @@ import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.websocket.api.UpgradeException; import org.eclipse.jetty.websocket.api.UpgradeException;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection;
import org.junit.After; import org.junit.After;
@ -82,7 +81,7 @@ public class ClientConnectTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection connection = server.accept(); ServerConnection connection = server.accept();
connection.readRequest(); connection.readRequest();
@ -109,7 +108,7 @@ public class ClientConnectTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection connection = server.accept(); ServerConnection connection = server.accept();
connection.readRequest(); connection.readRequest();
@ -136,7 +135,7 @@ public class ClientConnectTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
// Intentionally not accept incoming socket. // Intentionally not accept incoming socket.
// server.accept(); // server.accept();
@ -162,7 +161,7 @@ public class ClientConnectTest
// Intentionally bad port // Intentionally bad port
URI wsUri = new URI("ws://127.0.0.1:1"); URI wsUri = new URI("ws://127.0.0.1:1");
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
// The attempt to get upgrade response future should throw error // The attempt to get upgrade response future should throw error
try try
@ -184,7 +183,7 @@ public class ClientConnectTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection ssocket = server.accept(); ServerConnection ssocket = server.accept();
Assert.assertNotNull(ssocket); Assert.assertNotNull(ssocket);

View File

@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.toolchain.test.annotation.Slow; import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection;
import org.junit.After; import org.junit.After;
@ -82,7 +81,7 @@ public class SlowClientTest
client.getPolicy().setIdleTimeout(60000); client.getPolicy().setIdleTimeout(60000);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection sconnection = server.accept(); ServerConnection sconnection = server.accept();
sconnection.setSoTimeout(60000); sconnection.setSoTimeout(60000);

View File

@ -27,7 +27,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.toolchain.test.annotation.Slow; import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection;
import org.junit.After; import org.junit.After;
@ -82,7 +81,7 @@ public class SlowServerTest
client.getPolicy().setIdleTimeout(60000); client.getPolicy().setIdleTimeout(60000);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection sconnection = server.accept(); ServerConnection sconnection = server.accept();
sconnection.setSoTimeout(60000); sconnection.setSoTimeout(60000);
@ -133,7 +132,7 @@ public class SlowServerTest
client.getPolicy().setIdleTimeout(60000); client.getPolicy().setIdleTimeout(60000);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection sconnection = server.accept(); ServerConnection sconnection = server.accept();
sconnection.setSoTimeout(60000); sconnection.setSoTimeout(60000);

View File

@ -26,7 +26,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer;
import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection; import org.eclipse.jetty.websocket.client.blockhead.BlockheadServer.ServerConnection;
import org.junit.After; import org.junit.After;
@ -87,7 +86,7 @@ public class TimeoutTest
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection ssocket = server.accept(); ServerConnection ssocket = server.accept();
ssocket.upgrade(); ssocket.upgrade();

View File

@ -79,7 +79,7 @@ public class WebSocketClientTest
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
UpgradeRequest request = client.getUpgradeRequest(); UpgradeRequest request = client.getUpgradeRequest();
request.setSubProtocols("echo"); request.setSubProtocols("echo");
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
final ServerConnection srvSock = server.accept(); final ServerConnection srvSock = server.accept();
srvSock.upgrade(); srvSock.upgrade();
@ -106,7 +106,7 @@ public class WebSocketClientTest
{ {
TrackingSocket wsocket = new TrackingSocket(); TrackingSocket wsocket = new TrackingSocket();
WebSocketClient client = factory.newWebSocketClient(wsocket); WebSocketClient client = factory.newWebSocketClient(wsocket);
Future<UpgradeResponse> future = client.connect(server.getWsUri()); Future<ClientUpgradeResponse> future = client.connect(server.getWsUri());
// Server // Server
final ServerConnection srvSock = server.accept(); final ServerConnection srvSock = server.accept();
@ -142,7 +142,7 @@ public class WebSocketClientTest
WebSocketClient client = factSmall.newWebSocketClient(wsocket); WebSocketClient client = factSmall.newWebSocketClient(wsocket);
URI wsUri = server.getWsUri(); URI wsUri = server.getWsUri();
Future<UpgradeResponse> future = client.connect(wsUri); Future<ClientUpgradeResponse> future = client.connect(wsUri);
ServerConnection ssocket = server.accept(); ServerConnection ssocket = server.accept();
ssocket.upgrade(); ssocket.upgrade();