LimeWire Consolidated API

org.limewire.mojito.handler.response
Class AbstractResponseHandler<V extends Result>

java.lang.Object
  extended by org.limewire.mojito.handler.response.AbstractResponseHandler<V>
All Implemented Interfaces:
DHTTask<V>, ResponseHandler
Direct Known Subclasses:
GetValueResponseHandler, LookupResponseHandler, PingResponseHandler, StatsResponseHandler, StoreResponseHandler

public abstract class AbstractResponseHandler<V extends Result>
extends Object
implements ResponseHandler, DHTTask<V>

An abstract base class for ResponseHandlers


Field Summary
protected  Context context
          A handle to Context
protected  long lastResponseTime
          The time of the last response we received
 
Constructor Summary
AbstractResponseHandler(Context context)
           
AbstractResponseHandler(Context context, int maxErrors)
           
AbstractResponseHandler(Context context, long timeout)
           
AbstractResponseHandler(Context context, long timeout, int maxErrors)
           
 
Method Summary
 void cancel()
          Cancels the DHTTask
protected  DHTTimeoutException createTimeoutException(KUID nodeId, SocketAddress address, RequestMessage request, long time)
          A helper method to create Timeout Exceptions
protected abstract  void error(KUID nodeId, SocketAddress dst, RequestMessage message, IOException e)
          See handleError()
protected  void fireTimeoutException(KUID nodeId, SocketAddress address, RequestMessage request, long time)
          A helper method to throw Timeout Exceptions
protected  long getElapsedTime()
          Returns the total time that has elapsed since this ResponseHandler is active.
protected  int getErrors()
          Returns the number of errors that have occurred
protected  long getLastResponseTime()
          Returns the time when the last response was received
protected  Object getLock()
          Returns the lock Object for the ResponseHandler.
 int getMaxErrors()
          Returns the maximum number of errors that may occur before we're giving up to re-send a request
 long getTimeout()
          Returns the timeout (in milliseconds) of this handler.
 long getWaitOnLockTimeout()
          The maximum time we're waiting on a lock.
 void handleError(KUID nodeId, SocketAddress dst, RequestMessage message, IOException e)
          Called if an error occurred in handleResponse() or handleTimeout()
 void handleResponse(ResponseMessage response, long time)
          Called to handle a response
 void handleTick()
          Called in periodic intervals while we're waiting for a response.
 void handleTimeout(KUID nodeId, SocketAddress dst, RequestMessage request, long time)
          Called after the timeout time has elapsed and no response has arrived
 boolean isCancelled()
          Returns whether or not this handler has been cancelled
 boolean isDone()
          Returns whether or not this handler is done which means if it has returned a result or threw an Exception
protected  void resend(KUID nodeId, SocketAddress dst, RequestMessage message)
          Resends the given Message to nodeId/dst
protected  void resetErrors()
          Resets the error counter
protected abstract  void response(ResponseMessage message, long time)
          See handleResponse()
protected  void setException(DHTException ex)
          Sets the Exception which will be thrown by the call() method
 void setMaxErrors(int maxErrors)
          Sets the maximum number of errors that may occur before we're giving up to re-send a request
protected  void setReturnValue(V value)
          Sets the return value which will be returned by the call() method
 void setTimeout(long timeout)
          Sets the timeout of this handler
protected abstract  void start()
          Override this method to start the ResponseHandler
 void start(OnewayExchanger<V,ExecutionException> exchanger)
          Starts the DHTTask
protected  void tick()
          See handleTick()
protected abstract  void timeout(KUID nodeId, SocketAddress dst, RequestMessage message, long time)
          See handleTimeout()
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected final Context context
A handle to Context


lastResponseTime

protected long lastResponseTime
The time of the last response we received

Constructor Detail

AbstractResponseHandler

public AbstractResponseHandler(Context context)

AbstractResponseHandler

public AbstractResponseHandler(Context context,
                               long timeout)

AbstractResponseHandler

public AbstractResponseHandler(Context context,
                               int maxErrors)

AbstractResponseHandler

public AbstractResponseHandler(Context context,
                               long timeout,
                               int maxErrors)
Method Detail

getLock

protected Object getLock()
Returns the lock Object for the ResponseHandler.


start

public void start(OnewayExchanger<V,ExecutionException> exchanger)
Description copied from interface: DHTTask
Starts the DHTTask

Specified by:
start in interface DHTTask<V extends Result>

start

protected abstract void start()
                       throws DHTException
Override this method to start the ResponseHandler

Throws:
DHTException

cancel

public void cancel()
Description copied from interface: DHTTask
Cancels the DHTTask

Specified by:
cancel in interface DHTTask<V extends Result>

setTimeout

public void setTimeout(long timeout)
Sets the timeout of this handler


getTimeout

public long getTimeout()
Description copied from interface: ResponseHandler
Returns the timeout (in milliseconds) of this handler.

Specified by:
getTimeout in interface ResponseHandler

getElapsedTime

protected long getElapsedTime()
Returns the total time that has elapsed since this ResponseHandler is active.


getLastResponseTime

protected long getLastResponseTime()
Returns the time when the last response was received


resetErrors

protected void resetErrors()
Resets the error counter


getErrors

protected int getErrors()
Returns the number of errors that have occurred


setMaxErrors

public void setMaxErrors(int maxErrors)
Sets the maximum number of errors that may occur before we're giving up to re-send a request


getMaxErrors

public int getMaxErrors()
Returns the maximum number of errors that may occur before we're giving up to re-send a request


getWaitOnLockTimeout

public long getWaitOnLockTimeout()
The maximum time we're waiting on a lock. Subclasses may overwrite this method to customize the timeout.

Specified by:
getWaitOnLockTimeout in interface DHTTask<V extends Result>

response

protected abstract void response(ResponseMessage message,
                                 long time)
                          throws IOException
See handleResponse()

Throws:
IOException

handleResponse

public void handleResponse(ResponseMessage response,
                           long time)
                    throws IOException
Description copied from interface: ResponseHandler
Called to handle a response

Specified by:
handleResponse in interface ResponseHandler
Parameters:
response - the response message
time - the round trip time
Throws:
IOException

timeout

protected abstract void timeout(KUID nodeId,
                                SocketAddress dst,
                                RequestMessage message,
                                long time)
                         throws IOException
See handleTimeout()

Throws:
IOException

handleTimeout

public void handleTimeout(KUID nodeId,
                          SocketAddress dst,
                          RequestMessage request,
                          long time)
                   throws IOException
Description copied from interface: ResponseHandler
Called after the timeout time has elapsed and no response has arrived

Specified by:
handleTimeout in interface ResponseHandler
Parameters:
nodeId - the Node ID of the Contact (can be null)
dst - the address where we sent the request
request - the request message
time - the total time that has elapsed
Throws:
IOException

resend

protected void resend(KUID nodeId,
                      SocketAddress dst,
                      RequestMessage message)
               throws IOException
Resends the given Message to nodeId/dst

Throws:
IOException

error

protected abstract void error(KUID nodeId,
                              SocketAddress dst,
                              RequestMessage message,
                              IOException e)
See handleError()


handleError

public void handleError(KUID nodeId,
                        SocketAddress dst,
                        RequestMessage message,
                        IOException e)
Description copied from interface: ResponseHandler
Called if an error occurred in handleResponse() or handleTimeout()

Specified by:
handleError in interface ResponseHandler
Parameters:
nodeId - the Node ID of the Contact (can be null)
dst - the address of the Contact
message - the RequestMessage that failed to be send
e - the Exception that was thrown

tick

protected void tick()
See handleTick()


handleTick

public void handleTick()
Description copied from interface: ResponseHandler
Called in periodic intervals while we're waiting for a response.

Specified by:
handleTick in interface ResponseHandler

isCancelled

public boolean isCancelled()
Description copied from interface: ResponseHandler
Returns whether or not this handler has been cancelled

Specified by:
isCancelled in interface ResponseHandler

isDone

public boolean isDone()
Returns whether or not this handler is done which means if it has returned a result or threw an Exception


setReturnValue

protected void setReturnValue(V value)
Sets the return value which will be returned by the call() method


setException

protected void setException(DHTException ex)
Sets the Exception which will be thrown by the call() method


fireTimeoutException

protected void fireTimeoutException(KUID nodeId,
                                    SocketAddress address,
                                    RequestMessage request,
                                    long time)
A helper method to throw Timeout Exceptions


createTimeoutException

protected DHTTimeoutException createTimeoutException(KUID nodeId,
                                                     SocketAddress address,
                                                     RequestMessage request,
                                                     long time)
A helper method to create Timeout Exceptions


LimeWire Consolidated API

Copyright © 2009. All Rights Reserved.