From 2562dc838b5600277fcc64b9ccecbebfa2744974 Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Mon, 18 Jun 2012 16:06:46 -0500 Subject: [PATCH] ping and close generators akin what we are looking at (atm at least) --- .../jetty/websocket/frames/PingFrame.java | 35 ++++++++++++++----- .../generator/CloseFrameGenerator.java | 20 +++++++++-- .../generator/ControlFrameGenerator.java | 31 +++++++++++++++- .../jetty/websocket/generator/Generator.java | 9 ++--- .../generator/PingFrameGenerator.java | 22 ++++++++++-- .../generator/PongFrameGenerator.java | 7 +++- 6 files changed, 101 insertions(+), 23 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java index a3cc8a767a9..5c7a93f1742 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PingFrame.java @@ -1,26 +1,43 @@ package org.eclipse.jetty.websocket.frames; +import java.nio.ByteBuffer; + +import org.eclipse.jetty.websocket.api.WebSocketException; + /** * Representation of a Ping Frame (0x09). */ 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()); } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java index 208abd30e51..5a8b62065ef 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java @@ -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; } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/ControlFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/ControlFrameGenerator.java index 614cc0977df..e755e6ec936 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/ControlFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/ControlFrameGenerator.java @@ -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; + } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java index 445238f728e..83a2e92575c 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java @@ -37,23 +37,20 @@ import org.eclipse.jetty.websocket.generator.ControlFrameGenerator; public class Generator { private final EnumMap 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); } - } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java index 8163cef126a..443973dbc88 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java @@ -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; } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java index a2bff039d03..c38a91902c8 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java @@ -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) {