Skip to content

Commit

Permalink
First round of cleanup codec-http2
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Maurer committed Apr 5, 2014
1 parent b92b65e commit 22c1327
Show file tree
Hide file tree
Showing 37 changed files with 97 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public enum Http2Error {

private final int code;

private Http2Error(int code) {
Http2Error(int code) {
this.code = code;
}

public int getCode() {
return this.code;
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public class DefaultHttp2Connection implements Http2Connection {
private ChannelFutureListener closeListener;

public DefaultHttp2Connection(boolean server) {
this.localEndpoint = new DefaultEndpoint(server);
this.remoteEndpoint = new DefaultEndpoint(!server);
localEndpoint = new DefaultEndpoint(server);
remoteEndpoint = new DefaultEndpoint(!server);
}

@Override
Expand Down Expand Up @@ -158,18 +158,6 @@ public void operationComplete(ChannelFuture future) throws Exception {
return closeListener;
}

private void notifyStreamClosed(int id) {
for (Listener listener : listeners) {
listener.streamClosed(id);
}
}

private void notifyStreamCreated(int id) {
for (Listener listener : listeners) {
listener.streamCreated(id);
}
}

/**
* Simple stream implementation. Streams can be compared to each other by priority.
*/
Expand All @@ -180,7 +168,7 @@ private class DefaultStream implements Http2Stream {

public DefaultStream(int id) {
this.id = id;
this.priority = DEFAULT_STREAM_PRIORITY;
priority = DEFAULT_STREAM_PRIORITY;
}

@Override
Expand Down Expand Up @@ -331,6 +319,12 @@ public boolean isLocalSideOpen() {
return false;
}
}

private void notifyStreamClosed(int id) {
for (Listener listener : listeners) {
listener.streamClosed(id);
}
}
}

/**
Expand Down Expand Up @@ -406,7 +400,7 @@ public DefaultStream reservePushStream(int streamId, Http2Stream parent) throws

@Override
public void setPushToAllowed(boolean allow) {
this.pushToAllowed = allow;
pushToAllowed = allow;
}

@Override
Expand Down Expand Up @@ -453,5 +447,11 @@ private void checkNewStreamAllowed(int streamId) throws Http2Exception {
private boolean isLocal() {
return this == localEndpoint;
}

private void notifyStreamCreated(int id) {
for (Listener listener : listeners) {
listener.streamCreated(id);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class DefaultInboundFlowController implements InboundFlowController {

private int initialWindowSize = DEFAULT_FLOW_CONTROL_WINDOW_SIZE;
private StreamWindow connectionWindow = new StreamWindow(CONNECTION_STREAM_ID);
private final StreamWindow connectionWindow = new StreamWindow(CONNECTION_STREAM_ID);
private final Map<Integer, StreamWindow> streamWindows = Maps.newHashMap();

public DefaultInboundFlowController(Http2Connection connection) {
Expand Down Expand Up @@ -134,7 +134,7 @@ private final class StreamWindow {

public StreamWindow(int streamId) {
this.streamId = streamId;
this.windowSize = initialWindowSize;
windowSize = initialWindowSize;
}

public int getSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void setInitialOutboundWindowSize(int newWindowSize) throws Http2Exceptio

@Override
public void updateOutboundWindowSize(int streamId, int delta) throws Http2Exception {
StreamState streamWindow = null;
StreamState streamWindow;
if (streamId == CONNECTION_STREAM_ID) {
// Update the connection window and write any pending frames for all streams.
addAndGetConnectionWindowSize(delta);
Expand Down Expand Up @@ -146,15 +146,15 @@ private void writeFrame(Http2DataFrame frame, StreamState state, FrameWriter fra
* bytes. The reader index on the input frame is then advanced by the number of bytes. The
* returned frame will not have end-of-stream set.
*/
private Http2DataFrame readPartialFrame(Http2DataFrame frame, int numBytes) {
private static Http2DataFrame readPartialFrame(Http2DataFrame frame, int numBytes) {
return new DefaultHttp2DataFrame.Builder().setStreamId(frame.getStreamId())
.setContent(frame.content().readSlice(numBytes).retain()).build();
}

/**
* Indicates whether applying the delta to the given value will cause an integer overflow.
*/
private boolean isIntegerOverflow(int previousValue, int delta) {
private static boolean isIntegerOverflow(int previousValue, int delta) {
return delta > 0 && (Integer.MAX_VALUE - delta) < previousValue;
}

Expand Down Expand Up @@ -263,7 +263,7 @@ public void writePendingFrames(int maxFrames) throws Http2Exception {
/**
* Pending write for a single data frame.
*/
private class PendingWrite {
private static class PendingWrite {
private final Http2DataFrame frame;
private final FrameWriter writer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
handleOutboundSettings(ctx, (Http2SettingsFrame) msg, promise);
} else {
ctx.write(msg, promise);
return;
}

} catch (Throwable e) {
Expand Down Expand Up @@ -328,7 +327,7 @@ private void handleInboundRstStream(ChannelHandlerContext ctx, Http2RstStreamFra
ctx.fireChannelRead(frame);
}

private void handleInboundPing(ChannelHandlerContext ctx, Http2PingFrame frame) {
private static void handleInboundPing(ChannelHandlerContext ctx, Http2PingFrame frame) {
if (frame.isAck()) {
// The remote enpoint is responding to an Ack that we sent.
ctx.fireChannelRead(frame);
Expand Down Expand Up @@ -510,13 +509,13 @@ private void handleOutboundPing(ChannelHandlerContext ctx, Http2PingFrame frame,
ctx.writeAndFlush(frame, promise);
}

private void handleOutboundGoAway() throws Http2Exception {
private static void handleOutboundGoAway() throws Http2Exception {
// Why is this being sent? Intercept it and fail the write.
// Should have sent a CLOSE ChannelStateEvent
throw format(PROTOCOL_ERROR, "Another handler attempted to send GoAway.");
}

private void handleOutboundWindowUpdate() throws Http2Exception {
private static void handleOutboundWindowUpdate() throws Http2Exception {
// Why is this being sent? Intercept it and fail the write.
throw format(PROTOCOL_ERROR, "Another handler attempted to send window update.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface Http2Stream extends Comparable<Http2Stream> {
* The allowed states of an HTTP2 stream.
*/
enum State {
IDLE, RESERVED_LOCAL, RESERVED_REMOTE, OPEN, HALF_CLOSED_LOCAL, HALF_CLOSED_REMOTE, CLOSED;
IDLE, RESERVED_LOCAL, RESERVED_REMOTE, OPEN, HALF_CLOSED_LOCAL, HALF_CLOSED_REMOTE, CLOSED
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public final class DefaultHttp2DataFrame extends DefaultByteBufHolder implements

private DefaultHttp2DataFrame(Builder builder) {
super(builder.content);
this.streamId = builder.streamId;
this.endOfStream = builder.endOfStream;
this.paddingLength = builder.paddingLength;
streamId = builder.streamId;
endOfStream = builder.endOfStream;
paddingLength = builder.paddingLength;
}

@Override
Expand Down Expand Up @@ -180,7 +180,7 @@ public DefaultHttp2DataFrame build() {
return new DefaultHttp2DataFrame(this);
}

private void verifyLength(int paddingLength, ByteBuf data) {
private static void verifyLength(int paddingLength, ByteBuf data) {
int maxLength = MAX_FRAME_PAYLOAD_LENGTH;
maxLength -= paddingLength;
if (data.readableBytes() > maxLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public final class DefaultHttp2GoAwayFrame extends DefaultByteBufHolder implemen

private DefaultHttp2GoAwayFrame(Builder builder) {
super(builder.debugData);
this.lastStreamId = builder.lastStreamId;
this.errorCode = builder.errorCode;
lastStreamId = builder.lastStreamId;
errorCode = builder.errorCode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public final class DefaultHttp2HeadersFrame implements Http2HeadersFrame {
private final Http2Headers headers;

private DefaultHttp2HeadersFrame(Builder builder) {
this.streamId = builder.streamId;
this.priority = builder.priority;
this.headers = builder.headersBuilder.build();
this.endOfStream = builder.endOfStream;
streamId = builder.streamId;
priority = builder.priority;
headers = builder.headersBuilder.build();
endOfStream = builder.endOfStream;
}

@Override
Expand Down Expand Up @@ -98,13 +98,13 @@ public boolean equals(Object obj) {
@Override
public String toString() {
return "DefaultHttp2HeadersFrame [streamId=" + streamId + ", priority=" + priority
+ ", endOfStream=" + endOfStream + ", headers=" + headers + "]";
+ ", endOfStream=" + endOfStream + ", headers=" + headers + ']';
}

public static class Builder {
private int streamId;
private int priority = DEFAULT_STREAM_PRIORITY;
private Http2Headers.Builder headersBuilder = new Http2Headers.Builder();
private final Http2Headers.Builder headersBuilder = new Http2Headers.Builder();
private boolean endOfStream;

public Builder setStreamId(int streamId) {
Expand Down Expand Up @@ -133,7 +133,7 @@ public Http2Headers.Builder headers() {
}

public Builder setHeaders(Http2Headers headers) {
this.headersBuilder.addHeaders(headers);
headersBuilder.addHeaders(headers);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class DefaultHttp2PingFrame extends DefaultByteBufHolder implements

private DefaultHttp2PingFrame(Builder builder) {
super(builder.data);
this.ack = builder.ack;
ack = builder.ack;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public final class DefaultHttp2PriorityFrame implements Http2PriorityFrame {
private final int priority;

private DefaultHttp2PriorityFrame(Builder builder) {
this.streamId = builder.streamId;
this.priority = builder.priority;
streamId = builder.streamId;
priority = builder.priority;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public final class DefaultHttp2PushPromiseFrame implements Http2PushPromiseFrame
private final Http2Headers headers;

private DefaultHttp2PushPromiseFrame(Builder builder) {
this.streamId = builder.streamId;
this.promisedStreamId = builder.promisedStreamId;
this.headers = builder.headers;
streamId = builder.streamId;
promisedStreamId = builder.promisedStreamId;
headers = builder.headers;
}

@Override
Expand Down Expand Up @@ -90,7 +90,7 @@ public boolean equals(Object obj) {
@Override
public String toString() {
return "DefaultHttp2PushPromiseFrame [streamId=" + streamId + ", promisedStreamId="
+ promisedStreamId + ", headers=" + headers + "]";
+ promisedStreamId + ", headers=" + headers + ']';
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public final class DefaultHttp2RstStreamFrame implements Http2RstStreamFrame {
private final long errorCode;

private DefaultHttp2RstStreamFrame(Builder builder) {
this.streamId = builder.streamId;
this.errorCode = builder.errorCode;
streamId = builder.streamId;
errorCode = builder.errorCode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public final class DefaultHttp2SettingsFrame implements Http2SettingsFrame {
private final Integer initialWindowSize;

private DefaultHttp2SettingsFrame(Builder builder) {
this.ack = builder.ack;
this.headerTableSize = builder.headerTableSize;
this.pushEnabled = builder.pushEnabled;
this.maxConcurrentStreams = builder.maxConcurrentStreams;
this.initialWindowSize = builder.initialWindowSize;
ack = builder.ack;
headerTableSize = builder.headerTableSize;
pushEnabled = builder.pushEnabled;
maxConcurrentStreams = builder.maxConcurrentStreams;
initialWindowSize = builder.initialWindowSize;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public final class DefaultHttp2WindowUpdateFrame implements Http2WindowUpdateFra
private final int windowSizeIncrement;

private DefaultHttp2WindowUpdateFrame(Builder builder) {
this.streamId = builder.streamId;
this.windowSizeIncrement = builder.windowSizeIncrement;
streamId = builder.streamId;
windowSizeIncrement = builder.windowSizeIncrement;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class Http2FrameCodecUtil {
*/
public static int readUnsignedInt(ByteBuf buf) {
int offset = buf.readerIndex();
int value = (buf.getByte(offset + 0) & 0x7F) << 24 | (buf.getByte(offset + 1) & 0xFF) << 16
int value = (buf.getByte(offset) & 0x7F) << 24 | (buf.getByte(offset + 1) & 0xFF) << 16
| (buf.getByte(offset + 2) & 0xFF) << 8 | buf.getByte(offset + 3) & 0xFF;
buf.skipBytes(4);
return value;
Expand Down Expand Up @@ -98,10 +98,10 @@ public static int readPaddingLength(Http2Flags flags, ByteBuf payload) {
*/
public static short setPaddingFlags(short flags, int paddingLength) {
if (paddingLength > 255) {
flags |= Http2FrameCodecUtil.FLAG_PAD_HIGH;
flags |= FLAG_PAD_HIGH;
}
if (paddingLength > 0) {
flags |= Http2FrameCodecUtil.FLAG_PAD_LOW;
flags |= FLAG_PAD_LOW;
}
return flags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public final class Http2FrameHeader {
private final int streamId;

private Http2FrameHeader(Builder builder) {
this.payloadLength = builder.payloadLength;
this.type = builder.type;
this.flags = builder.flags;
this.streamId = builder.streamId;
payloadLength = builder.payloadLength;
type = builder.type;
flags = builder.flags;
streamId = builder.streamId;
}

public int getPayloadLength() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public interface Http2SettingsFrame extends Http2Frame {

/**
* Gets the sender's initial flow control window in bytes, or {@code null} if not set.
*
* @return
*/
Integer getInitialWindowSize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class AbstractHeadersUnmarshaller extends AbstractHttp2FrameUnma
/**
* A builder for a headers/push_promise frame.
*/
protected abstract class FrameBuilder {
protected abstract static class FrameBuilder {
protected ByteBuf headerBlock;

abstract int getStreamId();
Expand Down
Loading

0 comments on commit 22c1327

Please sign in to comment.