Skip to content

Commit

Permalink
SPDY Protocol Framing Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Pinner committed Jan 31, 2012
1 parent e4fa180 commit cf8a4d6
Show file tree
Hide file tree
Showing 30 changed files with 3,617 additions and 0 deletions.
@@ -0,0 +1,101 @@
/*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.netty.handler.codec.spdy;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.util.internal.StringUtil;

/**
* The default {@link SpdyDataFrame} implementation.
*/
public class DefaultSpdyDataFrame implements SpdyDataFrame {

private int streamID;
private boolean last;
private boolean compressed;
private ChannelBuffer data = ChannelBuffers.EMPTY_BUFFER;

/**
* Creates a new instance.
*
* @param streamID the Stream-ID of this frame
*/
public DefaultSpdyDataFrame(int streamID) {
setStreamID(streamID);
}

public int getStreamID() {
return streamID;
}

public void setStreamID(int streamID) {
if (streamID <= 0) {
throw new IllegalArgumentException(
"Stream-ID must be positive: " + streamID);
}
this.streamID = streamID;
}

public boolean isLast() {
return last;
}

public void setLast(boolean last) {
this.last = last;
}

public boolean isCompressed() {
return compressed;
}

public void setCompressed(boolean compressed) {
this.compressed = compressed;
}

public ChannelBuffer getData() {
return data;
}

public void setData(ChannelBuffer data) {
if (data == null) {
data = ChannelBuffers.EMPTY_BUFFER;
}
if (data.readableBytes() > SpdyCodecUtil.SPDY_MAX_LENGTH) {
throw new IllegalArgumentException("data payload cannot exceed "
+ SpdyCodecUtil.SPDY_MAX_LENGTH + " bytes");
}
this.data = data;
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(getClass().getSimpleName());
buf.append("(last: ");
buf.append(isLast());
buf.append("; compressed: ");
buf.append(isCompressed());
buf.append(')');
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamID);
buf.append(StringUtil.NEWLINE);
buf.append("--> Size = ");
buf.append(data.readableBytes());
return buf.toString();
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.netty.handler.codec.spdy;

import org.jboss.netty.util.internal.StringUtil;

/**
* The default {@link SpdyGoAwayFrame} implementation.
*/
public class DefaultSpdyGoAwayFrame implements SpdyGoAwayFrame {

private int lastGoodStreamID;

/**
* Creates a new instance.
*
* @param lastGoodStreamID the Last-good-stream-ID of this frame
*/
public DefaultSpdyGoAwayFrame(int lastGoodStreamID) {
setLastGoodStreamID(lastGoodStreamID);
}

public int getLastGoodStreamID() {
return lastGoodStreamID;
}

public void setLastGoodStreamID(int lastGoodStreamID) {
if (lastGoodStreamID < 0) {
throw new IllegalArgumentException("Last-good-stream-ID"
+ " cannot be negative: " + lastGoodStreamID);
}
this.lastGoodStreamID = lastGoodStreamID;
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(getClass().getSimpleName());
buf.append(StringUtil.NEWLINE);
buf.append("--> Last-good-stream-ID = ");
buf.append(lastGoodStreamID);
return buf.toString();
}
}
@@ -0,0 +1,96 @@
/*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.netty.handler.codec.spdy;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.netty.util.internal.StringUtil;

/**
* The default {@link SpdyHeaderBlock} implementation.
*/
public class DefaultSpdyHeaderBlock implements SpdyHeaderBlock {

private boolean invalid;
private final SpdyHeaders headers = new SpdyHeaders();

/**
* Creates a new instance.
*/
protected DefaultSpdyHeaderBlock() {
}

public boolean isInvalid() {
return invalid;
}

public void setInvalid() {
this.invalid = true;
}

public void addHeader(final String name, final Object value) {
headers.addHeader(name, value);
}

public void setHeader(final String name, final Object value) {
headers.setHeader(name, value);
}

public void setHeader(final String name, final Iterable<?> values) {
headers.setHeader(name, values);
}

public void removeHeader(final String name) {
headers.removeHeader(name);
}

public void clearHeaders() {
headers.clearHeaders();
}

public String getHeader(final String name) {
List<String> values = getHeaders(name);
return values.size() > 0 ? values.get(0) : null;
}

public List<String> getHeaders(final String name) {
return headers.getHeaders(name);
}

public List<Map.Entry<String, String>> getHeaders() {
return headers.getHeaders();
}

public boolean containsHeader(final String name) {
return headers.containsHeader(name);
}

public Set<String> getHeaderNames() {
return headers.getHeaderNames();
}

protected void appendHeaders(StringBuilder buf) {
for (Map.Entry<String, String> e: getHeaders()) {
buf.append(" ");
buf.append(e.getKey());
buf.append(": ");
buf.append(e.getValue());
buf.append(StringUtil.NEWLINE);
}
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.netty.handler.codec.spdy;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.netty.util.internal.StringUtil;

/**
* The default {@link SpdyHeadersFrame} implementation.
*/
public class DefaultSpdyHeadersFrame extends DefaultSpdyHeaderBlock
implements SpdyHeadersFrame {

private int streamID;

/**
* Creates a new instance.
*
* @param streamID the Stream-ID of this frame
*/
public DefaultSpdyHeadersFrame(int streamID) {
super();
setStreamID(streamID);
}

public int getStreamID() {
return streamID;
}

public void setStreamID(int streamID) {
if (streamID <= 0) {
throw new IllegalArgumentException(
"Stream-ID must be positive: " + streamID);
}
this.streamID = streamID;
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(getClass().getSimpleName());
buf.append(StringUtil.NEWLINE);
buf.append("--> Stream-ID = ");
buf.append(streamID);
buf.append(StringUtil.NEWLINE);
buf.append("--> Headers:");
buf.append(StringUtil.NEWLINE);
appendHeaders(buf);

// Remove the last newline.
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
return buf.toString();
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.netty.handler.codec.spdy;

/**
* The default {@link SpdyNoOpFrame} implementation.
*/
public class DefaultSpdyNoOpFrame implements SpdyNoOpFrame {

/**
* Creates a new instance.
*/
public DefaultSpdyNoOpFrame() {
}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(getClass().getSimpleName());
return buf.toString();
}
}

0 comments on commit cf8a4d6

Please sign in to comment.