Class SimpleChannelOutboundHandler<O>

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelOutboundHandlerAdapter
org.drasyl.peer.connection.handler.SimpleChannelOutboundHandler<O>
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelOutboundHandler
Direct Known Subclasses:
RelayableMessageGuard

public abstract class SimpleChannelOutboundHandler<O> extends io.netty.channel.ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapter 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
             SimpleChannelOutboundHandler<String> {

         @Override
         protected void channelWrite0(ChannelHandlerContext ctx, String message)
                 throws Exception {
             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). In this case you may need to use ReferenceCountUtil.retain(Object) if you pass the object to the next handler in the ChannelPipeline.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    see SimpleChannelOutboundHandler(boolean, boolean) with false as boolean parameters.
    protected
    SimpleChannelOutboundHandler(boolean outboundAutoRelease, boolean autoFulfillPromise)
    Create a new instance which will try to detect the types to match out of the type parameter of the class.
    protected
    SimpleChannelOutboundHandler(Class<? extends O> outboundMessageType)
    see SimpleChannelOutboundHandler(Class, boolean, boolean) with false as boolean parameters.
    protected
    SimpleChannelOutboundHandler(Class<? extends O> outboundMessageType, boolean outboundAutoRelease, boolean autoFulfillPromise)
    Create a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the given message should be handled.
    protected abstract void
    channelWrite0(io.netty.channel.ChannelHandlerContext ctx, O msg, io.netty.channel.ChannelPromise promise)
    Is called for each message of type SimpleChannelOutboundHandler on the outbound channel.
    void
    write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)
     

    Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter

    bind, close, connect, deregister, disconnect, flush, read

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.netty.channel.ChannelHandler

    exceptionCaught, handlerAdded, handlerRemoved
  • Constructor Details

    • SimpleChannelOutboundHandler

      protected SimpleChannelOutboundHandler()
      see SimpleChannelOutboundHandler(boolean, boolean) with false as boolean parameters.
    • SimpleChannelOutboundHandler

      protected SimpleChannelOutboundHandler(boolean outboundAutoRelease, boolean autoFulfillPromise)
      Create a new instance which will try to detect the types to match out of the type parameter of the class.
      Parameters:
      outboundAutoRelease - true if handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
      autoFulfillPromise - true if handled promise should be released automatically by calling ChannelPromise.setSuccess().
    • SimpleChannelOutboundHandler

      protected SimpleChannelOutboundHandler(Class<? extends O> outboundMessageType)
      see SimpleChannelOutboundHandler(Class, boolean, boolean) with false as boolean parameters.
    • SimpleChannelOutboundHandler

      protected SimpleChannelOutboundHandler(Class<? extends O> outboundMessageType, boolean outboundAutoRelease, boolean autoFulfillPromise)
      Create a new instance.
      Parameters:
      outboundMessageType - type of messages
      outboundAutoRelease - true if handled messages should be released automatically by passing them to ReferenceCountUtil.release(Object).
      autoFulfillPromise - true if handled promise should be released automatically by calling ChannelPromise.setSuccess().
  • Method Details

    • write

      public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception
      Specified by:
      write in interface io.netty.channel.ChannelOutboundHandler
      Overrides:
      write in class io.netty.channel.ChannelOutboundHandlerAdapter
      Throws:
      Exception
    • acceptOutboundMessage

      public boolean acceptOutboundMessage(Object msg)
      Returns true if the given message should be handled. If false it will be passed to the next ChannelOutboundHandler in the ChannelPipeline.
    • 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 type SimpleChannelOutboundHandler on the outbound channel.
      Parameters:
      ctx - the ChannelHandlerContext which this SimpleChannelDuplexHandler belongs to
      msg - the message to handle
      promise - the corresponding promise
      Throws:
      Exception - is thrown if an error occurred