Fixed tests.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1437 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Simone Bordet 2010-04-01 21:24:25 +00:00
parent 6fdd5c9677
commit 529e04295f
3 changed files with 38 additions and 75 deletions

View File

@ -39,7 +39,7 @@ public class WebSocketGenerator
if (_buffer.space() == 0)
expelBuffer(blockFor);
if ((frame & WebSocket.LENGTH_FRAME) == WebSocket.LENGTH_FRAME)
if (isLengthFrame(frame))
{
// Send a length delimited frame
@ -77,7 +77,7 @@ public class WebSocketGenerator
remaining -= chunk;
if (_buffer.space() > 0)
{
if (frame == WebSocket.SENTINEL_FRAME)
if (!isLengthFrame(frame))
_buffer.put((byte)0xFF);
// Gently flush the data, issuing a non-blocking write
flushBuffer();
@ -88,7 +88,7 @@ public class WebSocketGenerator
expelBuffer(blockFor);
if (remaining == 0)
{
if (frame == WebSocket.SENTINEL_FRAME)
if (!isLengthFrame(frame))
_buffer.put((byte)0xFF);
// Gently flush the data, issuing a non-blocking write
flushBuffer();
@ -97,57 +97,20 @@ public class WebSocketGenerator
}
}
private boolean isLengthFrame(byte frame)
{
return (frame & WebSocket.LENGTH_FRAME) == WebSocket.LENGTH_FRAME;
}
public synchronized void addFrame(byte frame, String content, int blockFor) throws IOException
{
byte[] bytes = content.getBytes("UTF-8");
addFrame(frame, bytes, 0, bytes.length, blockFor);
}
private synchronized void checkSpace(int needed, long blockFor)
throws IOException
public synchronized int flush(long blockFor) throws IOException
{
int space=_buffer.space();
if (space<needed)
{
if (_endp.isBlocking())
{
try
{
flushBuffer();
_buffer.compact();
space=_buffer.space();
}
catch(IOException e)
{
throw e;
}
}
else
{
flushBuffer();
_buffer.compact();
space=_buffer.space();
if (space<needed && _buffer.length()>0 && _endp.blockWritable(blockFor))
{
flushBuffer();
_buffer.compact();
space=_buffer.space();
}
}
if (space<needed)
{
_endp.close();
throw new IOException("Full Timeout");
}
}
}
public synchronized int flush(long blockFor)
{
return 0;
return expelBuffer(blockFor);
}
public synchronized int flush() throws IOException
@ -176,9 +139,9 @@ public class WebSocketGenerator
return 0;
}
private synchronized void expelBuffer(long blockFor) throws IOException
private synchronized int expelBuffer(long blockFor) throws IOException
{
flushBuffer();
int result = flushBuffer();
_buffer.compact();
if (!_endp.isBlocking())
{
@ -190,10 +153,11 @@ public class WebSocketGenerator
if (!ready)
throw new IOException("Write timeout");
flushBuffer();
result += flushBuffer();
_buffer.compact();
}
}
return result;
}
public synchronized boolean isBufferEmpty()

View File

@ -12,9 +12,9 @@ import org.eclipse.jetty.util.log.Log;
/* ------------------------------------------------------------ */
/**
/**
* Parser the WebSocket protocol.
*
*
*/
public class WebSocketParser
{
@ -35,7 +35,7 @@ public class WebSocketParser
/* ------------------------------------------------------------ */
/**
* @param buffers The buffers to use for parsing. Only the {@link Buffers#getBuffer()} is used.
* This should be a direct buffer if binary data is mostly used or an indirect buffer if utf-8 data
* This should be a direct buffer if binary data is mostly used or an indirect buffer if utf-8 data
* is mostly used.
* @param endp
* @param handler
@ -58,10 +58,10 @@ public class WebSocketParser
{
return _buffer;
}
/* ------------------------------------------------------------ */
/** Parse to next event.
* Parse to the next {@link EventHandler} event or until no more data is
* Parse to the next {@link EventHandler} event or until no more data is
* available. Fill data from the {@link EndPoint} only as necessary.
* @return An indication of progress or otherwise. -1 indicates EOF, 0 indicates
* that no bytes were read and no messages parsed. A positive number indicates either
@ -71,7 +71,7 @@ public class WebSocketParser
{
if (_buffer==null)
_buffer=_buffers.getBuffer();
int total_filled=0;
// Loop until an datagram call back or can't fill anymore
@ -85,11 +85,11 @@ public class WebSocketParser
{
// compact to mark (set at start of data)
_buffer.compact();
// if no space, then the data is too big for buffer
if (_buffer.space() == 0)
throw new IllegalStateException("FULL");
if (_buffer.space() == 0)
throw new IllegalStateException("FULL");
// catch IOExceptions (probably EOF) and try to parse what we have
try
{
@ -167,14 +167,14 @@ public class WebSocketParser
_buffer.skip(_length);
_state=STATE_START;
_handler.onFrame(_frame,data);
if (_buffer.length()==0)
{
_buffers.returnBuffer(_buffer);
_buffer=null;
}
return 1; // the number of messages handled.
return total_filled;
}
}
}
@ -190,10 +190,10 @@ public class WebSocketParser
_buffer.put(buffer);
buffer.clear();
}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */

View File

@ -1,7 +1,6 @@
package org.eclipse.jetty.websocket;
import junit.framework.TestCase;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.ByteArrayEndPoint;
import org.eclipse.jetty.util.StringUtil;
@ -12,7 +11,7 @@ import org.eclipse.jetty.util.StringUtil;
*/
public class WebSocketGeneratorTest extends TestCase
{
WebSocketBuffers _buffers;
ByteArrayBuffer _out;
ByteArrayEndPoint _endp;
@ -28,7 +27,7 @@ public class WebSocketGeneratorTest extends TestCase
_out = new ByteArrayBuffer(2048);
_endp.setOut(_out);
}
/* ------------------------------------------------------------ */
public void testOneString() throws Exception
{
@ -52,13 +51,13 @@ public class WebSocketGeneratorTest extends TestCase
assertEquals('d',_out.get());
assertEquals(0xff,0xff&_out.get());
}
public void testOneBuffer() throws Exception
{
_generator.addFrame((byte)0x84,"Hell\uFF4F W\uFF4Frld".getBytes(StringUtil.__UTF8),0);
_generator.flush();
assertEquals(0x84,0xff&_out.get());
assertEquals(15,0xff&_out.get());
assertEquals(0x80|15,0xff&_out.get());
assertEquals('H',_out.get());
assertEquals('e',_out.get());
assertEquals('l',_out.get());
@ -75,19 +74,19 @@ public class WebSocketGeneratorTest extends TestCase
assertEquals('l',_out.get());
assertEquals('d',_out.get());
}
public void testOneLongBuffer() throws Exception
{
byte[] b=new byte[150];
for (int i=0;i<b.length;i++)
b[i]=(byte)('0'+(i%10));
_generator.addFrame((byte)0x85,b,0);
_generator.flush();
assertEquals(0x85,0xff&_out.get());
assertEquals(0x80|(b.length>>7),0xff&_out.get());
assertEquals(0x7f&b.length,0xff&_out.get());
assertEquals(0x80|(0x7f&b.length),0xff&_out.get());
for (int i=0;i<b.length;i++)
assertEquals('0'+(i%10),0xff&_out.get());
}