2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 The Netty Project
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-08-08 03:27:24 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.channel;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2012-04-12 10:39:01 +02:00
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
|
import io.netty.channel.group.ChannelGroup;
|
|
|
|
|
2010-02-02 01:38:07 +01:00
|
|
|
import java.lang.annotation.Documented;
|
|
|
|
import java.lang.annotation.ElementType;
|
|
|
|
import java.lang.annotation.Inherited;
|
|
|
|
import java.lang.annotation.Retention;
|
|
|
|
import java.lang.annotation.RetentionPolicy;
|
|
|
|
import java.lang.annotation.Target;
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
/**
|
2008-09-24 11:48:32 +02:00
|
|
|
* Handles or intercepts a {@link ChannelEvent}, and sends a
|
2009-09-03 06:33:15 +02:00
|
|
|
* {@link ChannelEvent} to the next handler in a {@link ChannelPipeline}.
|
2008-09-02 16:12:56 +02:00
|
|
|
*
|
|
|
|
* <h3>Sub-types</h3>
|
2008-09-01 16:29:17 +02:00
|
|
|
* <p>
|
2009-04-28 15:35:55 +02:00
|
|
|
* {@link ChannelHandler} itself does not provide any method. To handle a
|
|
|
|
* {@link ChannelEvent} you need to implement its sub-interfaces. There are
|
|
|
|
* two sub-interfaces which handles a received event, one for upstream events
|
|
|
|
* and the other for downstream events:
|
2008-09-01 16:29:17 +02:00
|
|
|
* <ul>
|
2008-11-14 09:02:42 +01:00
|
|
|
* <li>{@link ChannelUpstreamHandler} handles and intercepts an upstream {@link ChannelEvent}.</li>
|
2008-09-02 16:12:56 +02:00
|
|
|
* <li>{@link ChannelDownstreamHandler} handles and intercepts a downstream {@link ChannelEvent}.</li>
|
2008-09-01 16:29:17 +02:00
|
|
|
* </ul>
|
2008-09-02 16:12:56 +02:00
|
|
|
*
|
2008-09-03 05:46:54 +02:00
|
|
|
* You will also find more detailed explanation from the documentation of
|
2009-04-28 15:35:55 +02:00
|
|
|
* each sub-interface on how an event is interpreted when it goes upstream and
|
2008-09-03 05:46:54 +02:00
|
|
|
* downstream respectively.
|
|
|
|
*
|
2008-09-02 16:12:56 +02:00
|
|
|
* <h3>The context object</h3>
|
2008-09-02 14:04:04 +02:00
|
|
|
* <p>
|
|
|
|
* A {@link ChannelHandler} is provided with a {@link ChannelHandlerContext}
|
2009-06-17 11:13:10 +02:00
|
|
|
* object. A {@link ChannelHandler} is supposed to interact with the
|
|
|
|
* {@link ChannelPipeline} it belongs to via a context object. Using the
|
2009-09-03 06:33:15 +02:00
|
|
|
* context object, the {@link ChannelHandler} can pass events upstream or
|
2010-02-17 09:22:45 +01:00
|
|
|
* downstream, modify the pipeline dynamically, or store the information
|
2009-09-03 06:33:15 +02:00
|
|
|
* (attachment) which is specific to the handler.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2010-02-01 12:46:09 +01:00
|
|
|
* <h3>State management</h3>
|
|
|
|
*
|
|
|
|
* A {@link ChannelHandler} often needs to store some stateful information.
|
|
|
|
* The simplest and recommended approach is to use member variables:
|
|
|
|
* <pre>
|
2010-02-02 03:00:04 +01:00
|
|
|
* public class DataServerHandler extends {@link SimpleChannelHandler} {
|
2010-02-01 12:46:09 +01:00
|
|
|
*
|
|
|
|
* <b>private boolean loggedIn;</b>
|
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@code @Override}
|
|
|
|
* public void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
|
|
|
|
* {@link Channel} ch = e.getChannel();
|
2010-02-01 12:46:09 +01:00
|
|
|
* Object o = e.getMessage();
|
|
|
|
* if (o instanceof LoginMessage) {
|
|
|
|
* authenticate((LoginMessage) o);
|
|
|
|
* <b>loggedIn = true;</b>
|
|
|
|
* } else (o instanceof GetDataMessage) {
|
|
|
|
* if (<b>loggedIn</b>) {
|
|
|
|
* ch.write(fetchSecret((GetDataMessage) o));
|
|
|
|
* } else {
|
|
|
|
* fail();
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
* Because the handler instance has a state variable which is dedicated to
|
|
|
|
* one connection, you have to create a new handler instance for each new
|
|
|
|
* channel to avoid a race condition where a unauthenticated client can get
|
|
|
|
* the confidential information:
|
|
|
|
* <pre>
|
|
|
|
* // Create a new handler instance per channel.
|
|
|
|
* // See {@link Bootstrap#setPipelineFactory(ChannelPipelineFactory)}.
|
2010-02-02 03:00:04 +01:00
|
|
|
* public class DataServerPipelineFactory implements {@link ChannelPipelineFactory} {
|
|
|
|
* public {@link ChannelPipeline} getPipeline() {
|
|
|
|
* return {@link Channels}.pipeline(<b>new DataServerHandler()</b>);
|
2010-02-01 12:46:09 +01:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <h4>Using an attachment</h4>
|
|
|
|
*
|
|
|
|
* Although it's recommended to use member variables to store the state of a
|
|
|
|
* handler, for some reason you might not want to create many handler instances.
|
|
|
|
* In such a case, you can use an <em>attachment</em> which is provided by
|
|
|
|
* {@link ChannelHandlerContext}:
|
|
|
|
* <pre>
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@code @Sharable}
|
|
|
|
* public class DataServerHandler extends {@link SimpleChannelHandler} {
|
2010-02-01 12:46:09 +01:00
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@code @Override}
|
|
|
|
* public void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
|
|
|
|
* {@link Channel} ch = e.getChannel();
|
2010-02-01 12:46:09 +01:00
|
|
|
* Object o = e.getMessage();
|
|
|
|
* if (o instanceof LoginMessage) {
|
|
|
|
* authenticate((LoginMessage) o);
|
|
|
|
* <b>ctx.setAttachment(true)</b>;
|
|
|
|
* } else (o instanceof GetDataMessage) {
|
|
|
|
* if (<b>Boolean.TRUE.equals(ctx.getAttachment())</b>) {
|
|
|
|
* ch.write(fetchSecret((GetDataMessage) o));
|
|
|
|
* } else {
|
|
|
|
* fail();
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
* Now that the state of the handler is stored as an attachment, you can add the
|
|
|
|
* same handler instance to different pipelines:
|
|
|
|
* <pre>
|
2010-02-02 03:00:04 +01:00
|
|
|
* public class DataServerPipelineFactory implements {@link ChannelPipelineFactory} {
|
|
|
|
*
|
|
|
|
* private static final DataServerHandler <b>SHARED</b> = new DataServerHandler();
|
|
|
|
*
|
|
|
|
* public {@link ChannelPipeline} getPipeline() {
|
|
|
|
* return {@link Channels}.pipeline(<b>SHARED</b>);
|
2010-02-01 12:46:09 +01:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <h4>Using a {@link ChannelLocal}</h4>
|
|
|
|
*
|
|
|
|
* If you have a state variable which needs to be accessed either from other
|
|
|
|
* handlers or outside handlers, you can use {@link ChannelLocal}:
|
|
|
|
* <pre>
|
|
|
|
* public final class DataServerState {
|
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* <b>public static final {@link ChannelLocal}<Boolean> loggedIn = new {@link ChannelLocal}<Boolean>() {
|
2010-02-01 12:46:09 +01:00
|
|
|
* protected Boolean initialValue(Channel channel) {
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
* }</b>
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@code @Sharable}
|
|
|
|
* public class DataServerHandler extends {@link SimpleChannelHandler} {
|
|
|
|
*
|
|
|
|
* {@code @Override}
|
|
|
|
* public void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} e) {
|
2010-02-01 12:46:09 +01:00
|
|
|
* Channel ch = e.getChannel();
|
|
|
|
* Object o = e.getMessage();
|
|
|
|
* if (o instanceof LoginMessage) {
|
|
|
|
* authenticate((LoginMessage) o);
|
|
|
|
* <b>DataServerState.loggedIn.set(ch, true);</b>
|
|
|
|
* } else (o instanceof GetDataMessage) {
|
|
|
|
* if (<b>DataServerState.loggedIn.get(ch)</b>) {
|
|
|
|
* ctx.getChannel().write(fetchSecret((GetDataMessage) o));
|
|
|
|
* } else {
|
|
|
|
* fail();
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Print the remote addresses of the authenticated clients:
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@link ChannelGroup} allClientChannels = ...;
|
|
|
|
* for ({@link Channel} ch: allClientChannels) {
|
2010-02-01 12:46:09 +01:00
|
|
|
* if (<b>DataServerState.loggedIn.get(ch)</b>) {
|
|
|
|
* System.out.println(ch.getRemoteAddress());
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
2010-02-02 03:00:04 +01:00
|
|
|
* <h4>The {@code @Sharable} annotation</h4>
|
2010-02-02 01:38:07 +01:00
|
|
|
* <p>
|
|
|
|
* In the examples above which used an attachment or a {@link ChannelLocal},
|
2010-02-02 03:00:04 +01:00
|
|
|
* you might have noticed the {@code @Sharable} annotation.
|
2010-02-02 01:38:07 +01:00
|
|
|
* <p>
|
2010-02-02 03:00:04 +01:00
|
|
|
* If a {@link ChannelHandler} is annotated with the {@code @Sharable}
|
2010-02-02 01:38:07 +01:00
|
|
|
* annotation, it means you can create an instance of the handler just once and
|
|
|
|
* add it to one or more {@link ChannelPipeline}s multiple times without
|
|
|
|
* a race condition.
|
|
|
|
* <p>
|
2010-02-02 03:00:04 +01:00
|
|
|
* If this annotation is not specified, you have to create a new handler
|
2010-02-02 01:38:07 +01:00
|
|
|
* instance every time you add it to a pipeline because it has unshared state
|
|
|
|
* such as member variables.
|
|
|
|
* <p>
|
|
|
|
* This annotation is provided for documentation purpose, just like
|
|
|
|
* <a href="http://www.javaconcurrencyinpractice.com/annotations/doc/">the JCIP annotations</a>.
|
|
|
|
*
|
2008-09-02 16:12:56 +02:00
|
|
|
* <h3>Additional resources worth reading</h3>
|
|
|
|
* <p>
|
2009-06-17 11:13:10 +02:00
|
|
|
* Please refer to the {@link ChannelEvent} and {@link ChannelPipeline} to find
|
|
|
|
* out what a upstream event and a downstream event are, what fundamental
|
|
|
|
* differences they have, and how they flow in a pipeline.
|
2008-09-05 12:58:37 +02:00
|
|
|
* @apiviz.landmark
|
2011-12-09 05:59:41 +01:00
|
|
|
* @apiviz.exclude ^io\.netty\.handler\..*$
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
|
|
|
public interface ChannelHandler {
|
2010-02-02 01:38:07 +01:00
|
|
|
|
2012-04-12 10:39:01 +02:00
|
|
|
void beforeAdd(ChannelHandlerContext ctx) throws Exception;
|
|
|
|
void afterAdd(ChannelHandlerContext ctx) throws Exception;
|
|
|
|
void beforeRemove(ChannelHandlerContext ctx) throws Exception;
|
|
|
|
void afterRemove(ChannelHandlerContext ctx) throws Exception;
|
|
|
|
|
2010-02-02 01:38:07 +01:00
|
|
|
/**
|
|
|
|
* Indicates that the same instance of the annotated {@link ChannelHandler}
|
|
|
|
* can be added to one or more {@link ChannelPipeline}s multiple times
|
|
|
|
* without a race condition.
|
|
|
|
* <p>
|
2010-02-02 03:00:04 +01:00
|
|
|
* If this annotation is not specified, you have to create a new handler
|
2010-02-02 01:38:07 +01:00
|
|
|
* instance every time you add it to a pipeline because it has unshared
|
|
|
|
* state such as member variables.
|
|
|
|
* <p>
|
|
|
|
* This annotation is provided for documentation purpose, just like
|
|
|
|
* <a href="http://www.javaconcurrencyinpractice.com/annotations/doc/">the JCIP annotations</a>.
|
2012-01-13 09:41:18 +01:00
|
|
|
*/
|
2010-02-02 01:38:07 +01:00
|
|
|
@Inherited
|
|
|
|
@Documented
|
|
|
|
@Target(ElementType.TYPE)
|
|
|
|
@Retention(RetentionPolicy.RUNTIME)
|
2011-11-06 17:54:21 +01:00
|
|
|
@interface Sharable {
|
2010-02-02 01:38:07 +01:00
|
|
|
// no value
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|