* Improved protocol buffers integration javadoc

* Changed the localtime example to use the base128 variant length header which is popular among protobuf users
This commit is contained in:
Trustin Lee 2010-06-23 05:16:47 +00:00
parent 685153663e
commit b2ebe26a25
4 changed files with 43 additions and 16 deletions

View File

@ -19,10 +19,10 @@ import static org.jboss.netty.channel.Channels.*;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufEncoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
/**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
@ -33,10 +33,10 @@ public class LocalTimeClientPipelineFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline();
p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LocalTimeProtocol.LocalTimes.getDefaultInstance()));
p.addLast("frameEncoder", new LengthFieldPrepender(4));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new LocalTimeClientHandler());

View File

@ -19,10 +19,10 @@ import static org.jboss.netty.channel.Channels.*;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.jboss.netty.handler.codec.protobuf.ProtobufDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufEncoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import org.jboss.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
/**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
@ -33,10 +33,10 @@ public class LocalTimeServerPipelineFactory implements ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline p = pipeline();
p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LocalTimeProtocol.Locations.getDefaultInstance()));
p.addLast("frameEncoder", new LengthFieldPrepender(4));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new LocalTimeServerHandler());

View File

@ -24,21 +24,34 @@ import com.google.protobuf.CodedInputStream;
/**
* A decoder that splits the received {@link ChannelBuffer}s dynamically by the
* value of the length field in the message. {@link ProtobufVarint32FrameDecoder}
* should be used to decode a binary message which has an integer header field
* encoded as Google Protocol Buffer <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints">Base
* 128 Varints</a> (32-bit) integer that represents the length of the message
* body.
* value of the Google Protocol Buffers
* <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints">Base
* 128 Varints</a> integer length field in the message. For example:
* <pre>
* BEFORE DECODE (302 bytes) AFTER DECODE (300 bytes)
* +--------+---------------+ +---------------+
* | Length | Protobuf Data |----->| Protobuf Data |
* | 0xAC02 | (300 bytes) | | (300 bytes) |
* +--------+---------------+ +---------------+
* </pre>
*
* @see com.google.protobuf.CodedInputStream
*
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author Tomasz Blachowicz (tblachowicz@gmail.com)
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
*
* @version $Rev$, $Date$
*/
public class ProtobufVarint32FrameDecoder extends FrameDecoder {
/**
* Creates a new instance.
*/
public ProtobufVarint32FrameDecoder() {
super();
}
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
buffer.markReaderIndex();

View File

@ -27,20 +27,34 @@ import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import com.google.protobuf.CodedOutputStream;
/**
* An encoder that prepends the length of the message. The length value is
* prepended as a binary form. encoded as Google Protocol Buffer
* An encoder that prepends the the Google Protocol Buffers
* <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints">Base
* 128 Varints</a> (32-bit).
* 128 Varints</a> integer length field. For example:
* <pre>
* BEFORE DECODE (300 bytes) AFTER DECODE (302 bytes)
* +---------------+ +--------+---------------+
* | Protobuf Data |-------------->| Length | Protobuf Data |
* | (300 bytes) | | 0xAC02 | (300 bytes) |
* +---------------+ +--------+---------------+
* </pre> *
*
* @see com.google.protobuf.CodedOutputStream
*
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
* @author Tomasz Blachowicz (tblachowicz@gmail.com)
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
* @version $Rev$, $Date$
*/
@Sharable
public class ProtobufVarint32LengthFieldPrepender extends OneToOneEncoder {
/**
* Creates a new instance.
*/
public ProtobufVarint32LengthFieldPrepender() {
super();
}
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel,
Object msg) throws Exception {