netty5/transport/src/main/java/io/netty/channel/ChannelHandlerContext.java

159 lines
5.8 KiB
Java
Raw Normal View History

/*
2012-06-04 22:31:44 +02:00
* Copyright 2012 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:
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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
* 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.
*/
2011-12-09 04:38:59 +01:00
package io.netty.channel;
2009-09-10 06:25:05 +02:00
import io.netty.buffer.ChannelBuffer;
import io.netty.util.AttributeMap;
import java.nio.channels.Channels;
import java.util.Queue;
import java.util.Set;
/**
* Enables a {@link ChannelHandler} to interact with its {@link ChannelPipeline}
* and other handlers. A handler can send a {@link ChannelEvent} upstream or
* downstream, modify the {@link ChannelPipeline} it belongs to dynamically.
2010-02-17 09:22:45 +01:00
*
* <h3>Sending an event</h3>
*
* You can send or forward a {@link ChannelEvent} to the closest handler in the
* same {@link ChannelPipeline} by calling {@link #sendUpstream(ChannelEvent)}
* or {@link #sendDownstream(ChannelEvent)}. Please refer to
* {@link ChannelPipeline} to understand how an event flows.
*
* <h3>Modifying a pipeline</h3>
*
* You can get the {@link ChannelPipeline} your handler belongs to by calling
* {@link #getPipeline()}. A non-trivial application could insert, remove, or
* replace handlers in the pipeline dynamically in runtime.
*
* <h3>Retrieving for later use</h3>
*
* You can keep the {@link ChannelHandlerContext} for later use, such as
* triggering an event outside the handler methods, even from a different thread.
* <pre>
2010-02-17 09:22:45 +01:00
* public class MyHandler extends {@link SimpleChannelHandler}
* implements {@link LifeCycleAwareChannelHandler} {
*
* <b>private {@link ChannelHandlerContext} ctx;</b>
2010-02-17 09:22:45 +01:00
*
* public void beforeAdd({@link ChannelHandlerContext} ctx) {
* <b>this.ctx = ctx;</b>
2010-02-17 09:22:45 +01:00
* }
*
* public void login(String username, password) {
* {@link Channels}.write(
* <b>this.ctx</b>,
* {@link Channels}.succeededFuture(<b>this.ctx.getChannel()</b>),
* new LoginMessage(username, password));
2010-02-17 09:22:45 +01:00
* }
* ...
* }
2009-04-28 15:35:55 +02:00
* </pre>
2010-02-17 09:22:45 +01:00
*
2010-02-17 09:28:45 +01:00
* <h3>Storing stateful information</h3>
*
* {@link #setAttachment(Object)} and {@link #getAttachment()} allow you to
* store and access stateful information that is related with a handler and its
* context. Please refer to {@link ChannelHandler} to learn various recommended
* ways to manage stateful information.
*
2010-02-17 09:22:45 +01:00
* <h3>A handler can have more than one context</h3>
*
2009-04-28 15:35:55 +02:00
* Please note that a {@link ChannelHandler} instance can be added to more than
* one {@link ChannelPipeline}. It means a single {@link ChannelHandler}
* instance can have more than one {@link ChannelHandlerContext} and therefore
2009-06-17 11:13:10 +02:00
* the single instance can be invoked with different
* {@link ChannelHandlerContext}s if it is added to one or more
* {@link ChannelPipeline}s more than once.
* <p>
* For example, the following handler will have as many independent attachments
* as how many times it is added to pipelines, regardless if it is added to the
* same pipeline multiple times or added to different pipelines multiple times:
2009-04-28 15:35:55 +02:00
* <pre>
2010-02-02 03:00:04 +01:00
* public class FactorialHandler extends {@link SimpleChannelHandler} {
*
* // This handler will receive a sequence of increasing integers starting
* // from 1.
2010-02-02 03:00:04 +01:00
* {@code @Override}
* public void messageReceived({@link ChannelHandlerContext} ctx, {@link MessageEvent} evt) {
2009-04-28 15:35:55 +02:00
* Integer a = (Integer) ctx.getAttachment();
* Integer b = (Integer) evt.getMessage();
*
* if (a == null) {
* a = 1;
2009-04-28 15:35:55 +02:00
* }
*
* ctx.setAttachment(Integer.valueOf(a * b));
2009-04-28 15:35:55 +02:00
* }
* }
*
* // Different context objects are given to "f1", "f2", "f3", and "f4" even if
2009-04-28 16:41:48 +02:00
* // they refer to the same handler instance. Because the FactorialHandler
* // stores its state in a context object (as an attachment), the factorial is
2009-04-28 16:43:20 +02:00
* // calculated correctly 4 times once the two pipelines (p1 and p2) are active.
* FactorialHandler fh = new FactorialHandler();
*
2010-02-02 03:00:04 +01:00
* {@link ChannelPipeline} p1 = {@link Channels}.pipeline();
* p1.addLast("f1", fh);
* p1.addLast("f2", fh);
*
2010-02-02 03:00:04 +01:00
* {@link ChannelPipeline} p2 = {@link Channels}.pipeline();
* p2.addLast("f3", fh);
* p2.addLast("f4", fh);
* </pre>
*
2009-06-17 11:13:10 +02:00
* <h3>Additional resources worth reading</h3>
* <p>
* Please refer to the {@link ChannelHandler}, {@link ChannelEvent}, and
* {@link ChannelPipeline} to find out what a upstream event and a downstream
2009-10-30 08:51:33 +01:00
* event are, what fundamental differences they have, how they flow in a
* pipeline, and how to handle the event in your application.
2011-12-09 04:38:59 +01:00
* @apiviz.owns io.netty.channel.ChannelHandler
*/
public interface ChannelHandlerContext
extends AttributeMap, ChannelFutureFactory,
ChannelInboundInvoker, ChannelOutboundInvoker {
Channel channel();
ChannelPipeline pipeline();
EventExecutor executor();
String name();
ChannelHandler handler();
Set<ChannelHandlerType> type();
boolean hasInboundByteBuffer();
boolean hasInboundMessageBuffer();
ChannelBuffer inboundByteBuffer();
<T> Queue<T> inboundMessageBuffer();
boolean hasOutboundByteBuffer();
boolean hasOutboundMessageBuffer();
ChannelBuffer outboundByteBuffer();
<T> Queue<T> outboundMessageBuffer();
boolean hasNextInboundByteBuffer();
boolean hasNextInboundMessageBuffer();
ChannelBuffer nextInboundByteBuffer();
Queue<Object> nextInboundMessageBuffer();
boolean hasNextOutboundByteBuffer();
boolean hasNextOutboundMessageBuffer();
ChannelBuffer nextOutboundByteBuffer();
Queue<Object> nextOutboundMessageBuffer();
2009-08-28 09:15:49 +02:00
}