Resolved issue: NETTY-97 Google Protocol Buffer codec

* Added ProtobufEncoder and ProtobufDecoder
* TODO: Need to write an example
This commit is contained in:
Trustin Lee 2008-12-30 02:15:17 +00:00
parent 54828269e4
commit 5d7ec61346
4 changed files with 241 additions and 0 deletions

10
pom.xml
View File

@ -33,6 +33,16 @@
</scm>
<dependencies>
<!-- Google Protocol Buffers - completely optional -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.0.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- Logging frameworks - completely optional -->
<dependency>
<groupId>org.slf4j</groupId>

View File

@ -0,0 +1,112 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.handler.codec.protobuf;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
/**
* Decodes a received {@link ChannelBuffer} into a
* <a href="http://code.google.com/p/protobuf/">Google Protocol Buffers</a>
* {@link Message}. Please note that this decoder must be used with a proper
* {@link FrameDecoder} such as {@link LengthFieldBasedFrameDecoder} if you are
* using a stream-based transport such as TCP/IP. A typical setup for TCP/IP
* would be:
* <pre>
* {@link ChannelPipeline} pipeline = ...;
*
* // Decoders
* pipeline.addLast("frameDecoder",
* new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4));
* pipeline.addLast("protobufDecoder",
* new {@link ProtobufDecoder}(MyMessage.getDefaultInstance()));
*
* // Encoder
* pipeline.addLast("protobufEncoder", new {@link ProtobufEncoder}());
* </pre>
* and then you can use a {@code MyMessage} instead of a {@link ChannelBuffer}
* as a message:
* <pre>
* void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
* MyMessage req = (MyMessage) e.getMessage();
* MyMessage res = MyMessage.newBuilder().setText(
* "Did you say '" + req.getText() + "'?").build();
* ch.write(res);
* }
* </pre>
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $
*
* @apiviz.landmark
*/
@ChannelPipelineCoverage("all")
public class ProtobufDecoder extends OneToOneDecoder {
private final Message prototype;
private final ExtensionRegistry extensionRegistry;
/**
* Creates a new instance.
*/
public ProtobufDecoder(Message prototype) {
this(prototype, null);
}
public ProtobufDecoder(Message prototype, ExtensionRegistry extensionRegistry) {
if (prototype == null) {
throw new NullPointerException("prototype");
}
this.prototype = prototype.getDefaultInstanceForType();
this.extensionRegistry = extensionRegistry;
}
@Override
protected Object decode(
ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (!(msg instanceof ChannelBuffer)) {
return msg;
}
if (extensionRegistry == null) {
return prototype.newBuilderForType().mergeFrom(
new ChannelBufferInputStream((ChannelBuffer) msg)).build();
} else {
return prototype.newBuilderForType().mergeFrom(
new ChannelBufferInputStream((ChannelBuffer) msg),
extensionRegistry).build();
}
}
}

View File

@ -0,0 +1,89 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.netty.handler.codec.protobuf;
import static org.jboss.netty.buffer.ChannelBuffers.*;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import com.google.protobuf.Message;
/**
* Encodes the requested <a href="http://code.google.com/p/protobuf/">Google
* Protocol Buffers</a> {@link Message} into a {@link ChannelBuffer}.
* A typical setup for TCP/IP would be:
* <pre>
* {@link ChannelPipeline} pipeline = ...;
*
* // Decoders
* pipeline.addLast("frameDecoder",
* new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4));
* pipeline.addLast("protobufDecoder",
* new {@link ProtobufDecoder}(MyMessage.getDefaultInstance()));
*
* // Encoder
* pipeline.addLast("protobufEncoder", new {@link ProtobufEncoder}());
* </pre>
* and then you can use a {@code MyMessage} instead of a {@link ChannelBuffer}
* as a message:
* <pre>
* void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
* MyMessage req = (MyMessage) e.getMessage();
* MyMessage res = MyMessage.newBuilder().setText(
* "Did you say '" + req.getText() + "'?").build();
* ch.write(res);
* }
* </pre>
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (, 12 6월 2008) $
*
* @apiviz.landmark
*/
@ChannelPipelineCoverage("all")
public class ProtobufEncoder extends OneToOneEncoder {
/**
* Creates a new instance.
*/
public ProtobufEncoder() {
super();
}
@Override
protected Object encode(
ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (!(msg instanceof Message)) {
return msg;
}
return wrappedBuffer(((Message) msg).toByteArray());
}
}

View File

@ -0,0 +1,30 @@
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
* Encoder and decoder which transform a
* <a href="http://code.google.com/p/protobuf/">Google Protocol Buffers</a>
* {@link com.google.protobuf.Message} into a {@link org.jboss.netty.buffer.ChannelBuffer}
* and vice versa.
*/
package org.jboss.netty.handler.codec.protobuf;