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