diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/CloseFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/CloseFrame.java index 689afd508ee..7416b70ba53 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/CloseFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/CloseFrame.java @@ -1,6 +1,26 @@ package org.eclipse.jetty.websocket.frames; -public class CloseFrame extends ControlFrame -{ +import org.eclipse.jetty.websocket.frames.ControlFrameType; +public class CloseFrame extends ControlFrame +{ + private final int pingId; + + public CloseFrame(short version, int pingId) + { + super(version, ControlFrameType.CLOSE_FRAME, (byte)0); + this.pingId = pingId; + } + + public int getPingId() + { + return pingId; + } + + @Override + public String toString() + { + return String.format("%s ping=%d", super.toString(), getPingId()); + } + } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/ControlFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/ControlFrame.java index 3369b8e21e7..30c37883507 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/ControlFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/ControlFrame.java @@ -1,6 +1,34 @@ package org.eclipse.jetty.websocket.frames; +import org.eclipse.jetty.websocket.frames.ControlFrameType; + public class ControlFrame extends BaseFrame { + private final short _version; + private final ControlFrameType _type; + private final byte _flags; // check if needed + + public ControlFrame( short version, ControlFrameType type, byte flags ) + { + _version = version; + _type = type; + _flags = flags; + } + + public short getVersion() + { + return _version; + } + + public ControlFrameType getType() + { + return _type; + } + + @Override + public String toString() + { + return String.format("%s frame v%s", getType(), getVersion()); + } } 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 33c7d71a235..a802e23ef43 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,6 +1,25 @@ package org.eclipse.jetty.websocket.frames; +import org.eclipse.jetty.websocket.frames.ControlFrameType; + public class PingFrame extends ControlFrame { + private final int pingId; + public PingFrame(short version, int pingId) + { + super(version, ControlFrameType.PING_FRAME, (byte)0); + this.pingId = pingId; + } + + public int getPingId() + { + return pingId; + } + + @Override + public String toString() + { + return String.format("%s ping=%d", super.toString(), getPingId()); + } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java index f0b47857c38..63d41d8f66d 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/frames/PongFrame.java @@ -1,6 +1,25 @@ package org.eclipse.jetty.websocket.frames; +import org.eclipse.jetty.websocket.frames.ControlFrameType; + public class PongFrame extends ControlFrame { + private final int pingId; + public PongFrame(short version, int pingId) + { + super(version, ControlFrameType.PONG_FRAME, (byte)0); + this.pingId = pingId; + } + + public int getPingId() + { + return pingId; + } + + @Override + public String toString() + { + return String.format("%s ping=%d", super.toString(), getPingId()); + } } 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 ebf98e92ad3..614cc0977df 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 @@ -1,5 +1,11 @@ package org.eclipse.jetty.websocket.generator; -public class ControlFrameGenerator { +import java.nio.ByteBuffer; +import org.eclipse.jetty.websocket.frames.ControlFrame; + +public abstract class ControlFrameGenerator +{ + + public abstract ByteBuffer generate(ControlFrame frame); } 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 e97e9680ba9..445238f728e 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 @@ -1,10 +1,13 @@ package org.eclipse.jetty.websocket.generator; +import java.nio.ByteBuffer; import java.util.EnumMap; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.websocket.frames.BaseFrame; +import org.eclipse.jetty.websocket.frames.ControlFrame; import org.eclipse.jetty.websocket.frames.ControlFrameType; +import org.eclipse.jetty.websocket.frames.PingFrame; import org.eclipse.jetty.websocket.generator.ControlFrameGenerator; /** @@ -33,12 +36,24 @@ import org.eclipse.jetty.websocket.generator.ControlFrameGenerator; */ public class Generator { - private final EnumMap generators = new EnumMap<>(ControlFrameType.class); + private final EnumMap generators = new EnumMap<>(ControlFrameType.class); public Generator(ByteBufferPool bufferPool) //, CompressionFactory.Compressor compressor) { HeadersBlockGenerator headerBlockGenerator = new HeadersBlockGenerator(); - generators.put(ControlFrameType.BASE_FRAME, new BaseFrame()); + generators.put(ControlFrameType.PING_FRAME, new PingFrameGenerator()); + generators.put(ControlFrameType.PONG_FRAME, new PongFrameGenerator()); + generators.put(ControlFrameType.CLOSE_FRAME, new CloseFrameGenerator()); + } + + + public ByteBuffer control(ControlFrame frame) + { + ControlFrameGenerator generator = generators.get(frame.getType()); + return generator.generate(frame); + } + + }