ping and close generators akin what we are looking at (atm at least)

This commit is contained in:
Jesse McConnell 2012-06-18 16:06:46 -05:00
parent b9ca387d53
commit 2562dc838b
6 changed files with 101 additions and 23 deletions

View File

@ -1,26 +1,43 @@
package org.eclipse.jetty.websocket.frames;
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(ControlFrameType.PING_FRAME);
this.pingId = pingId;
setPayload(payload);
}
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.frames.BaseFrame;
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(BaseFrame.OP_CLOSE);
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

@ -37,23 +37,20 @@ import org.eclipse.jetty.websocket.generator.ControlFrameGenerator;
public class Generator {
private final EnumMap<ControlFrameType, ControlFrameGenerator> generators = new EnumMap<>(ControlFrameType.class);
public Generator(ByteBufferPool bufferPool) //, CompressionFactory.Compressor compressor)
{
HeadersBlockGenerator headerBlockGenerator = new HeadersBlockGenerator();
generators.put(ControlFrameType.PING_FRAME, new PingFrameGenerator());
generators.put(ControlFrameType.PONG_FRAME, new PongFrameGenerator());
generators.put(ControlFrameType.CLOSE_FRAME, new CloseFrameGenerator());
generators.put(ControlFrameType.PING_FRAME, new PingFrameGenerator(bufferPool));
generators.put(ControlFrameType.PONG_FRAME, new PongFrameGenerator(bufferPool));
generators.put(ControlFrameType.CLOSE_FRAME, new CloseFrameGenerator(bufferPool));
}
public ByteBuffer control(ControlFrame frame)
{
ControlFrameGenerator generator = generators.get(frame.getType());
return generator.generate(frame);
}
}

View File

@ -2,16 +2,32 @@ package org.eclipse.jetty.websocket.generator;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.websocket.WebSocketConnectionRFC6455;
import org.eclipse.jetty.websocket.frames.BaseFrame;
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(BaseFrame.OP_PING);
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)
{