查看本类的 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.channel;
17  
18  import java.net.SocketAddress;
19  
20  
21  /**
22   * A {@link ChannelDownstreamHandler} which provides an individual handler
23   * method for each event type.  This handler down-casts the received downstream
24   * event into more meaningful sub-type event and calls an appropriate handler
25   * method with the down-cast event.  The names of the methods starts with the
26   * name of the operation and ends with {@code "Requested"}
27   * (e.g. {@link #writeRequested(ChannelHandlerContext, MessageEvent) writeRequested}.)
28   * <p>
29   * Please use {@link SimpleChannelHandler} if you need to implement both
30   * {@link ChannelUpstreamHandler} and {@link ChannelDownstreamHandler}.
31   *
32   * <h3>Overriding the {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream} method</h3>
33   * <p>
34   * You can override the {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream}
35   * method just like overriding an ordinary Java method.  Please make sure to
36   * call {@code super.handleDownstream()} so that other handler methods are
37   * invoked properly:
38   * </p>
39   * <pre>public class MyChannelHandler extends {@link SimpleChannelDownstreamHandler} {
40   *
41   *     {@code @Override}
42   *     public void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
43   *
44   *         // Log all channel state changes.
45   *         if (e instanceof {@link MessageEvent}) {
46   *             logger.info("Writing:: " + e);
47   *         }
48   *
49   *         <strong>super.handleDownstream(ctx, e);</strong>
50   *     }
51   * }</pre>
52   * <p>
53   * <strong>Caution:</strong>
54   * <p>
55   * Use the *Later(..) methods of the {@link Channels} class if you want to send an upstream event
56   * from a {@link ChannelDownstreamHandler} otherwise you may run into threading issues.
57   *
58   */
59  public class SimpleChannelDownstreamHandler implements ChannelDownstreamHandler {
60  
61      /**
62       * {@inheritDoc}  Down-casts the received downstream event into more
63       * meaningful sub-type event and calls an appropriate handler method with
64       * the down-casted event.
65       */
66      public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
67              throws Exception {
68  
69          if (e instanceof MessageEvent) {
70              writeRequested(ctx, (MessageEvent) e);
71          } else if (e instanceof ChannelStateEvent) {
72              ChannelStateEvent evt = (ChannelStateEvent) e;
73              switch (evt.getState()) {
74              case OPEN:
75                  if (!Boolean.TRUE.equals(evt.getValue())) {
76                      closeRequested(ctx, evt);
77                  }
78                  break;
79              case BOUND:
80                  if (evt.getValue() != null) {
81                      bindRequested(ctx, evt);
82                  } else {
83                      unbindRequested(ctx, evt);
84                  }
85                  break;
86              case CONNECTED:
87                  if (evt.getValue() != null) {
88                      connectRequested(ctx, evt);
89                  } else {
90                      disconnectRequested(ctx, evt);
91                  }
92                  break;
93              case INTEREST_OPS:
94                  setInterestOpsRequested(ctx, evt);
95                  break;
96              default:
97                  ctx.sendDownstream(e);
98              }
99          } else {
100             ctx.sendDownstream(e);
101         }
102     }
103 
104     /**
105      * Invoked when {@link Channel#write(Object)} is called.
106      */
107     public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
108         ctx.sendDownstream(e);
109     }
110 
111     /**
112      * Invoked when {@link Channel#bind(SocketAddress)} was called.
113      */
114     public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
115         ctx.sendDownstream(e);
116     }
117 
118     /**
119      * Invoked when {@link Channel#connect(SocketAddress)} was called.
120      */
121     public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
122         ctx.sendDownstream(e);
123     }
124 
125     /**
126      * Invoked when {@link Channel#setInterestOps(int)} was called.
127      */
128     public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
129         ctx.sendDownstream(e);
130     }
131 
132     /**
133      * Invoked when {@link Channel#disconnect()} was called.
134      */
135     public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
136         ctx.sendDownstream(e);
137     }
138 
139     /**
140      * Invoked when {@link Channel#unbind()} was called.
141      */
142     public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
143         ctx.sendDownstream(e);
144     }
145 
146     /**
147      * Invoked when {@link Channel#close()} was called.
148      */
149     public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
150         ctx.sendDownstream(e);
151     }
152 }