View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.protobuf;
17  
18  import static org.jboss.netty.buffer.ChannelBuffers.*;
19  
20  import org.jboss.netty.buffer.ChannelBuffer;
21  import org.jboss.netty.channel.Channel;
22  import org.jboss.netty.channel.ChannelHandler.Sharable;
23  import org.jboss.netty.channel.ChannelHandlerContext;
24  import org.jboss.netty.channel.ChannelPipeline;
25  import org.jboss.netty.channel.MessageEvent;
26  import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
27  import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
28  import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
29  
30  import com.google.protobuf.Message;
31  import com.google.protobuf.MessageLite;
32  
33  /**
34   * Encodes the requested <a href="http://code.google.com/p/protobuf/">Google
35   * Protocol Buffers</a> {@link Message} and {@link MessageLite} into a
36   * {@link ChannelBuffer}. A typical setup for TCP/IP would be:
37   * <pre>
38   * {@link ChannelPipeline} pipeline = ...;
39   *
40   * // Decoders
41   * pipeline.addLast("frameDecoder",
42   *                  new {@link LengthFieldBasedFrameDecoder}(1048576, 0, 4, 0, 4));
43   * pipeline.addLast("protobufDecoder",
44   *                  new {@link ProtobufDecoder}(MyMessage.getDefaultInstance()));
45   *
46   * // Encoder
47   * pipeline.addLast("frameEncoder", new {@link LengthFieldPrepender}(4));
48   * pipeline.addLast("protobufEncoder", new {@link ProtobufEncoder}());
49   * </pre>
50   * and then you can use a {@code MyMessage} instead of a {@link ChannelBuffer}
51   * as a message:
52   * <pre>
53   * void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
54   *     MyMessage req = (MyMessage) e.getMessage();
55   *     MyMessage res = MyMessage.newBuilder().setText(
56   *                               "Did you say '" + req.getText() + "'?").build();
57   *     ch.write(res);
58   * }
59   * </pre>
60   *
61   * @apiviz.landmark
62   */
63  @Sharable
64  public class ProtobufEncoder extends OneToOneEncoder {
65  
66      @Override
67      protected Object encode(
68              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
69          if (msg instanceof MessageLite) {
70              return wrappedBuffer(((MessageLite) msg).toByteArray());
71          }
72          if (msg instanceof MessageLite.Builder) {
73              return wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray());
74          }
75          return msg;
76      }
77  }