Class MessageResultListener

java.lang.Object
i5.las2peer.p2p.MessageResultListener

public class MessageResultListener extends Object
A MessageResultListener is a simple collector for one result of a message sending operation in a Node. The listener will be called by the node, when the result of the message or any exception in between has arrived. Since a message may be delivered as multi- or broadcast, multiple results (and exceptions) are possible. The original sending part may do something else while waiting for the message result or use the waitForAllAnswers() or waitForOneAnswer() methods. The notify... hook may be used to react directly to events concerning this listener, e.g. in a closure like manner.
  • Field Details

    • DEFAULT_TIMER

      public static final int DEFAULT_TIMER
      default non blocking timer for waiting
      See Also:
  • Constructor Details

    • MessageResultListener

      public MessageResultListener(long timeoutMs)
      simple constructor
      Parameters:
      timeoutMs - A timeout for the result
    • MessageResultListener

      public MessageResultListener(long timeoutMs, long timeoutMoreMs)
      simple constructor
      Parameters:
      timeoutMs - A timeout for the result
      timeoutMoreMs - A timeout after first result
    • MessageResultListener

      public MessageResultListener(int timeoutMs)
      simple constructor
      Parameters:
      timeoutMs - A timeout for the result
  • Method Details

    • addRecipient

      public void addRecipient()
      increase the number of recipients
    • addRecipients

      public void addRecipients(int number)
      increase the number of recipients
      Parameters:
      number - Amount of recipients to add
    • collectException

      public void collectException(Exception e)
      set an exception that has been thrown during delivery or processing of the message will cause a notifyException call
      Parameters:
      e - An exception to collect
    • collectAnswer

      public void collectAnswer(Message answer)
      set the result of the message, will cause a notifySuccess() call
      Parameters:
      answer - A message to collect as answer
    • isFinished

      public boolean isFinished()
      Returns:
      true, if the message processing is finished
    • getExceptions

      public Exception[] getExceptions()
      get all exceptions which have been registered so far
      Returns:
      array with all exception registered up to this moment
    • getResults

      public Message[] getResults()
      Returns:
      array with all collected results
    • isSuccess

      public boolean isSuccess()
      Returns:
      true, if the message resulted in a success
    • isTimedOut

      public boolean isTimedOut()
      Returns:
      true, if the message timed out before delivery
    • getAgeMs

      public long getAgeMs()
      get the age in milliseconds of this listener
      Returns:
      age in milliseconds
    • getTimeoutTime

      public long getTimeoutTime()
    • hasException

      public boolean hasException()
      was their any exception during sending or processing the corresponding message
      Returns:
      true, if any exception occurred
    • getNumberOfResults

      public int getNumberOfResults()
      Returns:
      number of successfully collected results
    • getNumberOfExpectedResults

      public int getNumberOfExpectedResults()
      Returns:
      the number of expected results
    • waitForOneAnswer

      public void waitForOneAnswer() throws InterruptedException
      sleep until a result has been received
      Throws:
      InterruptedException - If waiting was interrupted
    • waitForOneAnswer

      public void waitForOneAnswer(int sleepTimeMs) throws InterruptedException
      sleep until a result has been received.
      Parameters:
      sleepTimeMs - how long to sleep between checks
      Throws:
      InterruptedException - If waiting was interrupted
    • waitForAllAnswers

      public void waitForAllAnswers() throws InterruptedException
      wait for all expected results
      Throws:
      InterruptedException - If waiting was interrupted
    • waitForAllAnswers

      public void waitForAllAnswers(boolean waitForAll) throws InterruptedException
      Parameters:
      waitForAll - waits for all results if false, otherwise
      Throws:
      InterruptedException - If waiting was interrupted
    • checkTimeOut

      public boolean checkTimeOut()
      check if this method is times out, call the corresponding notification hook, if the timeout is detected here
      Returns:
      true, the the message is times out
    • notifySuccess

      public void notifySuccess()
      NotifiedSuccess will be called, when a result has arrived and is stored in this listener. This hook may be overwritten at listener usage or generation e.g. for decoupled implementation of message success observation. Also useful in a closure like generation of a Listener instance
    • notifyException

      public void notifyException(Exception exception)
      notifyException will be called, when an exception has arrived and is stored in this listener. This hook may be overwritten at listener usage or generation e.g. for decoupled implementation of message success observation. Also useful in a closure like generation of a Listener instance
      Parameters:
      exception - An exception that was received
    • notifyTimeout

      public void notifyTimeout()
      notifyTimeout will be called, if the underlying network has decided, that the corresponding message has timed out and passed a corresponding exception to this listener. This hook may be overwritten at listener usage or generation e.g. for decoupled implementation of message success observation. Also useful in a closure like generation of a Listener instance