diff --git a/src/org/ice4j/AbstractResponseCollector.java b/src/org/ice4j/AbstractResponseCollector.java new file mode 100644 index 0000000..ab52e44 --- /dev/null +++ b/src/org/ice4j/AbstractResponseCollector.java @@ -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 ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * + * @param event the BaseStunMessageEvent 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 StunTimeoutEvent 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 StunFailureEvent containing the + * PortUnreachableException that has just occurred. + */ + public void processUnreachable(StunFailureEvent event) + { + processFailure(event); + } +} diff --git a/src/org/ice4j/BaseStunMessageEvent.java b/src/org/ice4j/BaseStunMessageEvent.java new file mode 100644 index 0000000..efc00d6 --- /dev/null +++ b/src/org/ice4j/BaseStunMessageEvent.java @@ -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 EventObject which notifies of an event associated with + * a specific STUN Message. + * + * @author Lubomir Marinov + */ +public class BaseStunMessageEvent + extends EventObject +{ + + /** + * The STUN Message associated with this event. + */ + private final Message message; + + /** + * The ID of the transaction related to {@link #message}. + */ + private TransactionID transactionID; + + /** + * Initializes a new BaseStunMessageEvent associated with a + * specific STUN Message. + * + * @param sourceAddress the TransportAddress which is to be + * reported as the source of the new event + * @param message the STUN Message associated with the new event + */ + public BaseStunMessageEvent(TransportAddress sourceAddress, Message message) + { + super(sourceAddress); + + this.message = message; + } + + /** + * Gets the STUN Message associated with this event. + * + * @return the STUN Message associated with this event + */ + public Message getMessage() + { + return message; + } + + /** + * Gets the TransportAddress which is the source of this event. + * + * @return the TransportAddress which is the source of this event + */ + protected TransportAddress getSourceAddress() + { + return (TransportAddress) getSource(); + } + + /** + * Gets the ID of the transaction related to the STUN Message + * associated with this event. + * + * @return the ID of the transaction related to the STUN Message + * associated with this event + */ + public TransactionID getTransactionID() + { + if (transactionID == null) + transactionID + = TransactionID + .createTransactionID(getMessage().getTransactionID()); + return transactionID; + } +} diff --git a/src/org/ice4j/ResponseCollector.java b/src/org/ice4j/ResponseCollector.java index 2cdf716..d3bbca0 100644 --- a/src/org/ice4j/ResponseCollector.java +++ b/src/org/ice4j/ResponseCollector.java @@ -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 StunTimeoutEvent containing a reference to - * the transaction that has just failed. + * @param event the StunTimeoutEvent containing a reference to the + * transaction that has just failed. */ public void processTimeout(StunTimeoutEvent event); diff --git a/src/org/ice4j/StunFailureEvent.java b/src/org/ice4j/StunFailureEvent.java index 2eb63be..fd9bc30 100644 --- a/src/org/ice4j/StunFailureEvent.java +++ b/src/org/ice4j/StunFailureEvent.java @@ -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 TransactionID of the transaction that the source message - * is related to. - */ - private final TransactionID transactionID; - /** * The Exception 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 Message whose transaction has just failed. - * - * @return the Message whose transaction has just failed. - */ - public Message getMessage() - { - return message; - } - - /** - * Returns the id of the transaction that has just failed. - * - * @return the TransactionID of the transaction that has just - * failed. - */ - public TransactionID getTransactionID() - { - return transactionID; + return getSourceAddress(); } /** @@ -116,9 +78,10 @@ public class StunFailureEvent * @return a String 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()); diff --git a/src/org/ice4j/StunMessageEvent.java b/src/org/ice4j/StunMessageEvent.java index 1310f5a..75e23ce 100644 --- a/src/org/ice4j/StunMessageEvent.java +++ b/src/org/ice4j/StunMessageEvent.java @@ -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 TransactionID 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 String representation of this event, containing the * corresponding message, remote and local addresses. @@ -109,6 +71,7 @@ public class StunMessageEvent * @return a String representation of this event, containing the * corresponding message, remote and local addresses. */ + @Override public String toString() { StringBuffer buff = new StringBuffer("StunMessageEvent:\n\tMessage="); diff --git a/src/org/ice4j/StunTimeoutEvent.java b/src/org/ice4j/StunTimeoutEvent.java index fe25bd4..03de275 100644 --- a/src/org/ice4j/StunTimeoutEvent.java +++ b/src/org/ice4j/StunTimeoutEvent.java @@ -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 TransactionID of the transaction that the source message - * is related to. - */ - private final TransactionID transactionID; - /** * Constructs a StunTimeoutEvent 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 Message whose transaction has just expired. - * - * @return the Message whose transaction has just expired. - */ - public Message getMessage() - { - return message; - } - - /** - * Returns the id of the transaction that has just expired. - * - * @return the TransactionID of the transaction that has just - * expired. - */ - public TransactionID getTransactionID() - { - return transactionID; + return getSourceAddress(); } /** @@ -92,6 +54,7 @@ public class StunTimeoutEvent * @return a String 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(); } - } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index 840f3d4..7368e6f 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -27,8 +27,8 @@ import org.ice4j.stack.*; * @author Lubomir Marinov */ public class StunCandidateHarvester - implements CandidateHarvester, - ResponseCollector + extends AbstractResponseCollector + implements CandidateHarvester { /** * The Logger used by the StunCandidateHarvester @@ -267,6 +267,20 @@ public class StunCandidateHarvester waitForResolutionEnd(); } + /** + * Notifies this ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * + * @param event the BaseStunMessageEvent 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 StunTimeoutEvent 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 - * PortUnreachableException ... 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 StunFailureEvent containing the - * PortUnreachableException 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 * srflxCand candidate so that it would keep the potential NAT diff --git a/src/org/ice4j/stunclient/BlockingRequestSender.java b/src/org/ice4j/stunclient/BlockingRequestSender.java index fc1c567..f11353c 100644 --- a/src/org/ice4j/stunclient/BlockingRequestSender.java +++ b/src/org/ice4j/stunclient/BlockingRequestSender.java @@ -31,9 +31,10 @@ import org.ice4j.stack.*; * * * @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 ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * Notifies the discoverer so that it may resume. + * + * @param event the BaseStunMessageEvent 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 StunTimeoutEvent 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 StunFailureEvent that contains the - * PortUnreachableException 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(); diff --git a/test/org/ice4j/MessageEventDispatchingTest.java b/test/org/ice4j/MessageEventDispatchingTest.java index 4e48f10..0223b43 100644 --- a/test/org/ice4j/MessageEventDispatchingTest.java +++ b/test/org/ice4j/MessageEventDispatchingTest.java @@ -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 receivedResponses = new Vector(); + /** + * Notifies this ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * + * @param event the BaseStunMessageEvent 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 StunTimeoutEvent containing a reference to - * the failed Message 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 StunFailureEvent - * PortUnreachableException 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 */ diff --git a/test/org/ice4j/TransactionSupportTests.java b/test/org/ice4j/TransactionSupportTests.java index 3555b66..f11691b 100644 --- a/test/org/ice4j/TransactionSupportTests.java +++ b/test/org/ice4j/TransactionSupportTests.java @@ -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 receivedResponses = new Vector(); + /** + * Notifies this ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * + * @param event the BaseStunMessageEvent 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 responseEvt * @@ -533,32 +555,5 @@ public class TransactionSupportTests extends TestCase { receivedResponses.add(responseEvt); } - - /** - * Called when the associated transaction expires. - * - * @param evt the StunTimeoutEvent containing a reference to - * the failed Message 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 StunFailureEvent containing - * PortUnreachableException 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"); - } } } diff --git a/test/org/ice4j/stack/ShallowStackTest.java b/test/org/ice4j/stack/ShallowStackTest.java index 6921a47..be38b4a 100644 --- a/test/org/ice4j/stack/ShallowStackTest.java +++ b/test/org/ice4j/stack/ShallowStackTest.java @@ -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 ResponseCollector that a transaction described by + * the specified BaseStunMessageEvent has failed. The possible + * reasons for the failure include timeouts, unreachable destination, etc. + * + * @param event the BaseStunMessageEvent 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 StunFailureEvent containing the - * PortUnreachableException 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. */