Revamp channel handler API

- Merged LifeCycleAwareChannelHandler into ChannelHandler
- Replaced ChannelUpstreamHandler and ChannelDownstreamHandler with
  ChannelReader and ChannelWriter
  - These two new interfaces are much more type-safe than its ancestor.
- Simplified channel state model as described in #68
- Handler creates send/receive buffer.
  - Previously, Netty created them, but it led to more memory copies and
    inflexibility.  I'm going to allow a handler to create a bounded
    queue for example.
  - It currently uses Queue<T> but I'll define a new interface and make
    ChannelBuffer implement it (e.g. Queue<Byte>)
- Introduced AttributeMap which replaces attachments in Channel and
  ChannelHandlerContext and ChannelLocal
This commit is contained in:
Trustin Lee 2012-04-12 17:39:01 +09:00
parent 05204025cc
commit 22a815eaf8
18 changed files with 420 additions and 1026 deletions

View File

@ -0,0 +1,10 @@
package io.netty.util;
public interface Attribute<T> {
T get();
void set(T value);
T getAndSet(T value);
T setIfAbsent(T value);
boolean compareAndSet(T oldValue, T newValue);
void remove();
}

View File

@ -0,0 +1,59 @@
package io.netty.util;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class AttributeKey<T> implements Serializable, Comparable<AttributeKey<T>> {
private static final long serialVersionUID = 2783354860083517323L;
private static final ConcurrentMap<String, Boolean> names = new ConcurrentHashMap<String, Boolean>();
private final String name;
private final Class<T> valueType;
public AttributeKey(String name, Class<T> valueType) {
if (name == null) {
throw new NullPointerException("name");
}
if (valueType == null) {
throw new NullPointerException("valueType");
}
if (names.putIfAbsent(name, Boolean.TRUE) != null) {
throw new IllegalArgumentException("key name already in use: " + name);
}
this.name = name;
this.valueType = valueType;
}
public String name() {
return name;
}
public Class<T> valueType() {
return valueType;
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object o) {
return this == o;
}
@Override
public int compareTo(AttributeKey<T> o) {
return name().compareTo(o.name());
}
@Override
public String toString() {
return name();
}
}

View File

@ -0,0 +1,5 @@
package io.netty.util;
public interface AttributeMap {
<T> Attribute<T> attr(AttributeKey key, Class<T> type);
}

View File

@ -1,85 +0,0 @@
/*
* 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;
/**
* Handles or intercepts a downstream {@link ChannelEvent}, and sends a
* {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
* <p>
* The most common use case of this interface is to intercept an I/O request
* such as {@link Channel#write(Object)} and {@link Channel#close()}.
*
* <h3>{@link SimpleChannelDownstreamHandler}</h3>
* <p>
* In most cases, you will get to use a {@link SimpleChannelDownstreamHandler}
* to implement a downstream handler because it provides an individual handler
* method for each event type. You might want to implement this interface
* directly though if you want to handle various types of events in more
* generic way.
*
* <h3>Firing an event to the next handler</h3>
* <p>
* You can forward the received event downstream or upstream. In most cases,
* {@link ChannelDownstreamHandler} will send the event downstream
* (i.e. outbound) although it is legal to send the event upstream (i.e. inbound):
*
* <pre>
* // Sending the event downstream (outbound)
* void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
* ...
* ctx.sendDownstream(e);
* ...
* }
*
* // Sending the event upstream (inbound)
* void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
* ...
* ctx.sendUpstream(new {@link UpstreamChannelStateEvent}(...));
* ...
* }
* </pre>
*
* <h4>Using the helper class to send an event</h4>
* <p>
* You will also find various helper methods in {@link Channels} to be useful
* to generate and send an artificial or manipulated event.
* <p>
* <strong>Caution:</strong>
* <p>
* Use the *Later(..) methods of the {@link Channels} class if you want to send an upstream event from a {@link ChannelDownstreamHandler} otherwise you may run into threading issues.
*
* <h3>State management</h3>
*
* Please refer to {@link ChannelHandler}.
*
* <h3>Thread safety</h3>
* <p>
* {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream}
* may be invoked by more than one thread simultaneously. If the handler
* accesses a shared resource or stores stateful information, you might need
* proper synchronization in the handler implementation.
* @apiviz.exclude ^io\.netty\.handler\..*$
*/
public interface ChannelDownstreamHandler extends ChannelHandler {
/**
* Handles the specified downstream event.
*
* @param ctx the context object for this handler
* @param e the downstream event to process or intercept
*/
void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception;
}

View File

@ -15,6 +15,9 @@
*/
package io.netty.channel;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.group.ChannelGroup;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
@ -22,9 +25,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.group.ChannelGroup;
/**
* Handles or intercepts a {@link ChannelEvent}, and sends a
* {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
@ -207,6 +207,11 @@ import io.netty.channel.group.ChannelGroup;
*/
public interface ChannelHandler {
void beforeAdd(ChannelHandlerContext ctx) throws Exception;
void afterAdd(ChannelHandlerContext ctx) throws Exception;
void beforeRemove(ChannelHandlerContext ctx) throws Exception;
void afterRemove(ChannelHandlerContext ctx) throws Exception;
/**
* Indicates that the same instance of the annotated {@link ChannelHandler}
* can be added to one or more {@link ChannelPipeline}s multiple times

View File

@ -0,0 +1,115 @@
package io.netty.channel;
import io.netty.util.internal.QueueFactory;
import java.net.SocketAddress;
import java.util.Queue;
public class ChannelHandlerAdapter<I, O> implements ChannelReader<I>, ChannelWriter<O> {
@Override
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void channelRegistered(ChannelReaderContext<I> ctx) throws Exception {
ctx.next().channelRegistered();
}
@Override
public void channelUnregistered(ChannelReaderContext<I> ctx) throws Exception {
ctx.next().channelUnregistered();
}
@Override
public void channelActive(ChannelReaderContext<I> ctx) throws Exception {
ctx.next().channelActive();
}
@Override
public void channelInactive(ChannelReaderContext<I> ctx) throws Exception {
ctx.next().channelInactive();
}
@Override
public void exceptionCaught(ChannelReaderContext<I> ctx, Throwable cause) throws Exception {
ctx.next().exceptionCaught(cause);
}
@Override
public void userEventTriggered(ChannelReaderContext<I> ctx, Object evt) throws Exception {
ctx.next().userEventTriggered(evt);
}
@Override
@SuppressWarnings("unchecked")
public Queue<I> newReceiveBuffer(ChannelReaderContext<I> ctx) throws Exception {
return (Queue<I>) QueueFactory.createQueue(Object.class);
}
@Override
public void receiveBufferUpdated(ChannelReaderContext<I> ctx) throws Exception {
ctx.in().transferTo(ctx.next().in());
}
@Override
public void receiveBufferClosed(ChannelReaderContext<I> ctx) throws Exception {
ctx.next().in().close();
}
@Override
public void bind(ChannelWriterContext<O> ctx, SocketAddress localAddress, ChannelFuture future) throws Exception {
ctx.next().bind(localAddress, future);
}
@Override
public void connect(ChannelWriterContext<O> ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) throws Exception {
ctx.next().connect(remoteAddress, localAddress, future);
}
@Override
public void disconnect(ChannelWriterContext<O> ctx, ChannelFuture future) throws Exception {
ctx.next().disconnect(future);
}
@Override
public void close(ChannelWriterContext<O> ctx, ChannelFuture future) throws Exception {
ctx.next().close(future);
}
@Override
public void deregister(ChannelWriterContext<O> ctx, ChannelFuture future) throws Exception {
ctx.next().deregister(future);
}
@Override
@SuppressWarnings("unchecked")
public Queue<O> newSendBuffer(ChannelWriterContext<O> ctx) throws Exception {
return (Queue<O>) QueueFactory.createQueue(Object.class);
}
@Override
public void sendBufferUpdated(ChannelWriterContext<O> ctx) throws Exception {
ctx.out().transferTo(ctx.next().out());
}
@Override
public void sendBufferClosed(ChannelWriterContext<O> ctx) throws Exception {
ctx.next().out().close();
}
}

View File

@ -16,6 +16,11 @@
package io.netty.channel;
import io.netty.util.AttributeMap;
import java.net.SocketAddress;
import java.util.Queue;
/**
* Enables a {@link ChannelHandler} to interact with its {@link ChannelPipeline}
* and other handlers. A handler can send a {@link ChannelEvent} upstream or
@ -118,75 +123,34 @@ package io.netty.channel;
* pipeline, and how to handle the event in your application.
* @apiviz.owns io.netty.channel.ChannelHandler
*/
public interface ChannelHandlerContext {
public interface ChannelHandlerContext extends AttributeMap {
/**
* Returns the {@link Channel} that the {@link ChannelPipeline} belongs to.
* This method is a shortcut to <tt>getPipeline().getChannel()</tt>.
*/
Channel getChannel();
String name();
Channel channel();
ChannelReader handler();
NextHandler next();
/**
* Returns the {@link ChannelPipeline} that the {@link ChannelHandler}
* belongs to.
*/
ChannelPipeline getPipeline();
// XXX: What happens if inbound queue is bounded (limited capacity) and it's full?
// 1) EventLoop removes OP_READ
// 2) Once the first inbound buffer is drained to some level, EventLoop adds OP_READ again.
// * To achieve this, EventLoop has to specify a wrapped Queue when calling inboundBufferUpdated.
interface NextHandler {
// For readers
void channelRegistered();
void channelUnregistered();
void channelActive();
void channelInactive();
void exceptionCaught(Throwable cause);
void userEventTriggered(Object event);
Queue<?> in();
/**
* Returns the name of the {@link ChannelHandler} in the
* {@link ChannelPipeline}.
*/
String getName();
/**
* Returns the {@link ChannelHandler} that this context object is
* serving.
*/
ChannelHandler getHandler();
/**
* Returns {@code true} if and only if the {@link ChannelHandler} is an
* instance of {@link ChannelUpstreamHandler}.
*/
boolean canHandleUpstream();
/**
* Returns {@code true} if and only if the {@link ChannelHandler} is an
* instance of {@link ChannelDownstreamHandler}.
*/
boolean canHandleDownstream();
/**
* Sends the specified {@link ChannelEvent} to the
* {@link ChannelUpstreamHandler} which is placed in the closest upstream
* from the handler associated with this context. It is recommended to use
* the shortcut methods in {@link Channels} rather than calling this method
* directly.
*/
void sendUpstream(ChannelEvent e);
/**
* Sends the specified {@link ChannelEvent} to the
* {@link ChannelDownstreamHandler} which is placed in the closest
* downstream from the handler associated with this context. It is
* recommended to use the shortcut methods in {@link Channels} rather than
* calling this method directly.
*/
void sendDownstream(ChannelEvent e);
/**
* Retrieves an object which is {@link #setAttachment(Object) attached} to
* this context.
*
* @return {@code null} if no object was attached or
* {@code null} was attached
*/
Object getAttachment();
/**
* Attaches an object to this context to store a stateful information
* specific to the {@link ChannelHandler} which is associated with this
* context.
*/
void setAttachment(Object attachment);
// For writers
void bind(SocketAddress localAddress, ChannelFuture future);
void connect(SocketAddress remoteAddress, ChannelFuture future);
void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future);
void disconnect(ChannelFuture future);
void close(ChannelFuture future);
void deregister(ChannelFuture future);
Queue<?> out();
}
}

View File

@ -0,0 +1,19 @@
package io.netty.channel;
import java.util.Queue;
public interface ChannelReader<T> extends ChannelHandler {
void channelRegistered(ChannelReaderContext<T> ctx) throws Exception;
void channelUnregistered(ChannelReaderContext<T> ctx) throws Exception;
void channelActive(ChannelReaderContext<T> ctx) throws Exception;
void channelInactive(ChannelReaderContext<T> ctx) throws Exception;
void exceptionCaught(ChannelReaderContext<T> ctx, Throwable cause) throws Exception;
void userEventTriggered(ChannelReaderContext<T> ctx, Object evt) throws Exception;
Queue<T> newReceiveBuffer(ChannelReaderContext<T> ctx) throws Exception;
void receiveBufferUpdated(ChannelReaderContext<T> ctx) throws Exception;
void receiveBufferClosed(ChannelReaderContext<T> ctx) throws Exception;
}

View File

@ -0,0 +1,73 @@
package io.netty.channel;
import io.netty.util.internal.QueueFactory;
import java.util.Queue;
public class ChannelReaderAdapter<T> implements ChannelReader<T> {
@Override
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void channelRegistered(ChannelReaderContext<T> ctx) throws Exception {
ctx.next().channelRegistered();
}
@Override
public void channelUnregistered(ChannelReaderContext<T> ctx) throws Exception {
ctx.next().channelUnregistered();
}
@Override
public void channelActive(ChannelReaderContext<T> ctx) throws Exception {
ctx.next().channelActive();
}
@Override
public void channelInactive(ChannelReaderContext<T> ctx) throws Exception {
ctx.next().channelInactive();
}
@Override
public void exceptionCaught(ChannelReaderContext<T> ctx, Throwable cause) throws Exception {
ctx.next().exceptionCaught(cause);
}
@Override
public void userEventTriggered(ChannelReaderContext<T> ctx, Object evt) throws Exception {
ctx.next().userEventTriggered(evt);
}
@Override
@SuppressWarnings("unchecked")
public Queue<T> newReceiveBuffer(ChannelReaderContext<T> ctx) throws Exception {
return (Queue<T>) QueueFactory.createQueue(Object.class);
}
@Override
public void receiveBufferUpdated(ChannelReaderContext<T> ctx) throws Exception {
ctx.in().transferTo(ctx.next().in());
}
@Override
public void receiveBufferClosed(ChannelReaderContext<T> ctx) throws Exception {
ctx.next().in().close();
}
}

View File

@ -0,0 +1,7 @@
package io.netty.channel;
import java.util.Queue;
public interface ChannelReaderContext<T> extends ChannelHandlerContext {
Queue<T> in();
}

View File

@ -1,88 +0,0 @@
/*
* 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;
/**
* Handles or intercepts an upstream {@link ChannelEvent}, and sends a
* {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
* <p>
* The most common use case of this interface is to intercept an I/O event
* generated by I/O workers to transform the received messages or execute
* the relevant business logic.
*
* <h3>{@link SimpleChannelUpstreamHandler}</h3>
* <p>
* In most cases, you will get to use a {@link SimpleChannelUpstreamHandler} to
* implement an upstream handler because it provides an individual handler
* method for each event type. You might want to implement this interface
* directly though if you want to handle various types of events in more
* generic way.
*
* <h3>Firing an event to the next handler</h3>
* <p>
* You can forward the received event upstream or downstream. In most cases,
* {@link ChannelUpstreamHandler} will send the event upstream (i.e. inbound)
* although it is legal to send the event downstream (i.e. outbound):
*
* <pre>
* // Sending the event upstream (inbound)
* void handleUpstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
* ...
* ctx.sendUpstream(e);
* ...
* }
*
* // Sending the event downstream (outbound)
* void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
* ...
* ctx.sendDownstream(new {@link DownstreamMessageEvent}(...));
* ...
* }
* </pre>
*
* <h4>Using the helper class to send an event</h4>
* <p>
* You will also find various helper methods in {@link Channels} to be useful
* to generate and send an artificial or manipulated event.
*
* <h3>State management</h3>
*
* Please refer to {@link ChannelHandler}.
*
* <h3>Thread safety</h3>
* <p>
* {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream}
* will be invoked sequentially by the same thread (i.e. an I/O thread) and
* therefore a handler does not need to worry about being invoked with a new
* upstream event before the previous upstream event is finished.
* <p>
* This does not necessarily mean that there's a dedicated thread per
* {@link Channel}; the I/O thread of some transport can serve more than one
* {@link Channel} (e.g. NIO transport), while the I/O thread of other
* transports can serve only one (e.g. OIO transport).
*
* @apiviz.exclude ^io\.netty\.handler\..*$
*/
public interface ChannelUpstreamHandler extends ChannelHandler {
/**
* Handles the specified upstream event.
*
* @param ctx the context object for this handler
* @param e the upstream event to process or intercept
*/
void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception;
}

View File

@ -0,0 +1,16 @@
package io.netty.channel;
import java.net.SocketAddress;
import java.util.Queue;
public interface ChannelWriter<T> extends ChannelHandler {
void bind(ChannelWriterContext<T> ctx, SocketAddress localAddress, ChannelFuture future) throws Exception;
void connect(ChannelWriterContext<T> ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) throws Exception;
void disconnect(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception;
void close(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception;
void deregister(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception;
Queue<T> newSendBuffer(ChannelWriterContext<T> ctx) throws Exception;
void sendBufferUpdated(ChannelWriterContext<T> ctx) throws Exception;
void sendBufferClosed(ChannelWriterContext<T> ctx) throws Exception;
}

View File

@ -0,0 +1,69 @@
package io.netty.channel;
import io.netty.util.internal.QueueFactory;
import java.net.SocketAddress;
import java.util.Queue;
public class ChannelWriterAdapter<T> implements ChannelWriter<T> {
@Override
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterAdd(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void beforeRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
// Do nothing by default.
}
@Override
public void bind(ChannelWriterContext<T> ctx, SocketAddress localAddress, ChannelFuture future) throws Exception {
ctx.next().bind(localAddress, future);
}
@Override
public void connect(ChannelWriterContext<T> ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelFuture future) throws Exception {
ctx.next().connect(remoteAddress, localAddress, future);
}
@Override
public void disconnect(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception {
ctx.next().disconnect(future);
}
@Override
public void close(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception {
ctx.next().close(future);
}
@Override
public void deregister(ChannelWriterContext<T> ctx, ChannelFuture future) throws Exception {
ctx.next().deregister(future);
}
@Override
@SuppressWarnings("unchecked")
public Queue<T> newSendBuffer(ChannelWriterContext<T> ctx) throws Exception {
return (Queue<T>) QueueFactory.createQueue(Object.class);
}
@Override
public void sendBufferUpdated(ChannelWriterContext<T> ctx) throws Exception {
ctx.out().transferTo(ctx.next().out());
}
@Override
public void sendBufferClosed(ChannelWriterContext<T> ctx) throws Exception {
ctx.next().out().close();
}
}

View File

@ -0,0 +1,7 @@
package io.netty.channel;
import java.util.Queue;
public interface ChannelWriterContext<T> extends ChannelHandlerContext {
Queue<T> out();
}

View File

@ -1,36 +0,0 @@
/*
* 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;
/**
* A {@link ChannelHandler} that is notified when it is added to or removed
* from a {@link ChannelPipeline}.
*
* <h3>Invalid access to the {@link ChannelHandlerContext}</h3>
*
* Calling {@link ChannelHandlerContext#sendUpstream(ChannelEvent)} or
* {@link ChannelHandlerContext#sendDownstream(ChannelEvent)} in
* {@link #beforeAdd(ChannelHandlerContext)} or {@link #afterRemove(ChannelHandlerContext)}
* might lead to an unexpected behavior. It is because the context object
* might not have been fully added to the pipeline or the context object is not
* a part of the pipeline anymore respectively.
*/
public interface LifeCycleAwareChannelHandler extends ChannelHandler {
void beforeAdd(ChannelHandlerContext ctx) throws Exception;
void afterAdd(ChannelHandlerContext ctx) throws Exception;
void beforeRemove(ChannelHandlerContext ctx) throws Exception;
void afterRemove(ChannelHandlerContext ctx) throws Exception;
}

View File

@ -1,163 +0,0 @@
/*
* 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 java.net.SocketAddress;
/**
* A {@link ChannelDownstreamHandler} which provides an individual handler
* method for each event type. This handler down-casts the received downstream
* event into more meaningful sub-type event and calls an appropriate handler
* method with the down-cast event. The names of the methods starts with the
* name of the operation and ends with {@code "Requested"}
* (e.g. {@link #writeRequested(ChannelHandlerContext, MessageEvent) writeRequested}.)
* <p>
* Please use {@link SimpleChannelHandler} if you need to implement both
* {@link ChannelUpstreamHandler} and {@link ChannelDownstreamHandler}.
*
* <h3>Overriding the {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream} method</h3>
* <p>
* You can override the {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream}
* method just like overriding an ordinary Java method. Please make sure to
* call {@code super.handleDownstream()} so that other handler methods are
* invoked properly:
* </p>
* <pre>public class MyChannelHandler extends {@link SimpleChannelDownstreamHandler} {
*
* {@code @Override}
* public void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
*
* // Log all channel state changes.
* if (e instanceof {@link MessageEvent}) {
* logger.info("Writing:: " + e);
* }
*
* <strong>super.handleDownstream(ctx, e);</strong>
* }
* }</pre>
*
* <p>
* <strong>Caution:</strong>
* <p>
* Use the *Later(..) methods of the {@link Channels} class if you want to send an upstream event from a {@link ChannelDownstreamHandler} otherwise you may run into threading issues.
*
*/
public class SimpleChannelDownstreamHandler implements ChannelDownstreamHandler {
/**
* Creates a new instance.
*/
public SimpleChannelDownstreamHandler() {
}
/**
* {@inheritDoc} Down-casts the received downstream event into more
* meaningful sub-type event and calls an appropriate handler method with
* the down-casted event.
*/
@Override
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
throws Exception {
if (e instanceof MessageEvent) {
writeRequested(ctx, (MessageEvent) e);
} else if (e instanceof ChannelStateEvent) {
ChannelStateEvent evt = (ChannelStateEvent) e;
switch (evt.getState()) {
case OPEN:
if (!Boolean.TRUE.equals(evt.getValue())) {
closeRequested(ctx, evt);
}
break;
case BOUND:
if (evt.getValue() != null) {
bindRequested(ctx, evt);
} else {
unbindRequested(ctx, evt);
}
break;
case CONNECTED:
if (evt.getValue() != null) {
connectRequested(ctx, evt);
} else {
disconnectRequested(ctx, evt);
}
break;
case INTEREST_OPS:
setInterestOpsRequested(ctx, evt);
break;
default:
ctx.sendDownstream(e);
}
} else {
ctx.sendDownstream(e);
}
}
/**
* Invoked when {@link Channel#write(Object)} is called.
*/
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#bind(SocketAddress)} was called.
*/
public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#connect(SocketAddress)} was called.
*/
public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#setInterestOps(int)} was called.
*/
public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#disconnect()} was called.
*/
public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#unbind()} was called.
*/
public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#close()} was called.
*/
public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
}

View File

@ -1,348 +0,0 @@
/*
* 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 java.net.SocketAddress;
import io.netty.buffer.ChannelBuffer;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
/**
* A {@link ChannelHandler} which provides an individual handler method
* for each event type. This handler down-casts the received upstream or
* or downstream event into more meaningful sub-type event and calls an
* appropriate handler method with the down-cast event. For an upstream
* event, the names of the methods are identical to the upstream event names,
* as introduced in the {@link ChannelEvent} documentation. For a
* downstream event, the names of the methods starts with the name of the
* operation and ends with {@code "Requested"}
* (e.g. {@link #writeRequested(ChannelHandlerContext, MessageEvent) writeRequested}.)
* <p>
* Please use {@link SimpleChannelUpstreamHandler} or
* {@link SimpleChannelDownstreamHandler} if you want to intercept only
* upstream or downstream events.
*
* <h3>Overriding the {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream}
* and {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream} method</h3>
* <p>
* You can override the {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream}
* and {@link #handleDownstream(ChannelHandlerContext, ChannelEvent) handleDownstream}
* method just like overriding an ordinary Java method. Please make sure to
* call {@code super.handleUpstream()} or {@code super.handleDownstream()} so
* that other handler methods are invoked properly:
* </p>
* <pre>public class MyChannelHandler extends {@link SimpleChannelHandler} {
*
* {@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);
* }
*
* <strong>super.handleUpstream(ctx, e);</strong>
* }
*
* {@code @Override}
* public void handleDownstream({@link ChannelHandlerContext} ctx, {@link ChannelEvent} e) throws Exception {
*
* // Log all channel state changes.
* if (e instanceof {@link MessageEvent}) {
* logger.info("Writing:: " + e);
* }
*
* <strong>super.handleDownstream(ctx, e);</strong>
* }
* }</pre>
*/
public class SimpleChannelHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler {
private static final InternalLogger logger =
InternalLoggerFactory.getInstance(SimpleChannelHandler.class.getName());
/**
* Creates a new instance.
*/
public SimpleChannelHandler() {
}
/**
* {@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.
*/
public void channelOpen(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} is open and bound to a local address,
* but not connected.
*/
public void channelBound(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} is open, bound to a local address, and
* connected to a remote address.
*/
public void channelConnected(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel}'s {@link Channel#getInterestOps() interestOps}
* was changed.
*/
public void channelInterestChanged(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was disconnected from its remote peer.
*/
public void channelDisconnected(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was unbound from the current local address.
*/
public void channelUnbound(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was closed and all its related resources
* were released.
*/
public void channelClosed(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when something was written into a {@link Channel}.
*/
public void writeComplete(
ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a child {@link Channel} was open.
* (e.g. a server channel accepted a connection)
*/
public void childChannelOpen(
ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a child {@link Channel} was closed.
* (e.g. the accepted connection was closed)
*/
public void childChannelClosed(
ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* {@inheritDoc} Down-casts the received downstream event into more
* meaningful sub-type event and calls an appropriate handler method with
* the down-casted event.
*/
@Override
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
throws Exception {
if (e instanceof MessageEvent) {
writeRequested(ctx, (MessageEvent) e);
} else if (e instanceof ChannelStateEvent) {
ChannelStateEvent evt = (ChannelStateEvent) e;
switch (evt.getState()) {
case OPEN:
if (!Boolean.TRUE.equals(evt.getValue())) {
closeRequested(ctx, evt);
}
break;
case BOUND:
if (evt.getValue() != null) {
bindRequested(ctx, evt);
} else {
unbindRequested(ctx, evt);
}
break;
case CONNECTED:
if (evt.getValue() != null) {
connectRequested(ctx, evt);
} else {
disconnectRequested(ctx, evt);
}
break;
case INTEREST_OPS:
setInterestOpsRequested(ctx, evt);
break;
default:
ctx.sendDownstream(e);
}
} else {
ctx.sendDownstream(e);
}
}
/**
* Invoked when {@link Channel#write(Object)} is called.
*/
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#bind(SocketAddress)} was called.
*/
public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#connect(SocketAddress)} was called.
*/
public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#setInterestOps(int)} was called.
*/
public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#disconnect()} was called.
*/
public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#unbind()} was called.
*/
public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
/**
* Invoked when {@link Channel#close()} was called.
*/
public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendDownstream(e);
}
}

View File

@ -1,235 +0,0 @@
/*
* 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.
* <p>
* Please use {@link SimpleChannelHandler} if you need to implement both
* {@link ChannelUpstreamHandler} and {@link ChannelDownstreamHandler}.
*
* <h3>Overriding the {@link #handleUpstream(ChannelHandlerContext, ChannelEvent) handleUpstream} method</h3>
* <p>
* 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:
* </p>
* <pre>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);
* }
*
* <strong>super.handleUpstream(ctx, e);</strong>
* }
* }</pre>
*/
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.
* <br/>
* <strong>Be aware that this event is fired from within the Boss-Thread so you should not execute any heavy operation in there as it will block the dispatching to other workers!</strong>
*/
public void channelOpen(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} is open and bound to a local address,
* but not connected.
* <br/>
* <strong>Be aware that this event is fired from within the Boss-Thread so you should not execute any heavy operation in there as it will block the dispatching to other workers!</strong>
*/
public void channelBound(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} is open, bound to a local address, and
* connected to a remote address.
*/
public void channelConnected(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel}'s {@link Channel#getInterestOps() interestOps}
* was changed.
*/
public void channelInterestChanged(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was disconnected from its remote peer.
*/
public void channelDisconnected(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was unbound from the current local address.
*/
public void channelUnbound(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a {@link Channel} was closed and all its related resources
* were released.
*/
public void channelClosed(
ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when something was written into a {@link Channel}.
*/
public void writeComplete(
ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a child {@link Channel} was open.
* (e.g. a server channel accepted a connection)
*/
public void childChannelOpen(
ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
/**
* Invoked when a child {@link Channel} was closed.
* (e.g. the accepted connection was closed)
*/
public void childChannelClosed(
ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception {
ctx.sendUpstream(e);
}
}