Fixing PingPayloadParser with new FrameParser.copyBuffer() method

This commit is contained in:
Joakim Erdfelt 2012-06-19 13:07:57 -07:00
parent 17a7b12df1
commit 6bbeba9f59
5 changed files with 29 additions and 10 deletions

View File

@ -34,9 +34,22 @@ public class PingFrame extends ControlFrame
return OpCode.PING; return OpCode.PING;
} }
public String getPayloadAsString()
{
if (hasPayload())
{
ByteBuffer payload = getPayload();
return payload.asCharBuffer().toString();
}
else
{
return "<n/a>";
}
}
@Override @Override
public String toString() public String toString()
{ {
return String.format("%s ping, payload[has=%b, string=%s]",super.toString(),hasPayload(),getPayload().toString()); return String.format("%s ping, payload[has=%b, string=%s]",super.toString(),hasPayload(),getPayloadAsString());
} }
} }

View File

@ -30,7 +30,9 @@ public class PongFrame extends ControlFrame
/** /**
* Construct appropriate PongFrame from PingFrame * Construct appropriate PongFrame from PingFrame
* @param ping the ping frame to base pong from *
* @param ping
* the ping frame to base pong from
*/ */
public PongFrame(PingFrame ping) public PongFrame(PingFrame ping)
{ {

View File

@ -54,6 +54,15 @@ public abstract class FrameParser<T extends BaseFrame>
this.settings = settings; this.settings = settings;
} }
protected int copyBuffer(ByteBuffer src, ByteBuffer dest, int length)
{
int amt = Math.min(length,src.remaining());
byte b[] = new byte[amt];
src.get(b,0,amt);
dest.put(b,0,amt);
return amt;
}
/** /**
* The frame that is being parsed * The frame that is being parsed
* *

View File

@ -38,15 +38,10 @@ public class PingPayloadParser extends FrameParser<PingFrame>
payload = ByteBuffer.allocate(payloadLength); payload = ByteBuffer.allocate(payloadLength);
} }
int size = Math.min(payloadLength,buffer.remaining()); copyBuffer(buffer,payload,payloadLength);
int limit = buffer.limit();
buffer.limit(buffer.position() + size);
ByteBuffer bytes = buffer.slice(); // TODO: make sure reference to subsection is acceptable
buffer.limit(limit);
payload.put(bytes);
if (payload.position() >= payloadLength) if (payload.position() >= payloadLength)
{ {
frame.setPayload(bytes); frame.setPayload(payload);
return true; return true;
} }
} }

View File

@ -39,7 +39,7 @@ public class FrameParseCapture implements Parser.Listener
{ {
int count = 0; int count = 0;
for(BaseFrame frame: frames) { for(BaseFrame frame: frames) {
if (frame.getClass().isInstance(frameType)) if (frameType.isInstance(frame))
{ {
count++; count++;
} }