Renamed methods to enable/disable masking to is/setMaskingEnabled().

This commit is contained in:
Simone Bordet 2011-08-25 12:26:33 +02:00
parent a7a45bf7d9
commit 97cc150144
1 changed files with 74 additions and 74 deletions

View File

@ -41,8 +41,8 @@ import org.eclipse.jetty.websocket.WebSocketGeneratorD12.MaskGen;
/* ------------------------------------------------------------ */
/** WebSocket Client
* <p>This WebSocket Client class can create multiple websocket connections to multiple destinations.
* It uses the same {@link WebSocket} endpoint API as the server.
* <p>This WebSocket Client class can create multiple websocket connections to multiple destinations.
* It uses the same {@link WebSocket} endpoint API as the server.
* Simple usage is as follows: <pre>
* WebSocketClient client = new WebSocketClient();
* client.setMaxIdleTime(500);
@ -59,19 +59,19 @@ import org.eclipse.jetty.websocket.WebSocketGeneratorD12.MaskGen;
* {
* // close notification
* }
*
*
* public void onMessage(String data)
* {
* // handle incoming message
* }
* }).get(5,TimeUnit.SECONDS);
*
*
* connection.sendMessage("Hello World");
* </pre>
* </pre>
*/
public class WebSocketClient extends AggregateLifeCycle
{
private final static Logger __log = org.eclipse.jetty.util.log.Log.getLogger(WebSocketClient.class.getCanonicalName());
{
private final static Logger __log = org.eclipse.jetty.util.log.Log.getLogger(WebSocketClient.class.getName());
private final static Random __random = new Random();
private final static ByteArrayBuffer __ACCEPT = new ByteArrayBuffer.CaseInsensitive("Sec-WebSocket-Accept");
@ -82,15 +82,15 @@ public class WebSocketClient extends AggregateLifeCycle
private final Map<String,String> _cookies=new ConcurrentHashMap<String, String>();
private final List<String> _extensions=new CopyOnWriteArrayList<String>();
private int _bufferSize=64*1024;
private String _origin;
private String _protocol;
private int _maxIdleTime=-1;
private WebSocketBuffers _buffers;
private boolean _nullMask;
private boolean _maskingEnabled = true;
/* ------------------------------------------------------------ */
/** Create a WebSocket Client with default configuration.
@ -99,7 +99,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
this(new QueuedThreadPool());
}
/* ------------------------------------------------------------ */
/** Create a WebSocket Client with shared threadpool.
* @param threadpool
@ -113,11 +113,11 @@ public class WebSocketClient extends AggregateLifeCycle
addBean(_selector);
addBean(_threadPool);
}
/* ------------------------------------------------------------ */
/** Create a WebSocket Client from another.
* <p>If multiple clients are required so that connections created may have different
* configurations, then it is more efficient to create a client based on another, so
* <p>If multiple clients are required so that connections created may have different
* configurations, then it is more efficient to create a client based on another, so
* that the thread pool and IO infrastructure may be shared.
*/
public WebSocketClient(WebSocketClient parent)
@ -128,7 +128,7 @@ public class WebSocketClient extends AggregateLifeCycle
_selector=parent._selector;
_parent.addBean(this);
}
/* ------------------------------------------------------------ */
/**
* Get the selectorManager. Used to configure the manager.
@ -138,7 +138,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
return _selector;
}
/* ------------------------------------------------------------ */
/** Get the ThreadPool.
* <p>Used to set/query the thread pool configuration.
@ -148,7 +148,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
return _threadPool;
}
/* ------------------------------------------------------------ */
/** Get the maxIdleTime for connections opened by this client.
* @return The maxIdleTime in ms, or -1 if the default from {@link #getSelectorManager()} is used.
@ -186,7 +186,7 @@ public class WebSocketClient extends AggregateLifeCycle
throw new IllegalStateException(getState());
_bufferSize = bufferSize;
}
/* ------------------------------------------------------------ */
/** Get the subprotocol string for connections opened by this client.
* @return The subprotocol
@ -204,7 +204,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
_protocol = protocol;
}
/* ------------------------------------------------------------ */
/** Get the origin of the client
* @return The clients Origin
@ -228,30 +228,30 @@ public class WebSocketClient extends AggregateLifeCycle
{
return _cookies;
}
/* ------------------------------------------------------------ */
public List<String> getExtensions()
{
return _extensions;
}
/* ------------------------------------------------------------ */
/**
* @return True if a null mask is used.
/**
* @return whether masking is enabled.
*/
public boolean isNullMask()
public boolean isMaskingEnabled()
{
return _nullMask;
return _maskingEnabled;
}
/* ------------------------------------------------------------ */
/**
* @param maskGen
* @param maskingEnabled whether to enable masking
*/
public void setNullMaskGen(boolean nullMask)
public void setMaskingEnabled(boolean maskingEnabled)
{
_nullMask=nullMask;
_maskingEnabled=maskingEnabled;
}
/* ------------------------------------------------------------ */
@ -284,12 +284,12 @@ public class WebSocketClient extends AggregateLifeCycle
throw new RuntimeException(cause);
}
}
/* ------------------------------------------------------------ */
/** Asynchronously open a websocket connection.
* Open a websocket connection and return a {@link Future} to obtain the connection.
* Open a websocket connection and return a {@link Future} to obtain the connection.
* The caller must call {@link Future#get(long, TimeUnit)} if they wish to impose a connect timeout on the open.
*
*
* @param uri The URI to connect to.
* @param websocket The {@link WebSocket} instance to handle incoming events.
* @return A {@link Future} to the {@link WebSocket.Connection}
@ -304,7 +304,7 @@ public class WebSocketClient extends AggregateLifeCycle
throw new IllegalArgumentException("Bad WebSocket scheme '"+scheme+"'");
if ("wss".equalsIgnoreCase(scheme))
throw new IOException("wss not supported");
SocketChannel channel = SocketChannel.open();
channel.socket().setTcpNoDelay(true);
int maxIdleTime = getMaxIdleTime();
@ -323,18 +323,18 @@ public class WebSocketClient extends AggregateLifeCycle
return holder;
}
@Override
protected void doStart() throws Exception
{
if (_parent!=null && !_parent.isRunning())
throw new IllegalStateException("parent:"+getState());
_buffers = new WebSocketBuffers(_bufferSize);
_buffers = new WebSocketBuffers(_bufferSize);
super.doStart();
// Start a selector and timer if this is the root client
if (_parent==null)
{
@ -362,7 +362,7 @@ public class WebSocketClient extends AggregateLifeCycle
}
}
/* ------------------------------------------------------------ */
/** WebSocket Client Selector Manager
*/
@ -386,7 +386,7 @@ public class WebSocketClient extends AggregateLifeCycle
WebSocketFuture holder = (WebSocketFuture) endpoint.getSelectionKey().attachment();
return new HandshakeConnection(endpoint,holder);
}
@Override
protected void endPointOpened(SelectChannelEndPoint endpoint)
{
@ -414,13 +414,13 @@ public class WebSocketClient extends AggregateLifeCycle
{
__log.debug(ex);
WebSocketFuture holder = (WebSocketFuture)attachment;
holder.handshakeFailed(ex);
}
}
}
}
/* ------------------------------------------------------------ */
/** Handshake Connection.
* Handles the connection until the handshake succeeds or fails.
@ -433,21 +433,21 @@ public class WebSocketClient extends AggregateLifeCycle
private final HttpParser _parser;
private String _accept;
private String _error;
public HandshakeConnection(SelectChannelEndPoint endpoint, WebSocketFuture holder)
{
super(endpoint,System.currentTimeMillis());
_endp=endpoint;
_holder=holder;
byte[] bytes=new byte[16];
__random.nextBytes(bytes);
_key=new String(B64Code.encode(bytes));
Buffers buffers = new SimpleBuffers(_buffers.getBuffer(),null);
_parser=new HttpParser(buffers,_endp,
new HttpParser.EventHandler()
{
@Override
@ -459,7 +459,7 @@ public class WebSocketClient extends AggregateLifeCycle
_endp.close();
}
}
@Override
public void parsedHeader(Buffer name, Buffer value) throws IOException
{
@ -474,7 +474,7 @@ public class WebSocketClient extends AggregateLifeCycle
_error="Bad response: "+method+" "+url+" "+version;
_endp.close();
}
@Override
public void content(Buffer ref) throws IOException
{
@ -483,11 +483,11 @@ public class WebSocketClient extends AggregateLifeCycle
_endp.close();
}
});
String path=_holder.getURI().getPath();
if (path==null || path.length()==0)
path="/";
String request=
"GET "+path+" HTTP/1.1\r\n"+
"Host: "+holder.getURI().getHost()+":"+_holder.getURI().getPort()+"\r\n"+
@ -496,10 +496,10 @@ public class WebSocketClient extends AggregateLifeCycle
"Sec-WebSocket-Key: "+_key+"\r\n"+
(_origin==null?"":"Origin: "+_origin+"\r\n")+
"Sec-WebSocket-Version: "+WebSocketConnectionD12.VERSION+"\r\n";
if (holder.getProtocol()!=null)
request+="Sec-WebSocket-Protocol: "+holder.getProtocol()+"\r\n";
if (holder.getCookies()!=null && holder.getCookies().size()>0)
{
for (String cookie : holder.getCookies().keySet())
@ -510,9 +510,9 @@ public class WebSocketClient extends AggregateLifeCycle
}
request+="\r\n";
// TODO extensions
try
{
Buffer handshake = new ByteArrayBuffer(request,false);
@ -524,7 +524,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
holder.handshakeFailed(e);
}
}
public Connection handle() throws IOException
@ -539,7 +539,7 @@ public class WebSocketClient extends AggregateLifeCycle
case 0:
return this;
default:
break;
break;
}
}
if (_error==null)
@ -548,10 +548,10 @@ public class WebSocketClient extends AggregateLifeCycle
_error="No Sec-WebSocket-Accept";
else if (!WebSocketConnectionD12.hashKey(_key).equals(_accept))
_error="Bad Sec-WebSocket-Accept";
else
else
{
Buffer header=_parser.getHeaderBuffer();
MaskGen maskGen=_nullMask?new WebSocketGeneratorD12.FixedMaskGen():new WebSocketGeneratorD12.RandomMaskGen();
MaskGen maskGen=_maskingEnabled?new WebSocketGeneratorD12.RandomMaskGen():new WebSocketGeneratorD12.FixedMaskGen();
WebSocketConnectionD12 connection = new WebSocketConnectionD12(_holder.getWebSocket(),_endp,_buffers,System.currentTimeMillis(),_holder.getMaxIdleTime(),_holder.getProtocol(),null,10,maskGen);
if (header.hasContent())
@ -559,7 +559,7 @@ public class WebSocketClient extends AggregateLifeCycle
_buffers.returnBuffer(header);
_holder.onConnection(connection);
return connection;
}
}
@ -587,7 +587,7 @@ public class WebSocketClient extends AggregateLifeCycle
}
}
/* ------------------------------------------------------------ */
/** The Future Websocket Connection.
*/
@ -604,7 +604,7 @@ public class WebSocketClient extends AggregateLifeCycle
ByteChannel _channel;
WebSocketConnection _connection;
Throwable _exception;
public WebSocketFuture(WebSocket websocket, URI uri, String protocol, int maxIdleTime, Map<String,String> cookies,List<String> extensions, ByteChannel channel)
{
_websocket=websocket;
@ -615,9 +615,9 @@ public class WebSocketClient extends AggregateLifeCycle
_extensions=extensions;
_channel=channel;
}
public void onConnection(WebSocketConnection connection)
{
{
try
{
synchronized (this)
@ -633,7 +633,7 @@ public class WebSocketClient extends AggregateLifeCycle
_websocket.onOpen(connection.getConnection());
}
}
}
finally
{
@ -642,7 +642,7 @@ public class WebSocketClient extends AggregateLifeCycle
}
public void handshakeFailed(Throwable ex)
{
{
try
{
ByteChannel channel=null;
@ -684,17 +684,17 @@ public class WebSocketClient extends AggregateLifeCycle
{
return _websocket;
}
public URI getURI()
{
return _uri;
}
public int getMaxIdleTime()
{
return _maxIdleTime;
}
public String toString()
{
return "[" + _uri + ","+_websocket+"]@"+hashCode();
@ -774,7 +774,7 @@ public class WebSocketClient extends AggregateLifeCycle
else
connection=_connection.getConnection();
}
if (channel!=null)
closeChannel(channel,WebSocketConnectionD12.CLOSE_NOCLOSE,"timeout");
if (exception!=null)
@ -794,7 +794,7 @@ public class WebSocketClient extends AggregateLifeCycle
{
__log.warn(e);
}
try
{
channel.close();
@ -805,5 +805,5 @@ public class WebSocketClient extends AggregateLifeCycle
}
}
}
}