Adds basic support for StunHarvester-s to the ICE candidate gathering operations

This commit is contained in:
emcho 2010-02-15 22:16:10 +00:00
parent b3ea6b9fc9
commit 627097b986
4 changed files with 91 additions and 36 deletions

View File

@ -6,8 +6,6 @@
*/ */
package org.ice4j; package org.ice4j;
import java.net.*;
/** /**
* The interface is used as a callback when sending a request. The response * The interface is used as a callback when sending a request. The response
* collector is then used as a means of dispatching the response. * collector is then used as a means of dispatching the response.
@ -27,6 +25,9 @@ public interface ResponseCollector
* Notify the collector that no response had been received * Notify the collector that no response had been received
* after repeated retransmissions of the original request (as described * after repeated retransmissions of the original request (as described
* by rfc3489) and that the request should be considered unanswered. * 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); public void processTimeout(StunTimeoutEvent event);
@ -35,8 +36,8 @@ public interface ResponseCollector
* determined to be unreachable and that the request should be considered * determined to be unreachable and that the request should be considered
* unanswered. * unanswered.
* *
* @param exception the <tt>PortUnreachableException</tt> which signaled * @param event the <tt>StunFailureEvent</tt> containing the
* that the destination of the request was found to be unreachable * <tt>PortUnreachableException</tt> that has just occurred.
*/ */
public void processUnreachable(PortUnreachableException exception); public void processUnreachable(StunFailureEvent event);
} }

View File

@ -106,13 +106,15 @@ public class XorMappedAddressAttribute
/** /**
* Returns the result of applying XOR on this attribute's address, using the * Returns the result of applying XOR on this attribute's address, using the
* specified transaction identifier. The method may be used for both * specified operand. The method may be used for both
* encoding and decoding XorMappedAddresses. * encoding and decoding <tt>XorMappedAddresses</tt>.
* @param transactionID the transaction id to use for the XOR *
* @param operand the transaction id to use for the XOR
*
* @return the XOR-ed address. * @return the XOR-ed address.
*/ */
public TransportAddress applyXor(byte[] transactionID) public TransportAddress applyXor(byte[] operand)
{ {
return applyXor(getAddress(), transactionID); return applyXor(getAddress(), operand);
} }
} }

View File

@ -12,10 +12,13 @@ import java.util.*;
import java.util.logging.*; import java.util.logging.*;
import org.ice4j.*; import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.ice.*; import org.ice4j.ice.*;
import org.ice4j.message.*; import org.ice4j.message.*;
import org.ice4j.stack.*; import org.ice4j.stack.*;
import com.sun.org.apache.bcel.internal.generic.*;
/** /**
* A <tt>StunCandidateHarvester</tt> gathers STUN <tt>Candidate</tt>s for a * A <tt>StunCandidateHarvester</tt> gathers STUN <tt>Candidate</tt>s for a
* specified {@link org.ice4j.ice.Component}. * specified {@link org.ice4j.ice.Component}.
@ -149,9 +152,10 @@ public class StunCandidateHarvester
} }
/** /**
* Try to figure out . * Matches the response to one of the local candidates and creates a
* <tt>ServerReflexiveCandidate</tt> using it as a base.
* *
* @param response the response to dispatch. * @param response the response that we've just received from a StunServer.
*/ */
public void processResponse(StunMessageEvent response) public void processResponse(StunMessageEvent response)
{ {
@ -159,15 +163,21 @@ public class StunCandidateHarvester
{ {
TransactionID tranID = response.getTransactionID(); TransactionID tranID = response.getTransactionID();
Candidate localCand = resolveMap.remove(tranID); HostCandidate localCand = resolveMap.remove(tranID);
if ( localCand != null)
{
createServerReflexiveCandidate(response.getMessage(),
localCand);
}
//if this was the last candidate, we are done with the STUN //if this was the last candidate, we are done with the STUN
//resolution and need to notify the waiters. //resolution and need to notify the waiters.
if(resolveMap.isEmpty()) if(resolveMap.isEmpty())
resolveMap.notify(); resolveMap.notify();
System.out.println("received a message tranid=" + tranID); logger.finest("received a message tranid=" + tranID);
System.out.println("response=" + response); logger.finest("localCand=" + localCand);
} }
} }
@ -182,20 +192,7 @@ public class StunCandidateHarvester
*/ */
public void processTimeout(StunTimeoutEvent event) public void processTimeout(StunTimeoutEvent event)
{ {
synchronized (resolveMap) processFailure(event.getTransactionID());
{
TransactionID tranID = event.getTransactionID();
Candidate localCand = resolveMap.remove(tranID);
//if this was the last candidate, we are done with the STUN
//resolution and need to notify the waiters.
if(resolveMap.isEmpty())
resolveMap.notify();
System.out.println("a tran expired tranid=" + tranID);
System.out.println("event=" + event);
}
} }
/** /**
@ -204,11 +201,65 @@ public class StunCandidateHarvester
* because of the oddities of the BSD and Java socket implementations, which * because of the oddities of the BSD and Java socket implementations, which
* is why we currently ignore this method. * is why we currently ignore this method.
* *
* @param exception the <tt>PortUnreachableException</tt> which signaled * @param event the <tt>StunFailureEvent</tt>
* that the destination of the request was found to be unreachable * <tt>PortUnreachableException</tt> which signaled that the destination of
* the request was found to be unreachable.
*/ */
public void processUnreachable(PortUnreachableException exception) public void processUnreachable(StunFailureEvent 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.
*
* @param transactionID the ID of the transaction that has just ended.
*/
private void processFailure(TransactionID transactionID)
{
synchronized (resolveMap)
{
Candidate localCand = resolveMap.remove(transactionID);
//if this was the last candidate, we are done with the STUN
//resolution and need to notify the waiters.
if(resolveMap.isEmpty())
resolveMap.notify();
logger.finest("a tran expired tranid=" + transactionID);
logger.finest("localAddr=" + localCand);
}
}
/**
* Creates a <tt>ServerReflexiveCandidate</tt> using <tt>base</tt> as its
* and the <tt>XOR-MAPPED-ADDRESS</tt> attributes in <tt>response</tt> for
* the actual <tt>TransportAddress</tt> of the new candidate. If the message
* is somehow malformed and does not contain the corresponding attribute
* this method simply has no effect.
*
* @param response the <tt>Message</tt> that is supposed to contain the
* address we should use for this candidate.
* @param base the <tt>HostCandidate</tt> that we should use as a base
* for the new <tt>ServerReflexiveCandidate</tt>.
*/
private void createServerReflexiveCandidate(Message response,
HostCandidate base)
{
Attribute attribute
= response.getAttribute(Attribute.XOR_MAPPED_ADDRESS);
if(attribute == null
|| !(attribute instanceof XorMappedAddressAttribute))
return;
TransportAddress addr = ((XorMappedAddressAttribute)attribute)
.applyXor(Message.MAGIC_COOKIE);
ServerReflexiveCandidate srvrRflxCand
= new ServerReflexiveCandidate(addr, base, stunServer);
base.getParentComponent().addLocalCandidate(srvrRflxCand);
} }
} }

View File

@ -116,12 +116,13 @@ class BlockingRequestSender
* determined to be unreachable and that the request should be considered * determined to be unreachable and that the request should be considered
* unanswered. * unanswered.
* *
* @param exception the <tt>PortUnreachableException</tt> which signaled * @param event the <tt>StunFailureEvent</tt> that contains the
* that the destination of the request was found to be unreachable * <tt>PortUnreachableException</tt> which signaled that the destination of
* @see ResponseCollector#processUnreachable(PortUnreachableException) * the request was found to be unreachable
* @see ResponseCollector#processUnreachable(StunFailureEvent)
*/ */
public synchronized void processUnreachable( public synchronized void processUnreachable(
PortUnreachableException exception) StunFailureEvent event)
{ {
synchronized(sendLock) synchronized(sendLock)
{ {