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 io.netty.channel;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.util.Attribute;
21  import io.netty.util.AttributeKey;
22  import io.netty.util.AttributeMap;
23  import io.netty.util.concurrent.EventExecutor;
24  
25  import java.nio.channels.Channels;
26  
27  /**
28   * Enables a {@link ChannelHandler} to interact with its {@link ChannelPipeline}
29   * and other handlers. Among other things a handler can notify the next {@link ChannelHandler} in the
30   * {@link ChannelPipeline} as well as modify the {@link ChannelPipeline} it belongs to dynamically.
31   *
32   * <h3>Notify</h3>
33   *
34   * You can notify the closest handler in the same {@link ChannelPipeline} by calling one of the various methods
35   * provided here.
36   *
37   * Please refer to {@link ChannelPipeline} to understand how an event flows.
38   *
39   * <h3>Modifying a pipeline</h3>
40   *
41   * You can get the {@link ChannelPipeline} your handler belongs to by calling
42   * {@link #pipeline()}.  A non-trivial application could insert, remove, or
43   * replace handlers in the pipeline dynamically at runtime.
44   *
45   * <h3>Retrieving for later use</h3>
46   *
47   * You can keep the {@link ChannelHandlerContext} for later use, such as
48   * triggering an event outside the handler methods, even from a different thread.
49   * <pre>
50   * public class MyHandler extends {@link ChannelDuplexHandler} {
51   *
52   *     <b>private {@link ChannelHandlerContext} ctx;</b>
53   *
54   *     public void beforeAdd({@link ChannelHandlerContext} ctx) {
55   *         <b>this.ctx = ctx;</b>
56   *     }
57   *
58   *     public void login(String username, password) {
59   *         ctx.write(new LoginMessage(username, password));
60   *     }
61   *     ...
62   * }
63   * </pre>
64   *
65   * <h3>Storing stateful information</h3>
66   *
67   * {@link #attr(AttributeKey)} allow you to
68   * store and access stateful information that is related with a handler and its
69   * context.  Please refer to {@link ChannelHandler} to learn various recommended
70   * ways to manage stateful information.
71   *
72   * <h3>A handler can have more than one context</h3>
73   *
74   * Please note that a {@link ChannelHandler} instance can be added to more than
75   * one {@link ChannelPipeline}.  It means a single {@link ChannelHandler}
76   * instance can have more than one {@link ChannelHandlerContext} and therefore
77   * the single instance can be invoked with different
78   * {@link ChannelHandlerContext}s if it is added to one or more
79   * {@link ChannelPipeline}s more than once.
80   * <p>
81   * For example, the following handler will have as many independent {@link AttributeKey}s
82   * as how many times it is added to pipelines, regardless if it is added to the
83   * same pipeline multiple times or added to different pipelines multiple times:
84   * <pre>
85   * public class FactorialHandler extends {@link ChannelInboundHandlerAdapter} {
86   *
87   *   private final {@link AttributeKey}&lt;{@link Integer}&gt; counter = {@link AttributeKey}.valueOf("counter");
88   *
89   *   // This handler will receive a sequence of increasing integers starting
90   *   // from 1.
91   *   {@code @Override}
92   *   public void channelRead({@link ChannelHandlerContext} ctx, Object msg) {
93   *     Integer a = ctx.attr(counter).get();
94   *
95   *     if (a == null) {
96   *       a = 1;
97   *     }
98   *
99   *     attr.set(a * (Integer) msg);
100  *   }
101  * }
102  *
103  * // Different context objects are given to "f1", "f2", "f3", and "f4" even if
104  * // they refer to the same handler instance.  Because the FactorialHandler
105  * // stores its state in a context object (using an {@link AttributeKey}), the factorial is
106  * // calculated correctly 4 times once the two pipelines (p1 and p2) are active.
107  * FactorialHandler fh = new FactorialHandler();
108  *
109  * {@link ChannelPipeline} p1 = {@link Channels}.pipeline();
110  * p1.addLast("f1", fh);
111  * p1.addLast("f2", fh);
112  *
113  * {@link ChannelPipeline} p2 = {@link Channels}.pipeline();
114  * p2.addLast("f3", fh);
115  * p2.addLast("f4", fh);
116  * </pre>
117  *
118  * <h3>Additional resources worth reading</h3>
119  * <p>
120  * Please refer to the {@link ChannelHandler}, and
121  * {@link ChannelPipeline} to find out more about inbound and outbound operations,
122  * what fundamental differences they have, how they flow in a  pipeline,  and how to handle
123  * the operation in your application.
124  */
125 public interface ChannelHandlerContext extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker {
126 
127     /**
128      * Return the {@link Channel} which is bound to the {@link ChannelHandlerContext}.
129      */
130     Channel channel();
131 
132     /**
133      * Returns the {@link EventExecutor} which is used to execute an arbitrary task.
134      */
135     EventExecutor executor();
136 
137     /**
138      * The unique name of the {@link ChannelHandlerContext}.The name was used when then {@link ChannelHandler}
139      * was added to the {@link ChannelPipeline}. This name can also be used to access the registered
140      * {@link ChannelHandler} from the {@link ChannelPipeline}.
141      */
142     String name();
143 
144     /**
145      * The {@link ChannelHandler} that is bound this {@link ChannelHandlerContext}.
146      */
147     ChannelHandler handler();
148 
149     /**
150      * Return {@code true} if the {@link ChannelHandler} which belongs to this context was removed
151      * from the {@link ChannelPipeline}. Note that this method is only meant to be called from with in the
152      * {@link EventLoop}.
153      */
154     boolean isRemoved();
155 
156     @Override
157     ChannelHandlerContext fireChannelRegistered();
158 
159     @Override
160     ChannelHandlerContext fireChannelUnregistered();
161 
162     @Override
163     ChannelHandlerContext fireChannelActive();
164 
165     @Override
166     ChannelHandlerContext fireChannelInactive();
167 
168     @Override
169     ChannelHandlerContext fireExceptionCaught(Throwable cause);
170 
171     @Override
172     ChannelHandlerContext fireUserEventTriggered(Object evt);
173 
174     @Override
175     ChannelHandlerContext fireChannelRead(Object msg);
176 
177     @Override
178     ChannelHandlerContext fireChannelReadComplete();
179 
180     @Override
181     ChannelHandlerContext fireChannelWritabilityChanged();
182 
183     @Override
184     ChannelHandlerContext read();
185 
186     @Override
187     ChannelHandlerContext flush();
188 
189     /**
190      * Return the assigned {@link ChannelPipeline}
191      */
192     ChannelPipeline pipeline();
193 
194     /**
195      * Return the assigned {@link ByteBufAllocator} which will be used to allocate {@link ByteBuf}s.
196      */
197     ByteBufAllocator alloc();
198 
199     /**
200      * @deprecated Use {@link Channel#attr(AttributeKey)}
201      */
202     @Deprecated
203     @Override
204     <T> Attribute<T> attr(AttributeKey<T> key);
205 
206     /**
207      * @deprecated Use {@link Channel#hasAttr(AttributeKey)}
208      */
209     @Deprecated
210     @Override
211     <T> boolean hasAttr(AttributeKey<T> key);
212 }