Merge fixes

This commit is contained in:
Joakim Erdfelt 2012-06-18 14:27:24 -07:00
commit a4a5108da4
6 changed files with 98 additions and 19 deletions

View File

@ -1,18 +1,21 @@
package org.eclipse.jetty.websocket.frames;
import org.eclipse.jetty.websocket.api.OpCode;
import java.nio.ByteBuffer;
import org.eclipse.jetty.websocket.api.WebSocketException;
/**
* Representation of a <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">Ping Frame (0x09)</a>.
*/
public class PingFrame extends ControlFrame
{
private final int pingId;
public PingFrame(int pingId)
{
private ByteBuffer payload;
public PingFrame(ByteBuffer payload)
{
super(OpCode.PING);
this.pingId = pingId;
setPayload(payload);
}
@Override
@ -21,14 +24,27 @@ public class PingFrame extends ControlFrame
return OpCode.PING;
}
public int getPingId()
public void setPayload(ByteBuffer payload)
{
return pingId;
if ( payload.array().length >= 126 )
{
this.payload = payload;
setPayloadLength(payload.array().length);
}
else
{
throw new WebSocketException("too long, catch this better");
}
}
public ByteBuffer getPayload()
{
return payload;
}
@Override
public String toString()
{
return String.format("%s ping=%d", super.toString(), getPingId());
return String.format("%s ping", super.toString());
}
}

View File

@ -2,16 +2,30 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.api.OpCode;
import org.eclipse.jetty.websocket.frames.CloseFrame;
import org.eclipse.jetty.websocket.frames.ControlFrame;
public class CloseFrameGenerator extends ControlFrameGenerator
{
public CloseFrameGenerator(ByteBufferPool bufferPool)
{
super(bufferPool);
}
@Override
public ByteBuffer generate(ControlFrame frame)
{
// TODO Auto-generated method stub
return null;
CloseFrame close = (CloseFrame)frame;
ByteBuffer buffer = super.generate(frame);
buffer.putInt(OpCode.CLOSE.getCode());
buffer.put(frame.getMask());
buffer.putLong(frame.getPayloadLength());
buffer.put(close.getReason().getBytes());
return buffer;
}
}

View File

@ -2,10 +2,39 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.frames.BaseFrame;
import org.eclipse.jetty.websocket.frames.ControlFrame;
public abstract class ControlFrameGenerator
{
private final ByteBufferPool bufferPool;
protected ControlFrameGenerator(ByteBufferPool bufferPool)
{
this.bufferPool = bufferPool;
}
protected ByteBufferPool getByteBufferPool()
{
return bufferPool;
}
public abstract ByteBuffer generate(ControlFrame frame);
public ByteBuffer generate(ControlFrame frame)
{
// how to calculate the size since control frames may hold
// application data
// grabing 125 now since that is _max_ possible
ByteBuffer buffer = getByteBufferPool().acquire(125,true);
// all control frames are FIN as they can not be fragmented
buffer.putInt(BaseFrame.FLAG_FIN);
// revisit this on extensions since they can negotiate this
buffer.putInt(0);
buffer.putInt(0);
buffer.putInt(0);
return buffer;
}
}

View File

@ -39,9 +39,9 @@ public class Generator {
public Generator(ByteBufferPool bufferPool) //, CompressionFactory.Compressor compressor)
{
HeadersBlockGenerator headerBlockGenerator = new HeadersBlockGenerator();
generators.put(OpCode.PING,new PingFrameGenerator());
generators.put(OpCode.PONG,new PongFrameGenerator());
generators.put(OpCode.CLOSE,new CloseFrameGenerator());
generators.put(OpCode.PING,new PingFrameGenerator(bufferPool));
generators.put(OpCode.PONG,new PongFrameGenerator(bufferPool));
generators.put(OpCode.CLOSE,new CloseFrameGenerator(bufferPool));
}

View File

@ -2,16 +2,31 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.api.OpCode;
import org.eclipse.jetty.websocket.frames.ControlFrame;
import org.eclipse.jetty.websocket.frames.PingFrame;
public class PingFrameGenerator extends ControlFrameGenerator
{
public PingFrameGenerator(ByteBufferPool bufferPool)
{
super(bufferPool);
}
@Override
public ByteBuffer generate(ControlFrame frame)
{
// TODO Auto-generated method stub
return null;
PingFrame ping = (PingFrame)frame;
ByteBuffer buffer = super.generate(frame);
buffer.putInt(OpCode.PING.getCode());
buffer.put(frame.getMask());
buffer.putLong(frame.getPayloadLength());
buffer.put(ping.getPayload().array());
return buffer;
}
}

View File

@ -2,11 +2,16 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.frames.ControlFrame;
public class PongFrameGenerator extends ControlFrameGenerator
{
public PongFrameGenerator(ByteBufferPool bufferPool)
{
super(bufferPool);
}
@Override
public ByteBuffer generate(ControlFrame frame)
{