websocket debugging

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2887 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2011-03-14 12:06:28 +00:00
parent 7cca101aeb
commit 80a56c31a2
4 changed files with 59 additions and 42 deletions

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
/** /**
@ -587,28 +588,16 @@ public abstract class AbstractBuffer implements Buffer
{ {
for (int i = markIndex(); i < getIndex(); i++) for (int i = markIndex(); i < getIndex(); i++)
{ {
char c = (char) peek(i); byte b = peek(i);
if (Character.isISOControl(c)) TypeUtil.toHex(b,buf);
{
buf.append(c < 16 ? "\\0" : "\\");
buf.append(Integer.toString(c, 16));
}
else
buf.append(c);
} }
buf.append("}{"); buf.append("}{");
} }
int count = 0; int count = 0;
for (int i = getIndex(); i < putIndex(); i++) for (int i = getIndex(); i < putIndex(); i++)
{ {
char c = (char) peek(i); byte b = peek(i);
if (Character.isISOControl(c)) TypeUtil.toHex(b,buf);
{
buf.append(c < 16 ? "\\0" : "\\");
buf.append(Integer.toString(c, 16));
}
else
buf.append(c);
if (count++ == 50) if (count++ == 50)
{ {
if (putIndex() - i > 20) if (putIndex() - i > 20)

View File

@ -25,6 +25,7 @@ import org.eclipse.jetty.util.log.Log;
public class TestClient public class TestClient
{ {
private final static Random __random = new SecureRandom(); private final static Random __random = new SecureRandom();
private static boolean _verbose=false;
private final String _host; private final String _host;
private final int _port; private final int _port;
private final String _protocol; private final String _protocol;
@ -46,6 +47,7 @@ public class TestClient
private BlockingQueue<Long> _starts = new LinkedBlockingQueue<Long>(); private BlockingQueue<Long> _starts = new LinkedBlockingQueue<Long>();
int _messageBytes; int _messageBytes;
int _frames; int _frames;
byte _opcode=-1;
private final WebSocketParser.FrameHandler _handler = new WebSocketParser.FrameHandler() private final WebSocketParser.FrameHandler _handler = new WebSocketParser.FrameHandler()
{ {
public synchronized void onFrame(byte flags, byte opcode, Buffer buffer) public synchronized void onFrame(byte flags, byte opcode, Buffer buffer)
@ -67,6 +69,10 @@ public class TestClient
_messageBytes+=buffer.length(); _messageBytes+=buffer.length();
if (_opcode==-1)
_opcode=opcode;
if (WebSocketConnectionD06.isLastFrame(flags)) if (WebSocketConnectionD06.isLastFrame(flags))
{ {
_messagesReceived++; _messagesReceived++;
@ -79,10 +85,12 @@ public class TestClient
if (duration<_minDuration) if (duration<_minDuration)
_minDuration=duration; _minDuration=duration;
_totalTime+=duration; _totalTime+=duration;
System.out.printf("%d bytes from %s: frames=%d req=%d time=%.1fms opcode=0x%s\n",_messageBytes,_host,_frames,_messagesReceived,((double)duration/1000000.0),TypeUtil.toHexString(opcode)); System.out.printf("%d bytes from %s: frames=%d req=%d time=%.1fms opcode=0x%s\n",_messageBytes,_host,_frames,_messagesReceived,((double)duration/1000000.0),TypeUtil.toHexString(_opcode));
_frames=0; _frames=0;
_messageBytes=0; _messageBytes=0;
_opcode=-1;
} }
} }
catch(Exception e) catch(Exception e)
{ {
@ -108,7 +116,7 @@ public class TestClient
_input = new BufferedReader(new InputStreamReader(_socket.getInputStream(), "ISO-8859-1")); _input = new BufferedReader(new InputStreamReader(_socket.getInputStream(), "ISO-8859-1"));
_endp=new SocketEndPoint(_socket); _endp=new SocketEndPoint(_socket);
_generator = new WebSocketGeneratorD06(new WebSocketBuffers(32*1024),_endp,new WebSocketGeneratorD06.FixedMaskGen()); _generator = new WebSocketGeneratorD06(new WebSocketBuffers(32*1024),_endp,new WebSocketGeneratorD06.FixedMaskGen(new byte[4]));
_parser = new WebSocketParserD06(new WebSocketBuffers(32*1024),_endp,_handler,false); _parser = new WebSocketParserD06(new WebSocketBuffers(32*1024),_endp,_handler,false);
} }
@ -172,20 +180,22 @@ public class TestClient
public void run() public void run()
{ {
while (_endp.isOpen()) while (_endp.isOpen())
{
_parser.parseNext(); _parser.parseNext();
}
} }
}.start(); }.start();
} }
public void ping(int count,byte opcode,int fragment) public void ping(int count,byte opcode,int fragment)
{ {
_start=System.currentTimeMillis(); try
for (int i=0;i<count && !_socket.isClosed();i++)
{ {
if (_socket.isClosed()) _start=System.currentTimeMillis();
break; for (int i=0;i<count && !_socket.isClosed();i++)
try
{ {
if (_socket.isClosed())
break;
byte data[]=null; byte data[]=null;
if (opcode==WebSocketConnectionD06.OP_TEXT) if (opcode==WebSocketConnectionD06.OP_TEXT)
@ -210,28 +220,40 @@ public class TestClient
while(off<data.length) while(off<data.length)
{ {
_framesSent++; _framesSent++;
_generator.addFrame((byte)(off+len==data.length?0x8:0),(byte)(off==0?opcode:WebSocketConnectionD06.OP_CONTINUATION),data,off,len,_socket.getSoTimeout()); byte flags= (byte)(off+len==data.length?0x8:0);
byte op=(byte)(off==0?opcode:WebSocketConnectionD06.OP_CONTINUATION);
if (_verbose)
System.err.printf("%s#addFrame %s|%s %s\n",this.getClass().getSimpleName(),TypeUtil.toHexString(flags),TypeUtil.toHexString(op),TypeUtil.toHexString(data,off,len));
_generator.addFrame(flags,op,data,off,len,_socket.getSoTimeout());
off+=len; off+=len;
if(data.length-off>len) if(data.length-off>len)
len=data.length-off; len=data.length-off;
if (fragment>0&& len>fragment) if (fragment>0&& len>fragment)
len=fragment; len=fragment;
} }
_generator.flush(_socket.getSoTimeout()); _generator.flush(_socket.getSoTimeout());
Thread.sleep(1000); Thread.sleep(1000);
}
catch (Exception x)
{
throw new RuntimeException(x);
} }
} }
catch (Exception x)
{
throw new RuntimeException(x);
}
} }
public void dump() throws Exception
public void dump() throws IOException
{ {
for (int i=0;i<25;i++)
{
if (_messagesSent==_messagesReceived)
break;
Thread.sleep(100);
}
_socket.close(); _socket.close();
long duration=System.currentTimeMillis()-_start; long duration=System.currentTimeMillis()-_start;
System.out.println("--- "+_host+" websocket ping statistics using 1 connection ---"); System.out.println("--- "+_host+" websocket ping statistics using 1 connection ---");
@ -263,7 +285,6 @@ public class TestClient
{ {
String host="localhost"; String host="localhost";
int port=8080; int port=8080;
boolean verbose=false;
String protocol=null; String protocol=null;
int count=10; int count=10;
int size=64; int size=64;
@ -286,7 +307,7 @@ public class TestClient
else if ("-P".equals(a)||"--protocol".equals(a)) else if ("-P".equals(a)||"--protocol".equals(a))
protocol=args[++i]; protocol=args[++i];
else if ("-v".equals(a)||"--verbose".equals(a)) else if ("-v".equals(a)||"--verbose".equals(a))
verbose=true; _verbose=true;
else if ("-b".equals(a)||"--binary".equals(a)) else if ("-b".equals(a)||"--binary".equals(a))
binary=true; binary=true;
else if (a.startsWith("-")) else if (a.startsWith("-"))
@ -300,7 +321,7 @@ public class TestClient
try try
{ {
client.open(); client.open();
if (protocol.startsWith("echo")) if (protocol!=null && protocol.startsWith("echo"))
client.ping(count,binary?WebSocketConnectionD06.OP_BINARY:WebSocketConnectionD06.OP_TEXT,fragment); client.ping(count,binary?WebSocketConnectionD06.OP_BINARY:WebSocketConnectionD06.OP_TEXT,fragment);
else else
client.ping(count,WebSocketConnectionD06.OP_PING,-1); client.ping(count,WebSocketConnectionD06.OP_PING,-1);

View File

@ -241,7 +241,6 @@ public class TestServer extends Server
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
class TestEchoFragmentWebSocket extends TestWebSocket class TestEchoFragmentWebSocket extends TestWebSocket
{ {
@Override @Override
public void onConnect(Connection connection) public void onConnect(Connection connection)
{ {

View File

@ -247,13 +247,21 @@ public class WebSocketGeneratorD06 implements WebSocketGenerator
private synchronized int flushBuffer() throws IOException private synchronized int flushBuffer() throws IOException
{ {
if (!_endp.isOpen()) try
throw new EofException(); {
if (!_endp.isOpen())
throw new EofException();
if (_buffer!=null) if (_buffer!=null)
return _endp.flush(_buffer); return _endp.flush(_buffer);
return 0; return 0;
}
catch(IOException e)
{
System.err.println("FAILED to flush: "+_buffer.toDetailString());
throw e;
}
} }
private synchronized int expelBuffer(long blockFor) throws IOException private synchronized int expelBuffer(long blockFor) throws IOException