Class StopAndWaitArqHandler

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.ChannelDuplexHandler
org.drasyl.handler.arq.stopandwait.StopAndWaitArqHandler
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, io.netty.channel.ChannelOutboundHandler

public class StopAndWaitArqHandler extends io.netty.channel.ChannelDuplexHandler
Performs the Stop-and-wait ARQ protocol.

It also updates the writability of the associated Channel, so that the pending write operations are also considered to determine the writability.

This handler changes the behavior of the Promises returned by ChannelOutboundInvoker.write(Object): The promise is not complemented until the message's recipient has acknowledged arrival of the message. If you abort the promise, this handler will discard the message.

Keep in mind that this ARQ protocol is originally intended to be used in connection-oriented communication. Therefore, it may happen (e.g. due to a restart of one of the two peers) that the expected sequence number of the next message is no longer synchronized. This leads to the loss of the first message. To avoid this, a NOOP message should be sent once at the beginning of the communication.

This handler should be used together with ByteToStopAndWaitArqDataCodec and StopAndWaitArqCodec.

  ChannelPipeline p = ...;
  ...
  p.addLast("arq_codec", new StopAndWaitArqCodec());
  p.addLast("arq_handler", new StopAndWaitArqHandler(500, 100));
  p.addLast("buf_codec", new ByteToStopAndWaitArqDataCodec());
  ...
  p.addLast("handler", new HttpRequestHandler());
  
  • Nested Class Summary

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

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

    Constructors
    Constructor
    Description
    StopAndWaitArqHandler(int retryTimeout)
     
    StopAndWaitArqHandler(int retryTimeout, boolean expectedInboundSequenceNo)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)
     
    void
    flush(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    setExpectedInboundSequenceNo(boolean expectedInboundSequenceNo)
    Sets the sequence of the next inbound StopAndWaitArqData message.
    void
    setRetryTimeout(int retryTimeout)
    Defines how long to wait for a StopAndWaitArqAck message before resending it.
    void
    write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)
     

    Methods inherited from class io.netty.channel.ChannelDuplexHandler

    bind, close, connect, deregister, disconnect, read

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelActive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, 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

    handlerRemoved
  • Constructor Details

  • Method Details

    • handlerAdded

      public void handlerAdded(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      handlerAdded in interface io.netty.channel.ChannelHandler
      Overrides:
      handlerAdded in class io.netty.channel.ChannelHandlerAdapter
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx)
      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
    • channelRead

      public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)
      Specified by:
      channelRead in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelRead in class io.netty.channel.ChannelInboundHandlerAdapter
    • write

      public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise)
      Specified by:
      write in interface io.netty.channel.ChannelOutboundHandler
      Overrides:
      write in class io.netty.channel.ChannelDuplexHandler
    • flush

      public void flush(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      flush in interface io.netty.channel.ChannelOutboundHandler
      Overrides:
      flush in class io.netty.channel.ChannelDuplexHandler
      Throws:
      Exception
    • setRetryTimeout

      public void setRetryTimeout(int retryTimeout)
      Defines how long to wait for a StopAndWaitArqAck message before resending it. StopAndWaitArqData message is sent again. This timeout should not be smaller than the round trip time of the connection.
      Parameters:
      retryTimeout - time in ms before the StopAndWaitArqData message is sent again
    • setExpectedInboundSequenceNo

      public void setExpectedInboundSequenceNo(boolean expectedInboundSequenceNo)
      Sets the sequence of the next inbound StopAndWaitArqData message. This method can be used to manually synchronize the sequence number of the sender and receiver.
      Parameters:
      expectedInboundSequenceNo - expected sequence number of the next inbound StopAndWaitArqData message