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

View File

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

View File

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