Package i5.las2peer.p2p
Class MessageResultListener
java.lang.Object
i5.las2peer.p2p.MessageResultListener
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.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
a simple message status flag -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
default non blocking timer for waiting -
Constructor Summary
ConstructorDescriptionMessageResultListener
(int timeoutMs) simple constructorMessageResultListener
(long timeoutMs) simple constructorMessageResultListener
(long timeoutMs, long timeoutMoreMs) simple constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
increase the number of recipientsvoid
addRecipients
(int number) increase the number of recipientsboolean
check if this method is times out, call the corresponding notification hook, if the timeout is detected herevoid
collectAnswer
(Message answer) set the result of the message, will cause a notifySuccess() callvoid
set an exception that has been thrown during delivery or processing of the message will cause a notifyException calllong
getAgeMs()
get the age in milliseconds of this listenerget all exceptions which have been registered so farint
int
Message[]
long
boolean
was their any exception during sending or processing the corresponding messageboolean
boolean
boolean
void
notifyException
(Exception exception) notifyException will be called, when an exception has arrived and is stored in this listener.void
NotifiedSuccess will be called, when a result has arrived and is stored in this listener.void
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.void
wait for all expected resultsvoid
waitForAllAnswers
(boolean waitForAll) void
sleep until a result has been receivedvoid
waitForOneAnswer
(int sleepTimeMs) sleep until a result has been received.
-
Field Details
-
DEFAULT_TIMER
public static final int DEFAULT_TIMERdefault 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 resulttimeoutMoreMs
- 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
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
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
get all exceptions which have been registered so far- Returns:
- array with all exception registered up to this moment
-
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
sleep until a result has been received- Throws:
InterruptedException
- If waiting was interrupted
-
waitForOneAnswer
sleep until a result has been received.- Parameters:
sleepTimeMs
- how long to sleep between checks- Throws:
InterruptedException
- If waiting was interrupted
-
waitForAllAnswers
wait for all expected results- Throws:
InterruptedException
- If waiting was interrupted
-
waitForAllAnswers
- 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
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
-