View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.ChannelHandlerContext;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.MessageEvent;
25  import org.jboss.netty.channel.ChannelHandler.Sharable;
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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
62   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
63   *
64   * @version $Rev:231 $, $Date:2008-06-12 16:44:50 +0900 (목, 12 6월 2008) $
65   *
66   * @apiviz.landmark
67   */
68  @Sharable
69  public class ProtobufEncoder extends OneToOneEncoder {
70  
71      /**
72       * Creates a new instance.
73       */
74      public ProtobufEncoder() {
75          super();
76      }
77  
78      @Override
79      protected Object encode(
80              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
81          if (msg instanceof MessageLite) {
82              return wrappedBuffer(((MessageLite) msg).toByteArray());
83          }
84          if (msg instanceof MessageLite.Builder) {
85              return wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray());
86          }
87          return msg;
88      }
89  }