/* * Copyright 2011 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.channel; import io.netty.buffer.ChannelBuffer; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; /** * A {@link ChannelUpstreamHandler} which provides an individual handler method * for each event type. This handler down-casts the received upstream event * into more meaningful sub-type event and calls an appropriate handler method * with the down-cast event. The names of the methods are identical to the * upstream event names, as introduced in the {@link ChannelEvent} documentation. *
* Please use {@link SimpleChannelHandler} if you need to implement both * {@link ChannelUpstreamHandler} and {@link ChannelDownstreamHandler}. * *
* You can override the {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream} * method just like overriding an ordinary Java method. Please make sure to * call {@code super.handleUpstream()} so that other handler methods are invoked * properly: *
*public class MyChannelHandler extends {@link SimpleChannelUpstreamHandler} { * * {@code @Override} * public void handleUpstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception { * * // Log all channel state changes. * if (e instanceof {@link ChannelStateEvent}) { * logger.info("Channel state changed: " + e); * } * * super.handleUpstream(ctx, e); * } * }*/ public class SimpleChannelUpstreamHandler implements ChannelUpstreamHandler { private static final InternalLogger logger = InternalLoggerFactory.getInstance(SimpleChannelUpstreamHandler.class.getName()); /** * Creates a new instance. */ public SimpleChannelUpstreamHandler() { } /** * {@inheritDoc} Down-casts the received upstream event into more * meaningful sub-type event and calls an appropriate handler method with * the down-casted event. */ @Override public void handleUpstream( ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (e instanceof MessageEvent) { messageReceived(ctx, (MessageEvent) e); } else if (e instanceof WriteCompletionEvent) { WriteCompletionEvent evt = (WriteCompletionEvent) e; writeComplete(ctx, evt); } else if (e instanceof ChildChannelStateEvent) { ChildChannelStateEvent evt = (ChildChannelStateEvent) e; if (evt.getChildChannel().isOpen()) { childChannelOpen(ctx, evt); } else { childChannelClosed(ctx, evt); } } else if (e instanceof ChannelStateEvent) { ChannelStateEvent evt = (ChannelStateEvent) e; switch (evt.getState()) { case OPEN: if (Boolean.TRUE.equals(evt.getValue())) { channelOpen(ctx, evt); } else { channelClosed(ctx, evt); } break; case BOUND: if (evt.getValue() != null) { channelBound(ctx, evt); } else { channelUnbound(ctx, evt); } break; case CONNECTED: if (evt.getValue() != null) { channelConnected(ctx, evt); } else { channelDisconnected(ctx, evt); } break; case INTEREST_OPS: channelInterestChanged(ctx, evt); break; default: ctx.sendUpstream(e); } } else if (e instanceof ExceptionEvent) { exceptionCaught(ctx, (ExceptionEvent) e); } else { ctx.sendUpstream(e); } } /** * Invoked when a message object (e.g: {@link ChannelBuffer}) was received * from a remote peer. */ public void messageReceived( ChannelHandlerContext ctx, MessageEvent e) throws Exception { ctx.sendUpstream(e); } /** * Invoked when an exception was raised by an I/O thread or a * {@link ChannelHandler}. */ public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { if (this == ctx.getPipeline().getLast()) { logger.warn( "EXCEPTION, please implement " + getClass().getName() + ".exceptionCaught() for proper handling.", e.getCause()); } ctx.sendUpstream(e); } /** * Invoked when a {@link Channel} is open, but not bound nor connected. *