package org.glassfish.grizzly.http2.frames;
import java.util.HashMap;
import java.util.Map;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.ThreadCache;
import org.glassfish.grizzly.memory.MemoryManager;
public class PingFrame extends Http2Frame {
private static final ThreadCache.CachedTypeIndex<PingFrame> CACHE_IDX =
ThreadCache.obtainIndex(PingFrame.class, 8);
public static final int TYPE = 6;
public static final byte ACK_FLAG = 0x1;
static final Map<Integer, String> FLAG_NAMES_MAP =
new HashMap<>(2);
static {
FLAG_NAMES_MAP.put((int) ACK_FLAG, "ACK");
}
private long opaqueData;
private PingFrame() { }
static PingFrame create() {
PingFrame frame = ThreadCache.takeFromCache(CACHE_IDX);
if (frame == null) {
frame = new PingFrame();
}
return frame;
}
public static Http2Frame fromBuffer(final int flags, final int streamId, final Buffer frameBuffer) {
PingFrame frame = create();
frame.setFlags(flags);
frame.setStreamId(streamId);
frame.setFrameBuffer(frameBuffer);
if (frameBuffer.remaining() != 8) {
frame.length = frameBuffer.remaining();
} else {
frame.opaqueData = frameBuffer.getLong();
}
return frame;
}
public static PingFrameBuilder builder() {
return new PingFrameBuilder();
}
public long getOpaqueData() {
return opaqueData;
}
public boolean isAckSet() {
return isFlagSet(ACK_FLAG);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("PingFrame {")
.append(headerToString())
.append(", opaqueData=").append(opaqueData)
.append('}');
return sb.toString();
}
@Override
protected int calcLength() {
return 8;
}
@Override
protected Map<Integer, String> getFlagNamesMap() {
return FLAG_NAMES_MAP;
}
@Override
public void recycle() {
if (DONT_RECYCLE) {
return;
}
opaqueData = 0;
super.recycle();
ThreadCache.putToCache(CACHE_IDX, this);
}
@Override
public int getType() {
return TYPE;
}
@Override
public Buffer toBuffer(final MemoryManager memoryManager) {
final Buffer buffer = memoryManager.allocate(FRAME_HEADER_SIZE + 8);
serializeFrameHeader(buffer);
buffer.putLong(opaqueData);
buffer.trim();
return buffer;
}
public static class PingFrameBuilder extends Http2FrameBuilder<PingFrameBuilder> {
private long opaqueData;
protected PingFrameBuilder() {
}
public PingFrameBuilder opaqueData(final long opaqueData) {
this.opaqueData = opaqueData;
return this;
}
public PingFrameBuilder ack(final boolean isAck) {
if (isAck) {
setFlag(ACK_FLAG);
}
return this;
}
public PingFrame build() {
final PingFrame frame = PingFrame.create();
setHeaderValuesTo(frame);
frame.opaqueData = opaqueData;
return frame;
}
@Override
protected PingFrameBuilder getThis() {
return this;
}
}
}