查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.oneone;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  import org.jboss.netty.channel.Channel;
21  import org.jboss.netty.channel.ChannelEvent;
22  import org.jboss.netty.channel.ChannelHandlerContext;
23  import org.jboss.netty.channel.ChannelPipeline;
24  import org.jboss.netty.channel.ChannelUpstreamHandler;
25  import org.jboss.netty.channel.MessageEvent;
26  import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;
27  import org.jboss.netty.handler.codec.frame.Delimiters;
28  import org.jboss.netty.handler.codec.frame.FrameDecoder;
29  
30  /**
31   * Transforms a received message into another message.  Please note that this
32   * decoder must be used with a proper {@link FrameDecoder} such as
33   * {@link DelimiterBasedFrameDecoder} or you must implement proper framing
34   * mechanism by yourself if you are using a stream-based transport such as
35   * TCP/IP.  A typical setup for TCP/IP would be:
36   * <pre>
37   * {@link ChannelPipeline} pipeline = ...;
38   *
39   * // Decoders
40   * pipeline.addLast("frameDecoder", new {@link DelimiterBasedFrameDecoder}(80, {@link Delimiters#nulDelimiter()}));
41   * pipeline.addLast("customDecoder", new {@link OneToOneDecoder}() { ... });
42   *
43   * // Encoder
44   * pipeline.addLast("customEncoder", new {@link OneToOneEncoder}() { ... });
45   * </pre>
46   *
47   * @apiviz.landmark
48   */
49  public abstract class OneToOneDecoder implements ChannelUpstreamHandler {
50  
51      /**
52       * Creates a new instance with the current system character set.
53       */
54      protected OneToOneDecoder() {
55      }
56  
57      public void handleUpstream(
58              ChannelHandlerContext ctx, ChannelEvent evt) throws Exception {
59          if (!(evt instanceof MessageEvent)) {
60              ctx.sendUpstream(evt);
61              return;
62          }
63  
64          MessageEvent e = (MessageEvent) evt;
65          Object originalMessage = e.getMessage();
66          Object decodedMessage = decode(ctx, e.getChannel(), originalMessage);
67          if (originalMessage == decodedMessage) {
68              ctx.sendUpstream(evt);
69          } else if (decodedMessage != null) {
70              fireMessageReceived(ctx, decodedMessage, e.getRemoteAddress());
71          }
72      }
73  
74      /**
75       * Transforms the specified received message into another message and return
76       * the transformed message.  Return {@code null} if the received message
77       * is supposed to be discarded.
78       */
79      protected abstract Object decode(
80              ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception;
81  }