Class SimpleChannelDuplexHandler<I,O>
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.SimpleChannelInboundHandler<I>
-
- org.drasyl.peer.connection.handler.SimpleChannelDuplexHandler<I,O>
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
,io.netty.channel.ChannelOutboundHandler
- Direct Known Subclasses:
SignatureHandler
,ThreeWayHandshakeClientHandler
,ThreeWayHandshakeServerHandler
public abstract class SimpleChannelDuplexHandler<I,O> extends io.netty.channel.SimpleChannelInboundHandler<I> implements io.netty.channel.ChannelOutboundHandler
ChannelDuplexHandler
which allows to explicit only handle a specific type of messages.For example here is an implementation which only handle
String
messages.public class StringHandler extends
SimpleChannelDuplexHandler
<String
,String
> {@Override
protected void channelRead0(ChannelHandlerContext
ctx,String
message) throwsException
{ System.out.println(message); }@Override
protected void channelWrite0(ChannelHandlerContext
ctx,String
message) throwsException
{ System.out.println(message); } }Be aware that depending of the constructor parameters it will release all handled messages by passing them to
ReferenceCountUtil.release(Object)
and fulfills the correspondingChannelPromise
in thechannelWrite0(io.netty.channel.ChannelHandlerContext, O, io.netty.channel.ChannelPromise)
method. In this case you may need to useReferenceCountUtil.retain(Object)
if you pass the object to the next handler in theChannelPipeline
.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SimpleChannelDuplexHandler()
seeSimpleChannelDuplexHandler(boolean, boolean, boolean)
withtrue
as boolean parameters.protected
SimpleChannelDuplexHandler(boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise)
Create a new instance which will try to detect the types to match out of the types parameter of the class.protected
SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType)
seeSimpleChannelDuplexHandler(Class, Class, boolean, boolean, boolean)
withtrue
as boolean values.protected
SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType, boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise)
Create a new instance
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
acceptOutboundMessage(Object msg)
Returnstrue
if the given message should be handled.void
bind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)
protected abstract void
channelRead0(io.netty.channel.ChannelHandlerContext ctx, I msg)
Is called for each message of typeSimpleChannelDuplexHandler
on inbound channel.protected abstract void
channelWrite0(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise)
Is called for each message of typeSimpleChannelDuplexHandler
on the outbound channel.void
close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
void
connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)
void
deregister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
void
disconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
void
flush(io.netty.channel.ChannelHandlerContext ctx)
void
read(io.netty.channel.ChannelHandlerContext ctx)
void
write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)
-
Methods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelRead
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Constructor Detail
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler()
seeSimpleChannelDuplexHandler(boolean, boolean, boolean)
withtrue
as boolean parameters.
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise)
Create a new instance which will try to detect the types to match out of the types parameter of the class.- Parameters:
inboundAutoRelease
-true
if inbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object)
.outboundAutoRelease
-true
if outbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object)
.autoFulfillPromise
-true
if outboundChannelPromise
should be fulfilled automatically
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType)
seeSimpleChannelDuplexHandler(Class, Class, boolean, boolean, boolean)
withtrue
as boolean values.
-
SimpleChannelDuplexHandler
protected SimpleChannelDuplexHandler(Class<? extends I> inboundMessageType, Class<? extends O> outboundMessageType, boolean inboundAutoRelease, boolean outboundAutoRelease, boolean autoFulfillPromise)
Create a new instance- Parameters:
inboundMessageType
- The type of messages to matchoutboundMessageType
- The type of messages to matchinboundAutoRelease
-true
if inbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object)
.outboundAutoRelease
-true
if outbound handled messages should be released automatically by passing them toReferenceCountUtil.release(Object)
.autoFulfillPromise
-true
if outboundChannelPromise
should be fulfilled automatically
-
-
Method Detail
-
channelRead0
protected abstract void channelRead0(io.netty.channel.ChannelHandlerContext ctx, I msg) throws Exception
Is called for each message of typeSimpleChannelDuplexHandler
on inbound channel.- Specified by:
channelRead0
in classio.netty.channel.SimpleChannelInboundHandler<I>
- Parameters:
ctx
- theChannelHandlerContext
which thisSimpleChannelDuplexHandler
belongs tomsg
- the message to handle- Throws:
Exception
- is thrown if an error occurred
-
bind
public void bind(io.netty.channel.ChannelHandlerContext ctx, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)
- Specified by:
bind
in interfaceio.netty.channel.ChannelOutboundHandler
-
connect
public void connect(io.netty.channel.ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, io.netty.channel.ChannelPromise promise)
- Specified by:
connect
in interfaceio.netty.channel.ChannelOutboundHandler
-
disconnect
public void disconnect(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
- Specified by:
disconnect
in interfaceio.netty.channel.ChannelOutboundHandler
-
close
public void close(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise) throws Exception
- Specified by:
close
in interfaceio.netty.channel.ChannelOutboundHandler
- Throws:
Exception
-
deregister
public void deregister(io.netty.channel.ChannelHandlerContext ctx, io.netty.channel.ChannelPromise promise)
- Specified by:
deregister
in interfaceio.netty.channel.ChannelOutboundHandler
-
read
public void read(io.netty.channel.ChannelHandlerContext ctx)
- Specified by:
read
in interfaceio.netty.channel.ChannelOutboundHandler
-
write
public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception
- Specified by:
write
in interfaceio.netty.channel.ChannelOutboundHandler
- Throws:
Exception
-
acceptOutboundMessage
public boolean acceptOutboundMessage(Object msg)
Returnstrue
if the given message should be handled. Iffalse
it will be passed to the nextChannelOutboundHandler
in theChannelPipeline
.
-
channelWrite0
protected abstract void channelWrite0(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise) throws Exception
Is called for each message of typeSimpleChannelDuplexHandler
on the outbound channel.- Parameters:
ctx
- theChannelHandlerContext
which thisSimpleChannelDuplexHandler
belongs tomsg
- the message to handlepromise
- the corresponding promise- Throws:
Exception
- is thrown if an error occurred
-
flush
public void flush(io.netty.channel.ChannelHandlerContext ctx)
- Specified by:
flush
in interfaceio.netty.channel.ChannelOutboundHandler
-
-