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.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   *
53   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
54   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
55   *
56   * @version $Rev: 2122 $, $Date: 2010-02-02 11:00:04 +0900 (Tue, 02 Feb 2010) $
57   */
58  public class SimpleChannelDownstreamHandler implements ChannelDownstreamHandler {
59  
60      /**
61       * Creates a new instance.
62       */
63      public SimpleChannelDownstreamHandler() {
64          super();
65      }
66  
67      /**
68       * {@inheritDoc}  Down-casts the received downstream event into more
69       * meaningful sub-type event and calls an appropriate handler method with
70       * the down-casted event.
71       */
72      public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
73              throws Exception {
74  
75          if (e instanceof MessageEvent) {
76              writeRequested(ctx, (MessageEvent) e);
77          } else if (e instanceof ChannelStateEvent) {
78              ChannelStateEvent evt = (ChannelStateEvent) e;
79              switch (evt.getState()) {
80              case OPEN:
81                  if (!Boolean.TRUE.equals(evt.getValue())) {
82                      closeRequested(ctx, evt);
83                  }
84                  break;
85              case BOUND:
86                  if (evt.getValue() != null) {
87                      bindRequested(ctx, evt);
88                  } else {
89                      unbindRequested(ctx, evt);
90                  }
91                  break;
92              case CONNECTED:
93                  if (evt.getValue() != null) {
94                      connectRequested(ctx, evt);
95                  } else {
96                      disconnectRequested(ctx, evt);
97                  }
98                  break;
99              case INTEREST_OPS:
100                 setInterestOpsRequested(ctx, evt);
101                 break;
102             default:
103                 ctx.sendDownstream(e);
104             }
105         } else {
106             ctx.sendDownstream(e);
107         }
108     }
109 
110     /**
111      * Invoked when {@link Channel#write(Object)} is called.
112      */
113     public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
114         ctx.sendDownstream(e);
115     }
116 
117     /**
118      * Invoked when {@link Channel#bind(SocketAddress)} was called.
119      */
120     public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
121         ctx.sendDownstream(e);
122 
123     }
124 
125     /**
126      * Invoked when {@link Channel#connect(SocketAddress)} was called.
127      */
128     public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
129         ctx.sendDownstream(e);
130 
131     }
132 
133     /**
134      * Invoked when {@link Channel#setInterestOps(int)} was called.
135      */
136     public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
137         ctx.sendDownstream(e);
138     }
139 
140     /**
141      * Invoked when {@link Channel#disconnect()} was called.
142      */
143     public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
144         ctx.sendDownstream(e);
145 
146     }
147 
148     /**
149      * Invoked when {@link Channel#unbind()} was called.
150      */
151     public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
152         ctx.sendDownstream(e);
153 
154     }
155 
156     /**
157      * Invoked when {@link Channel#close()} was called.
158      */
159     public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
160         ctx.sendDownstream(e);
161     }
162 }