|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface ChannelHandlerContext
Enables a ChannelHandler to interact with its ChannelPipeline
and other handlers. A handler can send a ChannelEvent upstream or
downstream, modify the ChannelPipeline it belongs to dynamically.
ChannelEvent to the closest handler in the
same ChannelPipeline by calling sendUpstream(ChannelEvent)
or sendDownstream(ChannelEvent). Please refer to
ChannelPipeline to understand how an event flows.
ChannelPipeline your handler belongs to by calling
getPipeline(). A non-trivial application could insert, remove, or
replace handlers in the pipeline dynamically in runtime.
ChannelHandlerContext for later use, such as
triggering an event outside the handler methods, even from a different thread.
public class MyHandler extendsSimpleChannelHandlerimplementsLifeCycleAwareChannelHandler{ privateChannelHandlerContextctx; public void beforeAdd(ChannelHandlerContextctx) { this.ctx = ctx; } public void login(String username, password) {Channels.write( this.ctx,Channels.succeededFuture(this.ctx.getChannel()), new LoginMessage(username, password)); } ... }
setAttachment(Object) and getAttachment() allow you to
store and access stateful information that is related with a handler and its
context. Please refer to ChannelHandler to learn various recommended
ways to manage stateful information.
ChannelHandler instance can be added to more than
one ChannelPipeline. It means a single ChannelHandler
instance can have more than one ChannelHandlerContext and therefore
the single instance can be invoked with different
ChannelHandlerContexts if it is added to one or more
ChannelPipelines more than once.
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:
public class FactorialHandler extendsSimpleChannelHandler{ // This handler will receive a sequence of increasing integers starting // from 1.@Overridepublic void messageReceived(ChannelHandlerContextctx,MessageEventevt) { Integer a = (Integer) ctx.getAttachment(); Integer b = (Integer) evt.getMessage(); if (a == null) { a = 1; } ctx.setAttachment(Integer.valueOf(a * b)); } } // Different context objects are given to "f1", "f2", "f3", and "f4" even if // they refer to the same handler instance. Because the FactorialHandler // stores its state in a context object (as an attachment), the factorial is // calculated correctly 4 times once the two pipelines (p1 and p2) are active. FactorialHandler fh = new FactorialHandler();ChannelPipelinep1 =Channels.pipeline(); p1.addLast("f1", fh); p1.addLast("f2", fh);ChannelPipelinep2 =Channels.pipeline(); p2.addLast("f3", fh); p2.addLast("f4", fh);
Please refer to the ChannelHandler, ChannelEvent, and
ChannelPipeline to find out what a upstream event and a downstream
event are, what fundamental differences they have, how they flow in a
pipeline, and how to handle the event in your application.
| Method Summary | |
|---|---|
boolean |
canHandleDownstream()
Returns true if and only if the ChannelHandler is an
instance of ChannelDownstreamHandler. |
boolean |
canHandleUpstream()
Returns true if and only if the ChannelHandler is an
instance of ChannelUpstreamHandler. |
java.lang.Object |
getAttachment()
Retrieves an object which is attached to
this context. |
Channel |
getChannel()
Returns the Channel that the ChannelPipeline belongs to. |
ChannelHandler |
getHandler()
Returns the ChannelHandler that this context object is
serving. |
java.lang.String |
getName()
Returns the name of the ChannelHandler in the
ChannelPipeline. |
ChannelPipeline |
getPipeline()
Returns the ChannelPipeline that the ChannelHandler
belongs to. |
void |
sendDownstream(ChannelEvent e)
Sends the specified ChannelEvent to the
ChannelDownstreamHandler which is placed in the closest
downstream from the handler associated with this context. |
void |
sendUpstream(ChannelEvent e)
Sends the specified ChannelEvent to the
ChannelUpstreamHandler which is placed in the closest upstream
from the handler associated with this context. |
void |
setAttachment(java.lang.Object attachment)
Attaches an object to this context to store a stateful information specific to the ChannelHandler which is associated with this
context. |
| Method Detail |
|---|
Channel getChannel()
Channel that the ChannelPipeline belongs to.
This method is a shortcut to getPipeline().getChannel().
ChannelPipeline getPipeline()
ChannelPipeline that the ChannelHandler
belongs to.
java.lang.String getName()
ChannelHandler in the
ChannelPipeline.
ChannelHandler getHandler()
ChannelHandler that this context object is
serving.
boolean canHandleUpstream()
true if and only if the ChannelHandler is an
instance of ChannelUpstreamHandler.
boolean canHandleDownstream()
true if and only if the ChannelHandler is an
instance of ChannelDownstreamHandler.
void sendUpstream(ChannelEvent e)
ChannelEvent to the
ChannelUpstreamHandler which is placed in the closest upstream
from the handler associated with this context. It is recommended to use
the shortcut methods in Channels rather than calling this method
directly.
void sendDownstream(ChannelEvent e)
ChannelEvent to the
ChannelDownstreamHandler which is placed in the closest
downstream from the handler associated with this context. It is
recommended to use the shortcut methods in Channels rather than
calling this method directly.
java.lang.Object getAttachment()
attached to
this context.
null if no object was attached or
null was attachedvoid setAttachment(java.lang.Object attachment)
ChannelHandler which is associated with this
context.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||