More x-stun4j refactoring

This commit is contained in:
emcho 2010-01-31 14:50:19 +00:00
parent fcc4d9eb5c
commit ed3bf01531
3 changed files with 161 additions and 14 deletions

View File

@ -393,4 +393,28 @@ class StunClientTransaction
} }
} }
} }
/**
* Returns the local <tt>TransportAddress</tt> that this transaction is
* sending requests from.
*
* @return the local <tt>TransportAddress</tt> that this transaction is
* sending requests from.
*/
public TransportAddress getLocalAddress()
{
return localAddress;
}
/**
* Returns the remote <tt>TransportAddress</tt> that this transaction is
* sending requests to.
*
* @return the remote <tt>TransportAddress</tt> that this transaction is
* sending requests to.
*/
public TransportAddress getRemoteAddress()
{
return requestDestination;
}
} }

View File

@ -47,19 +47,29 @@ class StunServerTransaction
private StunStack stackCallback = null; private StunStack stackCallback = null;
/** /**
* The source of the transaction request. * The address that we are sending responses to.
*/ */
private TransportAddress responseDestination = null; private TransportAddress responseDestination = null;
/**
* The address that we are receiving requests from.
*/
private TransportAddress requestSource = null;
/** /**
* The response sent in response to the request. * The response sent in response to the request.
*/ */
private Response response = null; private Response response = null;
/**
* The <tt>TransportAddress</tt> that we received our request on.
*/
private TransportAddress localListeningAddress = null;
/** /**
* The <tt>TransportAddress</tt> we use when sending responses * The <tt>TransportAddress</tt> we use when sending responses
*/ */
private TransportAddress localAddress = null; private TransportAddress localSendingAddress = null;
/** /**
* The id of the transaction. * The id of the transaction.
@ -93,13 +103,21 @@ class StunServerTransaction
* @param stackCallback the stack that created us. * @param stackCallback the stack that created us.
* @param tranID the transaction id contained by the request that was the * @param tranID the transaction id contained by the request that was the
* cause for this transaction. * cause for this transaction.
* @param localListeningAddress the <tt>TransportAddress</tt> that this
* transaction is receiving requests on.
* @param requestSource the <tt>TransportAddress</tt> that this
* transaction is receiving requests from.
*/ */
public StunServerTransaction(StunStack stackCallback, public StunServerTransaction(StunStack stackCallback,
TransactionID tranID) TransactionID tranID,
TransportAddress localListeningAddress,
TransportAddress requestSource)
{ {
this.stackCallback = stackCallback; this.stackCallback = stackCallback;
this.transactionID = tranID;
this.transactionID = tranID; this.requestSource = requestSource;
this.localListeningAddress = localListeningAddress;
this.requestSource = requestSource;
runningThread = new Thread(this); runningThread = new Thread(this);
} }
@ -136,9 +154,9 @@ class StunServerTransaction
* the transaction's state to retransmitting. * the transaction's state to retransmitting.
* *
* @param response the response to send the transaction to. * @param response the response to send the transaction to.
* @param sendThrough the local address through which the response is to * @param sendThrough the local address through which responses are to
* be sent * be sent
* @param sendTo the destination of the response. * @param sendTo the destination for responses of this transaction.
* *
* @throws IOException if an error occurs while sending message bytes * @throws IOException if an error occurs while sending message bytes
* through the network socket. * through the network socket.
@ -146,7 +164,7 @@ class StunServerTransaction
* access point that had not been installed, * access point that had not been installed,
* @throws StunException if message encoding fails, * @throws StunException if message encoding fails,
*/ */
public void sendResponse(Response response, public void sendResponse(Response response,
TransportAddress sendThrough, TransportAddress sendThrough,
TransportAddress sendTo) TransportAddress sendTo)
throws StunException, throws StunException,
@ -158,8 +176,8 @@ class StunServerTransaction
//the transaction id might already have been set, but its our job //the transaction id might already have been set, but its our job
//to make sure of that //to make sure of that
response.setTransactionID(this.transactionID.getTransactionID()); response.setTransactionID(this.transactionID.getTransactionID());
this.localAddress = sendThrough; this.localSendingAddress = sendThrough;
this.responseDestination = sendTo; this.responseDestination = sendTo;
} }
isRetransmitting = true; isRetransmitting = true;
@ -187,7 +205,7 @@ class StunServerTransaction
return; return;
stackCallback.getNetAccessManager().sendMessage(response, stackCallback.getNetAccessManager().sendMessage(response,
localAddress, localSendingAddress,
responseDestination); responseDestination);
} }
@ -257,4 +275,52 @@ class StunServerTransaction
{ {
return isRetransmitting; return isRetransmitting;
} }
/**
* Returns the local <tt>TransportAddress</tt> that this transaction is
* sending responses from.
*
* @return the local <tt>TransportAddress</tt> that this transaction is
* sending responses from.
*/
public TransportAddress getSendingAddress()
{
return localSendingAddress;
}
/**
* Returns the remote <tt>TransportAddress</tt> that this transaction is
* receiving requests from.
*
* @return the remote <tt>TransportAddress</tt> that this transaction is
* receiving requests from.
*/
public TransportAddress getResponseDestinationAddress()
{
return responseDestination;
}
/**
* Returns the local <tt>TransportAddress</tt> that this transaction is
* receiving requests on.
*
* @return the local <tt>TransportAddress</tt> that this transaction is
* receiving requests on.
*/
public TransportAddress getLocalListeningAddress()
{
return localListeningAddress;
}
/**
* Returns the remote <tt>TransportAddress</tt> that this transaction is
* receiving requests from.
*
* @return the remote <tt>TransportAddress</tt> that this transaction is
* receiving requests from.
*/
public TransportAddress getRequestSourceAddress()
{
return requestSource;
}
} }

View File

@ -109,9 +109,66 @@ public class StunStack
*/ */
public void removeSocket(TransportAddress localAddr) public void removeSocket(TransportAddress localAddr)
{ {
//first cancel all transactions using this address.
cancelTransactionsForAddress(localAddr);
netAccessManager.removeSocket(localAddr); netAccessManager.removeSocket(localAddr);
}
/**
* Stops all transactions for the specified <tt>localAddr</tt> so that they
* won't send messages through any longer and so that we could remove the
* associated socket.
*
* @param localAddr the <tt>TransportAddress</tt> that we'd like to remove
* transactions for.
*/
private void cancelTransactionsForAddress(TransportAddress localAddr)
{
synchronized(clientTransactions)
{
Iterator<Map.Entry<TransactionID, StunClientTransaction>>
clientTransactionsIter = clientTransactions.entrySet()
.iterator();
while (clientTransactionsIter.hasNext())
{
Map.Entry<TransactionID, StunClientTransaction> entry
= clientTransactionsIter.next();
StunClientTransaction tran = entry.getValue();
if (tran.getLocalAddress().equals(localAddr))
clientTransactionsIter.remove();
tran.cancel();
}
}
synchronized(serverTransactions)
{
Iterator<Map.Entry<TransactionID, StunServerTransaction>>
serverTransactionsIter = serverTransactions.entrySet()
.iterator();
while (serverTransactionsIter.hasNext())
{
Map.Entry<TransactionID, StunServerTransaction> entry
= serverTransactionsIter.next();
StunServerTransaction tran = entry.getValue();
TransportAddress listenAddr = tran.getLocalListeningAddress();
TransportAddress sendingAddr = tran.getSendingAddress();
if ( listenAddr.equals(localAddr)
|| (sendingAddr != null && sendingAddr.equals(localAddr)) )
{
serverTransactionsIter.remove();
}
tran.expire();
}
}
} }
/** /**
@ -318,8 +375,8 @@ public class StunStack
else else
{ {
logger.finest("exising transaction not found"); logger.finest("exising transaction not found");
sTran = sTran = new StunServerTransaction(this, serverTid,
new StunServerTransaction(this, serverTid); event.getLocalAddress(), event.getRemoteAddress());
serverTransactions.put(serverTid, sTran); serverTransactions.put(serverTid, sTran);
sTran.start(); sTran.start();