2008-12-30 03:15:17 +01:00
|
|
|
/*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Copyright 2009 Red Hat, Inc.
|
2008-12-30 03:15:17 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Red Hat licenses this file to you 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:
|
2008-12-30 03:15:17 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-12-30 03:15:17 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* 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.
|
2008-12-30 03:15:17 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.protobuf;
|
2008-12-30 03:15:17 +01:00
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.buffer.ChannelBuffer;
|
|
|
|
import io.netty.buffer.ChannelBufferInputStream;
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelHandler.Sharable;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.channel.MessageEvent;
|
|
|
|
import io.netty.handler.codec.frame.FrameDecoder;
|
|
|
|
import io.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
|
|
|
|
import io.netty.handler.codec.frame.LengthFieldPrepender;
|
|
|
|
import io.netty.handler.codec.oneone.OneToOneDecoder;
|
2008-12-30 03:15:17 +01:00
|
|
|
|
|
|
|
import com.google.protobuf.ExtensionRegistry;
|
|
|
|
import com.google.protobuf.Message;
|
2010-05-06 06:11:11 +02:00
|
|
|
import com.google.protobuf.MessageLite;
|
2008-12-30 03:15:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a received {@link ChannelBuffer} into a
|
|
|
|
* <a href="http://code.google.com/p/protobuf/">Google Protocol Buffers</a>
|
2010-05-06 06:11:11 +02:00
|
|
|
* {@link Message} and {@link MessageLite}. Please note that this decoder must
|
|
|
|
* be used with a proper {@link FrameDecoder} such as {@link ProtobufVarint32FrameDecoder}
|
|
|
|
* or {@link LengthFieldBasedFrameDecoder} if you are using a stream-based
|
|
|
|
* transport such as TCP/IP. A typical setup for TCP/IP would be:
|
2008-12-30 03:15:17 +01:00
|
|
|
* <pre>
|
|
|
|
* {@link ChannelPipeline} pipeline = ...;
|
|
|
|
*
|
|
|
|
* // Decoders
|
|
|
|
* pipeline.addLast("frameDecoder",
|
2009-01-06 07:01:18 +01:00
|
|
|
* new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
|
2008-12-30 03:15:17 +01:00
|
|
|
* pipeline.addLast("protobufDecoder",
|
|
|
|
* new {@link ProtobufDecoder}(MyMessage.getDefaultInstance()));
|
|
|
|
*
|
|
|
|
* // Encoder
|
2009-01-07 13:48:36 +01:00
|
|
|
* pipeline.addLast("frameEncoder", new {@link LengthFieldPrepender}(4));
|
2008-12-30 03:15:17 +01:00
|
|
|
* pipeline.addLast("protobufEncoder", new {@link ProtobufEncoder}());
|
|
|
|
* </pre>
|
|
|
|
* and then you can use a {@code MyMessage} instead of a {@link ChannelBuffer}
|
|
|
|
* as a message:
|
|
|
|
* <pre>
|
2010-02-02 03:00:04 +01:00
|
|
|
* void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
|
2008-12-30 03:15:17 +01:00
|
|
|
* MyMessage req = (MyMessage) e.getMessage();
|
|
|
|
* MyMessage res = MyMessage.newBuilder().setText(
|
|
|
|
* "Did you say '" + req.getText() + "'?").build();
|
|
|
|
* ch.write(res);
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
* @apiviz.landmark
|
|
|
|
*/
|
2010-02-02 01:38:07 +01:00
|
|
|
@Sharable
|
2008-12-30 03:15:17 +01:00
|
|
|
public class ProtobufDecoder extends OneToOneDecoder {
|
|
|
|
|
2010-05-06 06:11:11 +02:00
|
|
|
private final MessageLite prototype;
|
2008-12-30 03:15:17 +01:00
|
|
|
private final ExtensionRegistry extensionRegistry;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*/
|
2010-05-06 06:11:11 +02:00
|
|
|
public ProtobufDecoder(MessageLite prototype) {
|
2008-12-30 03:15:17 +01:00
|
|
|
this(prototype, null);
|
|
|
|
}
|
|
|
|
|
2010-05-06 06:11:11 +02:00
|
|
|
public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
|
2008-12-30 03:15:17 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-16 07:57:34 +02:00
|
|
|
ChannelBuffer buf = (ChannelBuffer) msg;
|
|
|
|
if (buf.hasArray()) {
|
2011-01-12 11:23:50 +01:00
|
|
|
final int offset = buf.readerIndex();
|
2010-08-16 07:57:34 +02:00
|
|
|
if(extensionRegistry == null) {
|
|
|
|
return prototype.newBuilderForType().mergeFrom(
|
2011-01-12 11:23:50 +01:00
|
|
|
buf.array(), buf.arrayOffset() + offset, buf.readableBytes()).build();
|
2010-08-16 07:57:34 +02:00
|
|
|
} else {
|
|
|
|
return prototype.newBuilderForType().mergeFrom(
|
2011-01-12 11:23:50 +01:00
|
|
|
buf.array(), buf.arrayOffset() + offset, buf.readableBytes(), extensionRegistry).build();
|
2010-08-16 07:57:34 +02:00
|
|
|
}
|
2008-12-30 03:15:17 +01:00
|
|
|
} else {
|
2010-08-16 07:57:34 +02:00
|
|
|
if (extensionRegistry == null) {
|
|
|
|
return prototype.newBuilderForType().mergeFrom(
|
|
|
|
new ChannelBufferInputStream((ChannelBuffer) msg)).build();
|
|
|
|
} else {
|
|
|
|
return prototype.newBuilderForType().mergeFrom(
|
|
|
|
new ChannelBufferInputStream((ChannelBuffer) msg), extensionRegistry).build();
|
|
|
|
}
|
2008-12-30 03:15:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|