Removes duplication, simplifies the implementation of ResponseCollector.

This commit is contained in:
lubomir.marinov 2010-03-02 09:11:15 +00:00
parent 41ed1c9e41
commit 0b0986dd29
11 changed files with 278 additions and 301 deletions

View File

@ -0,0 +1,51 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j;
/**
* @author Lubomir Marinov
*/
public abstract class AbstractResponseCollector
implements ResponseCollector
{
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
*/
protected abstract void processFailure(BaseStunMessageEvent event);
/**
* Notifies this collector that no response had been received after repeated
* retransmissions of the original request (as described by rfc3489) and
* that the request should be considered unanswered.
*
* @param event the <tt>StunTimeoutEvent</tt> containing a reference to the
* transaction that has just failed.
*/
public void processTimeout(StunTimeoutEvent event)
{
processFailure(event);
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be considered
* unanswered.
*
* @param event the <tt>StunFailureEvent</tt> containing the
* <tt>PortUnreachableException</tt> that has just occurred.
*/
public void processUnreachable(StunFailureEvent event)
{
processFailure(event);
}
}

View File

@ -0,0 +1,84 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license. See terms of license at gnu.org.
*/
package org.ice4j;
import java.util.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* Represents an <tt>EventObject</tt> which notifies of an event associated with
* a specific STUN <tt>Message</tt>.
*
* @author Lubomir Marinov
*/
public class BaseStunMessageEvent
extends EventObject
{
/**
* The STUN <tt>Message</tt> associated with this event.
*/
private final Message message;
/**
* The ID of the transaction related to {@link #message}.
*/
private TransactionID transactionID;
/**
* Initializes a new <tt>BaseStunMessageEvent</tt> associated with a
* specific STUN <tt>Message</tt>.
*
* @param sourceAddress the <tt>TransportAddress</tt> which is to be
* reported as the source of the new event
* @param message the STUN <tt>Message</tt> associated with the new event
*/
public BaseStunMessageEvent(TransportAddress sourceAddress, Message message)
{
super(sourceAddress);
this.message = message;
}
/**
* Gets the STUN <tt>Message</tt> associated with this event.
*
* @return the STUN <tt>Message</tt> associated with this event
*/
public Message getMessage()
{
return message;
}
/**
* Gets the <tt>TransportAddress</tt> which is the source of this event.
*
* @return the <tt>TransportAddress</tt> which is the source of this event
*/
protected TransportAddress getSourceAddress()
{
return (TransportAddress) getSource();
}
/**
* Gets the ID of the transaction related to the STUN <tt>Message</tt>
* associated with this event.
*
* @return the ID of the transaction related to the STUN <tt>Message</tt>
* associated with this event
*/
public TransactionID getTransactionID()
{
if (transactionID == null)
transactionID
= TransactionID
.createTransactionID(getMessage().getTransactionID());
return transactionID;
}
}

View File

@ -22,12 +22,12 @@ public interface ResponseCollector
public void processResponse(StunMessageEvent response);
/**
* Notify the collector that no response had been received
* after repeated retransmissions of the original request (as described
* by rfc3489) and that the request should be considered unanswered.
* Notifies this collector that no response had been received after repeated
* retransmissions of the original request (as described by rfc3489) and
* that the request should be considered unanswered.
*
* @param event the <tt>StunTimeoutEvent</tt> containing a reference to
* the transaction that has just failed.
* @param event the <tt>StunTimeoutEvent</tt> containing a reference to the
* transaction that has just failed.
*/
public void processTimeout(StunTimeoutEvent event);

View File

@ -6,10 +6,7 @@
*/
package org.ice4j;
import java.util.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* The class is used to dispatch events that occur when a STUN transaction
@ -19,24 +16,13 @@ import org.ice4j.stack.*;
* @author Emil Ivov
*/
public class StunFailureEvent
extends EventObject
extends BaseStunMessageEvent
{
/**
* Serial version UID for this Serializable class.
*/
private static final long serialVersionUID = 41232541L;
/**
* The message that the corresponding transaction was about.
*/
private final Message message;
/**
* The <tt>TransactionID</tt> of the transaction that the source message
* is related to.
*/
private final TransactionID transactionID;
/**
* The <tt>Exception</tt> that caused this failure.
*/
@ -56,10 +42,7 @@ public class StunFailureEvent
TransportAddress localAddress,
Throwable cause)
{
super(localAddress);
this.message = message;
this.transactionID = TransactionID.createTransactionID(message
.getTransactionID());
super(localAddress, message);
this.cause = cause;
}
@ -73,28 +56,7 @@ public class StunFailureEvent
*/
public TransportAddress getLocalAddress()
{
return (TransportAddress)getSource();
}
/**
* Returns the <tt>Message</tt> whose transaction has just failed.
*
* @return the <tt>Message</tt> whose transaction has just failed.
*/
public Message getMessage()
{
return message;
}
/**
* Returns the id of the transaction that has just failed.
*
* @return the <tt>TransactionID</tt> of the transaction that has just
* failed.
*/
public TransactionID getTransactionID()
{
return transactionID;
return getSourceAddress();
}
/**
@ -116,9 +78,10 @@ public class StunFailureEvent
* @return a <tt>String</tt> representation of this event, containing the
* corresponding message, and local address.
*/
@Override
public String toString()
{
StringBuffer buff = new StringBuffer("StunTimeoutEvent:\n\tMessage=");
StringBuffer buff = new StringBuffer("StunFailureEvent:\n\tMessage=");
buff.append(getMessage());
buff.append(" localAddr=").append(getLocalAddress());

View File

@ -6,11 +6,7 @@
*/
package org.ice4j;
import java.util.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* The class is used to dispatch incoming stun messages. Apart from the message
@ -22,28 +18,17 @@ import org.ice4j.stack.*;
* @author Emil Ivov
*/
public class StunMessageEvent
extends EventObject
extends BaseStunMessageEvent
{
/**
* Serial version UID for this Serializable class.
*/
private static final long serialVersionUID = 41267843L;
/**
* The message itself.
*/
private Message message = null;
/**
* The sending address.
*/
private TransportAddress remoteAddress = null;
/**
* The <tt>TransactionID</tt> of the transaction that the source message
* is related to.
*/
private final TransactionID transactionID;
private final TransportAddress remoteAddress;
/**
* Constructs a StunMessageEvent according to the specified message.
@ -55,11 +40,9 @@ public class StunMessageEvent
Message message,
TransportAddress remoteAddress)
{
super(sourceAddress);
this.message = message;
super(sourceAddress, message);
this.remoteAddress = remoteAddress;
this.transactionID = TransactionID.createTransactionID(message
.getTransactionID());
}
/**
@ -69,16 +52,7 @@ public class StunMessageEvent
*/
public TransportAddress getLocalAddress()
{
return (TransportAddress)getSource();
}
/**
* Returns the message being dispatched.
* @return the message that caused the event.
*/
public Message getMessage()
{
return message;
return getSourceAddress();
}
/**
@ -90,18 +64,6 @@ public class StunMessageEvent
return remoteAddress;
}
/**
* Returns the id of the transaction associated with the message that
* triggered this event.
*
* @return the id of the transaction associated with the message that
* triggered this event.
*/
public TransactionID getTransactionID()
{
return transactionID;
}
/**
* Returns a <tt>String</tt> representation of this event, containing the
* corresponding message, remote and local addresses.
@ -109,6 +71,7 @@ public class StunMessageEvent
* @return a <tt>String</tt> representation of this event, containing the
* corresponding message, remote and local addresses.
*/
@Override
public String toString()
{
StringBuffer buff = new StringBuffer("StunMessageEvent:\n\tMessage=");

View File

@ -6,10 +6,7 @@
*/
package org.ice4j;
import java.util.*;
import org.ice4j.message.*;
import org.ice4j.stack.*;
/**
* The class is used to dispatch events that occur when a STUN transaction
@ -18,24 +15,13 @@ import org.ice4j.stack.*;
* @author Emil Ivov
*/
public class StunTimeoutEvent
extends EventObject
extends BaseStunMessageEvent
{
/**
* Serial version UID for this Serializable class.
*/
private static final long serialVersionUID = 41267841L;
/**
* The message that the corresponding transaction was about.
*/
private Message message = null;
/**
* The <tt>TransactionID</tt> of the transaction that the source message
* is related to.
*/
private final TransactionID transactionID;
/**
* Constructs a <tt>StunTimeoutEvent</tt> according to the specified
* message.
@ -46,10 +32,7 @@ public class StunTimeoutEvent
public StunTimeoutEvent(Message message,
TransportAddress localAddress)
{
super(localAddress);
this.message = message;
this.transactionID = TransactionID.createTransactionID(message
.getTransactionID());
super(localAddress, message);
}
/**
@ -61,28 +44,7 @@ public class StunTimeoutEvent
*/
public TransportAddress getLocalAddress()
{
return (TransportAddress)getSource();
}
/**
* Returns the <tt>Message</tt> whose transaction has just expired.
*
* @return the <tt>Message</tt> whose transaction has just expired.
*/
public Message getMessage()
{
return message;
}
/**
* Returns the id of the transaction that has just expired.
*
* @return the <tt>TransactionID</tt> of the transaction that has just
* expired.
*/
public TransactionID getTransactionID()
{
return transactionID;
return getSourceAddress();
}
/**
@ -92,6 +54,7 @@ public class StunTimeoutEvent
* @return a <tt>String</tt> representation of this event, containing the
* corresponding message, and local address.
*/
@Override
public String toString()
{
StringBuffer buff = new StringBuffer("StunTimeoutEvent:\n\tMessage=");
@ -101,5 +64,4 @@ public class StunTimeoutEvent
return buff.toString();
}
}

View File

@ -27,8 +27,8 @@ import org.ice4j.stack.*;
* @author Lubomir Marinov
*/
public class StunCandidateHarvester
implements CandidateHarvester,
ResponseCollector
extends AbstractResponseCollector
implements CandidateHarvester
{
/**
* The <tt>Logger</tt> used by the <tt>StunCandidateHarvester</tt>
@ -267,6 +267,20 @@ public class StunCandidateHarvester
waitForResolutionEnd();
}
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected void processFailure(BaseStunMessageEvent event)
{
processFailure(event.getTransactionID());
}
/**
* Removes the corresponding local candidate from the list of candidates
* that we are waiting on in order to complete the harvest.
@ -315,34 +329,6 @@ public class StunCandidateHarvester
}
}
/**
* Notifies the collector that no response had been received
* after repeated retransmissions of the original request (as described
* by rfc3489) and that the request should be considered unanswered.
*
* @param event the <tt>StunTimeoutEvent</tt> that contains the transaction
* which has just expired.
*/
public void processTimeout(StunTimeoutEvent event)
{
processFailure(event.getTransactionID());
}
/**
* Called when one of our requests results in a
* <tt>PortUnreachableException</tt> ... which is actually quite rare
* because of the oddities of the BSD and Java socket implementations, which
* is why we currently ignore this method.
*
* @param event the <tt>StunFailureEvent</tt> containing the
* <tt>PortUnreachableException</tt> which signaled that the destination of
* the request was found to be unreachable.
*/
public void processUnreachable(StunFailureEvent event)
{
processFailure(event.getTransactionID());
}
/**
* Resends a binding request to our stun server through the specified
* <tt>srflxCand</tt> candidate so that it would keep the potential NAT

View File

@ -31,9 +31,10 @@ import org.ice4j.stack.*;
*</code>
*
* @author Emil Ivov
* @author Lubomir Marinov
*/
class BlockingRequestSender
implements ResponseCollector
extends AbstractResponseCollector
{
/**
* Our class logger
@ -81,50 +82,35 @@ class BlockingRequestSender
this.localAddress = localAddress;
}
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
* Notifies the discoverer so that it may resume.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected synchronized void processFailure(BaseStunMessageEvent event)
{
synchronized(sendLock)
{
ended = true;
notifyAll();
}
}
/**
* Saves the message event and notifies the discoverer thread so that
* it may resume.
* @param evt the newly arrived message event.
*/
public synchronized void processResponse(StunMessageEvent evt)
{
synchronized(sendLock){
this.responseEvent = evt;
ended = true;
notifyAll();
}
}
/**
* Notifies the discoverer thread when a message has timeout-ed so that
* it may resume and consider it as unanswered.
*
* @param evt the <tt>StunTimeoutEvent</tt> containing the transaction that
* has just expired.
*/
public synchronized void processTimeout(StunTimeoutEvent evt)
{
synchronized(sendLock){
ended = true;
notifyAll();
}
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be considered
* unanswered.
*
* @param event the <tt>StunFailureEvent</tt> that contains the
* <tt>PortUnreachableException</tt> which signaled that the destination of
* the request was found to be unreachable
* @see ResponseCollector#processUnreachable(StunFailureEvent)
*/
public synchronized void processUnreachable(
StunFailureEvent event)
{
synchronized(sendLock)
{
this.responseEvent = evt;
ended = true;
notifyAll();
}
@ -150,13 +136,15 @@ class BlockingRequestSender
throws StunException,
IOException
{
synchronized(sendLock){
synchronized(sendLock)
{
stunStack.sendRequest(request, serverAddress, localAddress,
BlockingRequestSender.this);
}
ended = false;
while(!ended){
while(!ended)
{
try
{
wait();

View File

@ -281,13 +281,36 @@ public class MessageEventDispatchingTest extends TestCase
}
/**
* A utility class we use to collect incoming responses.
* A utility class to collect incoming responses.
*/
private class PlainResponseCollector
implements ResponseCollector
private static class PlainResponseCollector
extends AbstractResponseCollector
{
public final Vector<Object> receivedResponses = new Vector<Object>();
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected void processFailure(BaseStunMessageEvent event)
{
String receivedResponse;
if (event instanceof StunFailureEvent)
receivedResponse = "unreachable";
else if (event instanceof StunTimeoutEvent)
receivedResponse = "timeout";
else
receivedResponse = "failure";
receivedResponses.add(receivedResponse);
notifyAll();
}
/**
* Stores incoming requests.
*
@ -299,36 +322,6 @@ public class MessageEventDispatchingTest extends TestCase
notifyAll();
}
/**
* Called when the associated transaction expires.
*
* @param evt the <tt>StunTimeoutEvent</tt> containing a reference to
* the failed <tt>Message</tt> and the expired transaction.
*/
public synchronized void processTimeout(StunTimeoutEvent evt)
{
receivedResponses.add("timeout");
notifyAll();
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param event the <tt>StunFailureEvent</tt>
* <tt>PortUnreachableException</tt> which signaled that the destination
* of the request was found to be unreachable
*
* @see ResponseCollector#processUnreachable(StunFailureEvent)
*/
public synchronized void processUnreachable(
StunFailureEvent event)
{
receivedResponses.add("unreachable");
notifyAll();
}
/**
* Waits for a short period of time for a response to arrive
*/

View File

@ -516,14 +516,36 @@ public class TransactionSupportTests extends TestCase
/**
* A simple utility for asynchronously collecting responses.
*/
private class PlainResponseCollector
implements ResponseCollector
private static class PlainResponseCollector
extends AbstractResponseCollector
{
/**
* The responses we've collected so far.
*/
public final Vector<Object> receivedResponses = new Vector<Object>();
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected void processFailure(BaseStunMessageEvent event)
{
String receivedResponse;
if (event instanceof StunFailureEvent)
receivedResponse = "unreachable";
else if (event instanceof StunTimeoutEvent)
receivedResponse = "timeout";
else
receivedResponse = "failure";
receivedResponses.add(receivedResponse);
}
/**
* Logs the received <tt>responseEvt</tt>
*
@ -533,32 +555,5 @@ public class TransactionSupportTests extends TestCase
{
receivedResponses.add(responseEvt);
}
/**
* Called when the associated transaction expires.
*
* @param evt the <tt>StunTimeoutEvent</tt> containing a reference to
* the failed <tt>Message</tt> and the expired transaction.
*/
public void processTimeout(StunTimeoutEvent evt)
{
receivedResponses.add("timeout");
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param event the <tt>StunFailureEvent</tt> containing
* <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
*
* @see ResponseCollector#processUnreachable(StunFailureEvent)
*/
public void processUnreachable(StunFailureEvent event)
{
receivedResponses.add("unreachable");
}
}
}

View File

@ -37,8 +37,6 @@ public class ShallowStackTest extends TestCase {
private DatagramSocket localSock = null;
private DatagramSocket dummyServerSocket = null;
private DatagramPacket bindingRequestPacket
= new DatagramPacket(new byte[4096], 4096);
/**
* Creates a test instance for the method with the specified name.
@ -216,12 +214,12 @@ public class ShallowStackTest extends TestCase {
//---------- create the response ---------------------------------------
Response bindingResponse = MessageFactory.createBindingResponse(
new TransportAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
msgFixture.ADDRESS_ATTRIBUTE_PORT, Transport.UDP ),
new TransportAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_2,
msgFixture.ADDRESS_ATTRIBUTE_PORT_2, Transport.UDP),
new TransportAddress( msgFixture.ADDRESS_ATTRIBUTE_ADDRESS_3,
msgFixture.ADDRESS_ATTRIBUTE_PORT_3, Transport.UDP));
new TransportAddress( MsgFixture.ADDRESS_ATTRIBUTE_ADDRESS,
MsgFixture.ADDRESS_ATTRIBUTE_PORT, Transport.UDP ),
new TransportAddress( MsgFixture.ADDRESS_ATTRIBUTE_ADDRESS_2,
MsgFixture.ADDRESS_ATTRIBUTE_PORT_2, Transport.UDP),
new TransportAddress( MsgFixture.ADDRESS_ATTRIBUTE_ADDRESS_3,
MsgFixture.ADDRESS_ATTRIBUTE_PORT_3, Transport.UDP));
//---------- send & receive the response -------------------------------
dgramCollector.startListening(dummyServerSocket);
@ -302,8 +300,8 @@ public class ShallowStackTest extends TestCase {
/**
* A simple utility that allows us to asynchronously collect messages.
*/
public class SimpleResponseCollector
implements ResponseCollector
public static class SimpleResponseCollector
extends AbstractResponseCollector
{
/**
@ -312,6 +310,29 @@ public class ShallowStackTest extends TestCase {
*/
Response collectedResponse = null;
/**
* Notifies this <tt>ResponseCollector</tt> that a transaction described by
* the specified <tt>BaseStunMessageEvent</tt> has failed. The possible
* reasons for the failure include timeouts, unreachable destination, etc.
*
* @param event the <tt>BaseStunMessageEvent</tt> which describes the failed
* transaction and the runtime type of which specifies the failure reason
* @see AbstractResponseCollector#processFailure(BaseStunMessageEvent)
*/
protected synchronized void processFailure(BaseStunMessageEvent event)
{
String msg;
if (event instanceof StunFailureEvent)
msg = "Unreachable";
else if (event instanceof StunTimeoutEvent)
msg = "Timeout";
else
msg = "Failure";
logger.info(msg);
notifyAll();
}
/**
* Logs the received response and notifies the wait method.
*/
@ -322,35 +343,6 @@ public class ShallowStackTest extends TestCase {
notifyAll();
}
/**
* Logs the timeout event for later use.
*
* @param evt the timeout event that has just occurred.
*/
public synchronized void processTimeout(StunTimeoutEvent evt)
{
logger.info("Timeout");
notifyAll();
}
/**
* Notifies this collector that the destination of the request has been
* determined to be unreachable and that the request should be
* considered unanswered.
*
* @param evt the <tt>StunFailureEvent</tt> containing the
* <tt>PortUnreachableException</tt> which signaled
* that the destination of the request was found to be unreachable
*
* @see ResponseCollector#processUnreachable(StunFailureEvent)
*/
public synchronized void processUnreachable(
StunFailureEvent evt)
{
logger.info("Unreachable");
notifyAll();
}
/**
* Blocks until a request arrives or 50 ms pass.
*/