com.veraxsystems.icmp.ping.handlers
Class BlockingPing

java.lang.Object
  extended by com.veraxsystems.icmp.ping.handlers.BlockingPing
All Implemented Interfaces:
PingResponseHandler

public class BlockingPing
extends java.lang.Object
implements PingResponseHandler

The implementation of PingResponseHandler for synchronous processing of ICMP EchoRequests using asynchronous Ping.echo method. The synchronous (blocking) BlockingPing.echo method is defined by this class. It sends several ICMP EchoRequests to the set of address provided as input parameter. At the end of request processing the appropriate overridden methods of PingResponseHandler are executed. Each received result is converted to PingResult object and assigned to appropriate entry of internal result array. Additionally, the count down semaphore is decreased so BlockingPing.echo method waits until all requests are processed - this way the synchronous behavior is achieved.


Constructor Summary
BlockingPing()
           
 
Method Summary
 PingResult[] echo(java.net.InetAddress[] addresses, byte[] dataToSend, int dataLength, int timeout)
          Send the set of ICMP echo requests.
 void handleException(PingRequest request, java.lang.Exception exception)
          Method called by Ping in case exception occurred when receiving ICMP echo response.
 void handleResponse(PingRequest request, IcmpEchoResponse response)
          Method called by Ping on receiving proper ICMP echo response.
 void handleTimeout(PingRequest request)
          Method called by Ping in case timeout occurred when receiving ICMP echo response.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingPing

public BlockingPing()
Method Detail

handleResponse

public void handleResponse(PingRequest request,
                           IcmpEchoResponse response)
Description copied from interface: PingResponseHandler
Method called by Ping on receiving proper ICMP echo response.

Specified by:
handleResponse in interface PingResponseHandler
Parameters:
request - the descriptor of request issued by echo() method
response - ICMP response received

handleTimeout

public void handleTimeout(PingRequest request)
Description copied from interface: PingResponseHandler
Method called by Ping in case timeout occurred when receiving ICMP echo response.

Specified by:
handleTimeout in interface PingResponseHandler
Parameters:
request - the descriptor of request issued by echo() method

handleException

public void handleException(PingRequest request,
                            java.lang.Exception exception)
Description copied from interface: PingResponseHandler
Method called by Ping in case exception occurred when receiving ICMP echo response. Such situation means fatal error of Ping class so it is very probable it will not work any more.

Specified by:
handleException in interface PingResponseHandler
Parameters:
request - the descriptor of request issued by echo() method
exception - exception occurred

echo

public PingResult[] echo(java.net.InetAddress[] addresses,
                         byte[] dataToSend,
                         int dataLength,
                         int timeout)
                  throws PingErrorException
Send the set of ICMP echo requests. Method is executed synchronously and waits until all echo requests are finished. Internally, echo requests are send asynchronously - it means all requests are send immediately without waiting for responses (those, are handled in separate thread). Next, method waits until either all echo responses are received or timeouts occurs.

Each echo result is placed in array of results corresponding to addresses parameter (it means for each input host the result is placed in corresponding element of result array).

Each request timeout is calculated separately - it means that its calculation starts in the moment when request is sent (not when echo method is called). Such behavior leads to situation that echo execution lasts slightly longer than timeout specified.

Method reports exception when either Ping is not initialized or current thread is interrupted.

Parameters:
addresses - array of host addresses send ICMP echo requests
dataToSend - data to be send as part of ICMP message (when null the default data is used)
dataLength - length of data to be sent
timeout - expressed in milliseconds time to wait for echo response
Returns:
the array of results
Throws:
PingErrorException - issued if Ping class is not initialized or thread is interrupted