Adds support for storing application data in transaction IDs so that we could keep in there references to CandidatePair-s

This commit is contained in:
emcho 2010-05-01 21:35:47 +00:00
parent 44e7266220
commit f0a6ba00c9
8 changed files with 216 additions and 51 deletions

View File

@ -33,7 +33,7 @@ public class BaseStunMessageEvent
/** /**
* The ID of the transaction related to {@link #message}. * The ID of the transaction related to {@link #message}.
*/ */
private TransactionID transactionID; private TransactionID transactionID = null;
/** /**
* Initializes a new <tt>BaseStunMessageEvent</tt> associated with a * Initializes a new <tt>BaseStunMessageEvent</tt> associated with a
@ -85,4 +85,15 @@ public class BaseStunMessageEvent
.createTransactionID(getMessage().getTransactionID()); .createTransactionID(getMessage().getTransactionID());
return transactionID; return transactionID;
} }
/**
* Allows descendants of this class to set the transaction ID so that we
* don't need to look it up later. This is not mandatory.
*
* @param tranID the ID of the transaction associated with this event.
*/
protected void setTransactionID(TransactionID tranID)
{
this.transactionID = tranID;
}
} }

View File

@ -31,13 +31,4 @@ public interface ResponseCollector
*/ */
public void processTimeout(StunTimeoutEvent event); public void processTimeout(StunTimeoutEvent 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);
} }

View File

@ -7,6 +7,7 @@
package org.ice4j; package org.ice4j;
import org.ice4j.message.*; import org.ice4j.message.*;
import org.ice4j.stack.*;
/** /**
* The class is used to dispatch events that occur when a STUN transaction * The class is used to dispatch events that occur when a STUN transaction
@ -28,11 +29,14 @@ public class StunTimeoutEvent
* *
* @param message the message itself * @param message the message itself
* @param localAddress the local address that the message was sent from. * @param localAddress the local address that the message was sent from.
* @param transactionID the ID of the associated with this event.
*/ */
public StunTimeoutEvent(Message message, public StunTimeoutEvent(Message message,
TransportAddress localAddress) TransportAddress localAddress,
TransactionID transactionID)
{ {
super(localAddress, message); super(localAddress, message);
super.setTransactionID(transactionID);
} }
/** /**

View File

@ -355,7 +355,6 @@ public class Agent
streams.get(0).getCheckList().computeInitialCheckListPairStates(); streams.get(0).getCheckList().computeInitialCheckListPairStates();
} }
/** /**
* Computes and sets the foundations foundation for all <tt>Candidate</tt>s * Computes and sets the foundations foundation for all <tt>Candidate</tt>s
* currently found in <tt>component</tt>. * currently found in <tt>component</tt>.

View File

@ -77,19 +77,32 @@ public class ConnectivityCheckClient
paceMaker.start(); paceMaker.start();
} }
/**
private TransactionID startCheckForPair(CandidatePair pair) * Creates a STUN {@link Request} containing the necessary PRIORITY and
* CONTROLLING/CONTROLLED attributes. Also stores a reference to
* <tt>candidatePair</tt> in the newly created transactionID so that we
* could then refer back to it in subsequent response or failure events.
*
* @param candidatePair that {@link CandidatePair} that we'd like to start
* a check for.
*
* @return a reference to the {@link TransactionID} used in the connectivity
* check client transaction or <tt>null</tt> if sending the check has
* failed for some reason.
*/
private TransactionID startCheckForPair(CandidatePair candidatePair)
{ {
//we don't need to do a canReach() verification here as it has been //we don't need to do a canReach() verification here as it has been
//already verified during the gathering process. //already verified during the gathering process.
DatagramSocket stunSocket = ((HostCandidate)pair.getLocalCandidate()) DatagramSocket stunSocket = ((HostCandidate)candidatePair
.getStunSocket(null); .getLocalCandidate()) .getStunSocket(null);
Request request = MessageFactory.createBindingRequest(); Request request = MessageFactory.createBindingRequest();
//priority //the priority we'd like the remote party to use for a peer reflexive
//candidate if one is discovered as a consequence of this check.
PriorityAttribute priority = AttributeFactory.createPriorityAttribute( PriorityAttribute priority = AttributeFactory.createPriorityAttribute(
pair.getLocalCandidate().computePriorityForType( candidatePair.getLocalCandidate().computePriorityForType(
CandidateType.PEER_REFLEXIVE_CANDIDATE)); CandidateType.PEER_REFLEXIVE_CANDIDATE));
request.addAttribute(priority); request.addAttribute(priority);
@ -115,26 +128,24 @@ public class ConnectivityCheckClient
request.addAttribute(unameAttr); request.addAttribute(unameAttr);
//todo: do this in the stun stack so that we could do the
//calculation once the request is ready (we'd need the transaction id
//for example.
//todo: also implement SASL prepare //todo: also implement SASL prepare
MessageIntegrityAttribute msgIntegrity = AttributeFactory MessageIntegrityAttribute msgIntegrity = AttributeFactory
.createMessageIntegrityAttribute(localUserName); .createMessageIntegrityAttribute(localUserName);
request.addAttribute(msgIntegrity); request.addAttribute(msgIntegrity);
TransactionID tran; TransactionID tran = TransactionID.createNewTransactionID();
tran.setApplicationData(candidatePair);
try try
{ {
tran = stunStack.sendRequest(request, stunStack.sendRequest(request,
pair.getRemoteCandidate().getTransportAddress(), stunSocket, candidatePair.getRemoteCandidate().getTransportAddress(),
this); candidatePair.getLocalCandidate().getTransportAddress(),
this, tran);
if(logger.isLoggable(Level.FINEST)) if(logger.isLoggable(Level.FINEST))
logger.finest("checking pair " + pair + " with tran=" logger.finest("checking pair " + candidatePair + " with tran="
+ tran.toString()); + tran.toString());
return tran; return tran;
@ -159,18 +170,18 @@ public class ConnectivityCheckClient
System.out.println("timeout event=" + event); System.out.println("timeout event=" + event);
} }
public void processUnreachable(StunFailureEvent event)
{
System.out.println("failure event=" + event);
}
/** /**
* The thread that actually sends the checks for a particular check list * The thread that actually sends the checks for a particular check list
* in the pace defined in RFC 5245. * in the pace defined in RFC 5245.
*/ */
private static class PaceMaker extends Thread private static class PaceMaker extends Thread
{ {
/**
* Indicates whether this thread is still running. Not really used at
* this point.
*/
public boolean isRunning = true; public boolean isRunning = true;
/** /**
* The {@link ConnectivityCheckClient} that created us. * The {@link ConnectivityCheckClient} that created us.
*/ */
@ -225,6 +236,9 @@ public class ConnectivityCheckClient
{ {
logger.log(Level.FINER, "PaceMake got interrupted", e); logger.log(Level.FINER, "PaceMake got interrupted", e);
} }
if (!isRunning)
return;
} }
checkList.setState(CheckListState.RUNNING); checkList.setState(CheckListState.RUNNING);
@ -242,6 +256,7 @@ public class ConnectivityCheckClient
//we are done sending checks for this list. we'll send its //we are done sending checks for this list. we'll send its
//final state in either the processResponse() //final state in either the processResponse()
//processTimeout() or processFailure() method. //processTimeout() or processFailure() method.
isRunning = false;
return; return;
} }

View File

@ -147,6 +147,31 @@ class StunClientTransaction
TransportAddress requestDestination, TransportAddress requestDestination,
TransportAddress localAddress, TransportAddress localAddress,
ResponseCollector responseCollector) ResponseCollector responseCollector)
{
this(stackCallback, request, requestDestination, localAddress,
responseCollector, TransactionID.createNewTransactionID());
}
/**
* Creates a client transaction.
*
* @param stackCallback the stack that created us.
* @param request the request that we are living for.
* @param requestDestination the destination of the request.
* @param localAddress the local <tt>TransportAddress</tt> this transaction
* will be communication through.
* @param responseCollector the instance that should receive this request's
* response retransmit.
* @param transactionID the ID that we'd like the new transaction to have
* in case the application created it in order to use it for application
* data correlation.
*/
public StunClientTransaction(StunStack stackCallback,
Request request,
TransportAddress requestDestination,
TransportAddress localAddress,
ResponseCollector responseCollector,
TransactionID transactionID)
{ {
this.stackCallback = stackCallback; this.stackCallback = stackCallback;
this.request = request; this.request = request;
@ -156,7 +181,8 @@ class StunClientTransaction
initTransactionConfiguration(); initTransactionConfiguration();
this.transactionID = TransactionID.createTransactionID(); this.transactionID = transactionID;
try try
{ {
request.setTransactionID(transactionID.getTransactionID()); request.setTransactionID(transactionID.getTransactionID());
@ -220,8 +246,8 @@ class StunClientTransaction
waitFor(nextWaitInterval); waitFor(nextWaitInterval);
responseCollector.processTimeout( responseCollector.processTimeout( new StunTimeoutEvent(
new StunTimeoutEvent(this.request, getLocalAddress())); this.request, getLocalAddress(), transactionID));
stackCallback.removeClientTransaction(this); stackCallback.removeClientTransaction(this);
} }

View File

@ -123,6 +123,54 @@ public class StunStack
netAccessManager.removeSocket(localAddr); netAccessManager.removeSocket(localAddr);
} }
/**
* Returns the transaction with the specified <tt>transactionID</tt> or
* <tt>null</tt> if no such transaction exists.
*
* @param transactionID the ID of the transaction we are looking for.
*
* @return the {@link StunClientTransaction} we are looking for.
*/
protected StunClientTransaction getClientTransaction(byte[] transactionID)
{
synchronized (clientTransactions)
{
Collection<StunClientTransaction> cTrans
= clientTransactions.values();
for (StunClientTransaction tran : cTrans)
{
if (tran.getTransactionID().equals(transactionID))
return tran;
}
}
return null;
}
/**
* Returns the transaction with the specified <tt>transactionID</tt> or
* <tt>null</tt> if no such transaction exists.
*
* @param transactionID the ID of the transaction we are looking for.
*
* @return the {@link StunClientTransaction} we are looking for.
*/
protected StunServerTransaction getServerTransaction(byte[] transactionID)
{
synchronized (serverTransactions)
{
Collection<StunServerTransaction> sTrans
= serverTransactions.values();
for (StunServerTransaction tran : sTrans)
{
if (tran.getTransactionID().equals(transactionID))
return tran;
}
}
return null;
}
/** /**
* Cancels the {@link StunClientTransaction} with the specified * Cancels the {@link StunClientTransaction} with the specified
* <tt>transactionID</tt>. Cancellation means that the stack will not * <tt>transactionID</tt>. Cancellation means that the stack will not
@ -237,10 +285,41 @@ public class StunStack
TransportAddress sendThrough, TransportAddress sendThrough,
ResponseCollector collector ) ResponseCollector collector )
throws IOException, IllegalArgumentException throws IOException, IllegalArgumentException
{
return sendRequest(request, sendTo, sendThrough, collector,
TransactionID.createNewTransactionID());
}
/**
* Sends the specified request through the specified access point, and
* registers the specified ResponseCollector for later notification.
* @param request the request to send
* @param sendTo the destination address of the request.
* @param sendThrough the local address to use when sending the request
* @param collector the instance to notify when a response arrives or the
* the transaction timeouts
* @param transactionID the ID that we'd like the new transaction to use
* in case the application created it in order to use it for application
* data correlation.
*
* @return the <tt>TransactionID</tt> of the <tt>StunClientTransaction</tt>
* that we used in order to send the request.
*
* @throws IOException if an error occurs while sending message bytes
* through the network socket.
* @throws IllegalArgumentException if the apDescriptor references an
* access point that had not been installed,
*/
public TransactionID sendRequest( Request request,
TransportAddress sendTo,
TransportAddress sendThrough,
ResponseCollector collector,
TransactionID transactionID)
throws IOException, IllegalArgumentException
{ {
StunClientTransaction clientTransaction StunClientTransaction clientTransaction
= new StunClientTransaction(this, request, sendTo, sendThrough, = new StunClientTransaction(this, request, sendTo, sendThrough,
collector); collector, transactionID);
clientTransactions.put(clientTransaction.getTransactionID(), clientTransactions.put(clientTransaction.getTransactionID(),
clientTransaction); clientTransaction);
@ -406,8 +485,8 @@ public class StunStack
if(msg instanceof Request) if(msg instanceof Request)
{ {
logger.finest("parsing request"); logger.finest("parsing request");
TransactionID serverTid = TransactionID.
createTransactionID(msg.getTransactionID()); TransactionID serverTid = event.getTransactionID();
StunServerTransaction sTran = serverTransactions.get(serverTid); StunServerTransaction sTran = serverTransactions.get(serverTid);
if( sTran != null) if( sTran != null)
@ -450,8 +529,7 @@ public class StunStack
//response //response
else if(msg instanceof Response) else if(msg instanceof Response)
{ {
TransactionID tid TransactionID tid = event.getTransactionID();
= TransactionID.createTransactionID(msg.getTransactionID());
StunClientTransaction tran = clientTransactions.remove(tid); StunClientTransaction tran = clientTransactions.remove(tid);
if(tran != null) if(tran != null)

View File

@ -22,6 +22,11 @@ public class TransactionID
*/ */
private final byte transactionID[] = new byte[12]; private final byte transactionID[] = new byte[12];
/**
* Any object that the application would like to correlate to a transaction.
*/
private Object applicationData = null;
/** /**
* The object to use to generate the rightmost 8 bytes of the id. * The object to use to generate the rightmost 8 bytes of the id.
*/ */
@ -50,7 +55,7 @@ public class TransactionID
* *
* @return A <tt>TransactionID</tt> object with a unique transaction id. * @return A <tt>TransactionID</tt> object with a unique transaction id.
*/ */
public static TransactionID createTransactionID() public static TransactionID createNewTransactionID()
{ {
TransactionID tid = new TransactionID(); TransactionID tid = new TransactionID();
@ -73,12 +78,35 @@ public class TransactionID
} }
/** /**
* Creates a transaction identifier object with the specified id. * Returns a <tt>TransactionID</tt> instance for the specified id. If
* @param transactionID the id to give to the new TransactionID * <tt>transactionID</tt> is the ID of a client or a server transaction
* @return a new TransactionID object with the specified id value; * already known to the stack, then this method would return a reference
* to that transaction's instance so that we could use it to for storing
* application data.
*
* @param transactionID the value of the ID.
*
* @return a reference to the (possibly already existing)
* <tt>TransactionID</tt> corresponding to the value of
* <tt>transactionID</tt>
*/ */
public static TransactionID createTransactionID(byte[] transactionID) public static TransactionID createTransactionID(byte[] transactionID)
{ {
//first check whether we can find a client or a server tran with the
//specified id.
StunClientTransaction cliTran = StunStack.getInstance()
.getClientTransaction(transactionID);
if(cliTran != null)
return cliTran.getTransactionID();
StunServerTransaction serTran = StunStack.getInstance()
.getServerTransaction(transactionID);
if(serTran != null)
return serTran.getTransactionID();
//seems that the caller really wants a new ID
TransactionID tid = new TransactionID(); TransactionID tid = new TransactionID();
System.arraycopy(transactionID, 0, tid.transactionID, 0, 12); System.arraycopy(transactionID, 0, tid.transactionID, 0, 12);
@ -89,7 +117,6 @@ public class TransactionID
| (tid.transactionID[1] << 8 & 0x0000FF00) | (tid.transactionID[1] << 8 & 0x0000FF00)
| (tid.transactionID[0] & 0x000000FF); | (tid.transactionID[0] & 0x000000FF);
return tid; return tid;
} }
@ -177,4 +204,18 @@ public class TransactionID
return idStr.toString(); return idStr.toString();
} }
/**
* Stores <tt>applicationData</tt> in this ID so that we can refer back to
* it if we ever need to at a later stage (e.g. when receiving a response
* to a {@link StunClientTransaction}).
*
* @param applicationData a reference to the {@link Object} that the
* application would like to correlate to the transaction represented by
* this ID.
*/
public void setApplicationData(Object applicationData)
{
this.applicationData = applicationData;
}
} }