From f0a6ba00c9052e1fc35022c74c8a8755e6f06b9b Mon Sep 17 00:00:00 2001 From: emcho Date: Sat, 1 May 2010 21:35:47 +0000 Subject: [PATCH] Adds support for storing application data in transaction IDs so that we could keep in there references to CandidatePair-s --- src/org/ice4j/BaseStunMessageEvent.java | 13 ++- src/org/ice4j/ResponseCollector.java | 9 -- src/org/ice4j/StunTimeoutEvent.java | 6 +- src/org/ice4j/ice/Agent.java | 1 - .../ice/checks/ConnectivityCheckClient.java | 55 +++++++----- .../ice4j/stack/StunClientTransaction.java | 42 +++++++-- src/org/ice4j/stack/StunStack.java | 90 +++++++++++++++++-- src/org/ice4j/stack/TransactionID.java | 51 +++++++++-- 8 files changed, 216 insertions(+), 51 deletions(-) diff --git a/src/org/ice4j/BaseStunMessageEvent.java b/src/org/ice4j/BaseStunMessageEvent.java index 34aecaa..91136fb 100644 --- a/src/org/ice4j/BaseStunMessageEvent.java +++ b/src/org/ice4j/BaseStunMessageEvent.java @@ -33,7 +33,7 @@ public class BaseStunMessageEvent /** * The ID of the transaction related to {@link #message}. */ - private TransactionID transactionID; + private TransactionID transactionID = null; /** * Initializes a new BaseStunMessageEvent associated with a @@ -85,4 +85,15 @@ public class BaseStunMessageEvent .createTransactionID(getMessage().getTransactionID()); 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; + } } diff --git a/src/org/ice4j/ResponseCollector.java b/src/org/ice4j/ResponseCollector.java index d3bbca0..afba139 100644 --- a/src/org/ice4j/ResponseCollector.java +++ b/src/org/ice4j/ResponseCollector.java @@ -31,13 +31,4 @@ public interface ResponseCollector */ 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 StunFailureEvent containing the - * PortUnreachableException that has just occurred. - */ - public void processUnreachable(StunFailureEvent event); } diff --git a/src/org/ice4j/StunTimeoutEvent.java b/src/org/ice4j/StunTimeoutEvent.java index 03de275..794d6eb 100644 --- a/src/org/ice4j/StunTimeoutEvent.java +++ b/src/org/ice4j/StunTimeoutEvent.java @@ -7,6 +7,7 @@ package org.ice4j; import org.ice4j.message.*; +import org.ice4j.stack.*; /** * 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 localAddress the local address that the message was sent from. + * @param transactionID the ID of the associated with this event. */ public StunTimeoutEvent(Message message, - TransportAddress localAddress) + TransportAddress localAddress, + TransactionID transactionID) { super(localAddress, message); + super.setTransactionID(transactionID); } /** diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 4c57712..ffba09a 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -355,7 +355,6 @@ public class Agent streams.get(0).getCheckList().computeInitialCheckListPairStates(); } - /** * Computes and sets the foundations foundation for all Candidates * currently found in component. diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index 6a7edb3..9319219 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -77,19 +77,32 @@ public class ConnectivityCheckClient 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 + * candidatePair 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 null 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 //already verified during the gathering process. - DatagramSocket stunSocket = ((HostCandidate)pair.getLocalCandidate()) - .getStunSocket(null); + DatagramSocket stunSocket = ((HostCandidate)candidatePair + .getLocalCandidate()) .getStunSocket(null); 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( - pair.getLocalCandidate().computePriorityForType( + candidatePair.getLocalCandidate().computePriorityForType( CandidateType.PEER_REFLEXIVE_CANDIDATE)); request.addAttribute(priority); @@ -115,26 +128,24 @@ public class ConnectivityCheckClient 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 MessageIntegrityAttribute msgIntegrity = AttributeFactory .createMessageIntegrityAttribute(localUserName); - request.addAttribute(msgIntegrity); - TransactionID tran; + TransactionID tran = TransactionID.createNewTransactionID(); + tran.setApplicationData(candidatePair); try { - tran = stunStack.sendRequest(request, - pair.getRemoteCandidate().getTransportAddress(), stunSocket, - this); + stunStack.sendRequest(request, + candidatePair.getRemoteCandidate().getTransportAddress(), + candidatePair.getLocalCandidate().getTransportAddress(), + this, tran); if(logger.isLoggable(Level.FINEST)) - logger.finest("checking pair " + pair + " with tran=" + logger.finest("checking pair " + candidatePair + " with tran=" + tran.toString()); return tran; @@ -159,18 +170,18 @@ public class ConnectivityCheckClient 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 * in the pace defined in RFC 5245. */ private static class PaceMaker extends Thread { + /** + * Indicates whether this thread is still running. Not really used at + * this point. + */ public boolean isRunning = true; + /** * The {@link ConnectivityCheckClient} that created us. */ @@ -225,6 +236,9 @@ public class ConnectivityCheckClient { logger.log(Level.FINER, "PaceMake got interrupted", e); } + + if (!isRunning) + return; } checkList.setState(CheckListState.RUNNING); @@ -242,6 +256,7 @@ public class ConnectivityCheckClient //we are done sending checks for this list. we'll send its //final state in either the processResponse() //processTimeout() or processFailure() method. + isRunning = false; return; } diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index bfa1fff..0298ea0 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -142,11 +142,36 @@ class StunClientTransaction * @param responseCollector the instance that should receive this request's * response retransmit. */ - public StunClientTransaction(StunStack stackCallback, - Request request, - TransportAddress requestDestination, - TransportAddress localAddress, - ResponseCollector responseCollector) + public StunClientTransaction(StunStack stackCallback, + Request request, + TransportAddress requestDestination, + TransportAddress localAddress, + 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 TransportAddress 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.request = request; @@ -156,7 +181,8 @@ class StunClientTransaction initTransactionConfiguration(); - this.transactionID = TransactionID.createTransactionID(); + this.transactionID = transactionID; + try { request.setTransactionID(transactionID.getTransactionID()); @@ -220,8 +246,8 @@ class StunClientTransaction waitFor(nextWaitInterval); - responseCollector.processTimeout( - new StunTimeoutEvent(this.request, getLocalAddress())); + responseCollector.processTimeout( new StunTimeoutEvent( + this.request, getLocalAddress(), transactionID)); stackCallback.removeClientTransaction(this); } diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index 67f1a91..f79fa30 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -123,6 +123,54 @@ public class StunStack netAccessManager.removeSocket(localAddr); } + /** + * Returns the transaction with the specified transactionID or + * null 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 cTrans + = clientTransactions.values(); + + for (StunClientTransaction tran : cTrans) + { + if (tran.getTransactionID().equals(transactionID)) + return tran; + } + } + return null; + } + + /** + * Returns the transaction with the specified transactionID or + * null 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 sTrans + = serverTransactions.values(); + + for (StunServerTransaction tran : sTrans) + { + if (tran.getTransactionID().equals(transactionID)) + return tran; + } + } + return null; + } + /** * Cancels the {@link StunClientTransaction} with the specified * transactionID. Cancellation means that the stack will not @@ -237,13 +285,44 @@ public class StunStack TransportAddress sendThrough, ResponseCollector collector ) 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 TransactionID of the StunClientTransaction + * 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 = new StunClientTransaction(this, request, sendTo, sendThrough, - collector); + collector, transactionID); clientTransactions.put(clientTransaction.getTransactionID(), - clientTransaction); + clientTransaction); clientTransaction.sendRequest(); @@ -406,8 +485,8 @@ public class StunStack if(msg instanceof Request) { logger.finest("parsing request"); - TransactionID serverTid = TransactionID. - createTransactionID(msg.getTransactionID()); + + TransactionID serverTid = event.getTransactionID(); StunServerTransaction sTran = serverTransactions.get(serverTid); if( sTran != null) @@ -450,8 +529,7 @@ public class StunStack //response else if(msg instanceof Response) { - TransactionID tid - = TransactionID.createTransactionID(msg.getTransactionID()); + TransactionID tid = event.getTransactionID(); StunClientTransaction tran = clientTransactions.remove(tid); if(tran != null) diff --git a/src/org/ice4j/stack/TransactionID.java b/src/org/ice4j/stack/TransactionID.java index e4a25cc..39e3a43 100644 --- a/src/org/ice4j/stack/TransactionID.java +++ b/src/org/ice4j/stack/TransactionID.java @@ -22,6 +22,11 @@ public class TransactionID */ 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. */ @@ -50,7 +55,7 @@ public class TransactionID * * @return A TransactionID object with a unique transaction id. */ - public static TransactionID createTransactionID() + public static TransactionID createNewTransactionID() { TransactionID tid = new TransactionID(); @@ -73,12 +78,35 @@ public class TransactionID } /** - * Creates a transaction identifier object with the specified id. - * @param transactionID the id to give to the new TransactionID - * @return a new TransactionID object with the specified id value; + * Returns a TransactionID instance for the specified id. If + * transactionID is the ID of a client or a server transaction + * 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) + * TransactionID corresponding to the value of + * 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(); System.arraycopy(transactionID, 0, tid.transactionID, 0, 12); @@ -89,7 +117,6 @@ public class TransactionID | (tid.transactionID[1] << 8 & 0x0000FF00) | (tid.transactionID[0] & 0x000000FF); - return tid; } @@ -177,4 +204,18 @@ public class TransactionID return idStr.toString(); } + + /** + * Stores applicationData 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; + } }