diff --git a/src/org/ice4j/stack/StunClientTransaction.java b/src/org/ice4j/stack/StunClientTransaction.java index c64ea87..7db0cdc 100644 --- a/src/org/ice4j/stack/StunClientTransaction.java +++ b/src/org/ice4j/stack/StunClientTransaction.java @@ -91,9 +91,9 @@ class StunClientTransaction private int nextWaitInterval = originalWaitInterval; /** - * The StunProvider that created us. + * The StunStack that created us. */ - private StunProvider providerCallback = null; + private StunStack stackCallback = null; /** * The request that we are retransmitting. @@ -133,8 +133,9 @@ class StunClientTransaction private Thread retransmissionsThread = null; /** - * Creates a client transaction - * @param providerCallback the provider that created us. + * 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 @@ -142,16 +143,16 @@ class StunClientTransaction * @param responseCollector the instance that should receive this request's * response retransmit. */ - public StunClientTransaction(StunProvider providerCallback, + public StunClientTransaction(StunStack stackCallback, Request request, TransportAddress requestDestination, TransportAddress localAddress, ResponseCollector responseCollector) { - this.providerCallback = providerCallback; - this.request = request; - this.localAddress = localAddress; - this.responseCollector = responseCollector; + this.stackCallback = stackCallback; + this.request = request; + this.localAddress = localAddress; + this.responseCollector = responseCollector; this.requestDestination = requestDestination; initTransactionConfiguration(); @@ -219,7 +220,7 @@ class StunClientTransaction waitFor(nextWaitInterval); responseCollector.processTimeout(); - providerCallback.removeClientTransaction(this); + stackCallback.removeClientTransaction(this); } @@ -259,7 +260,7 @@ class StunClientTransaction logger.finer("Trying to resend a cancelled transaction."); return; } - providerCallback.getNetAccessManager().sendMessage( + stackCallback.getNetAccessManager().sendMessage( this.request, localAddress, requestDestination); @@ -304,8 +305,9 @@ class StunClientTransaction } /** - * Dispatches the response then cancels itself and notifies the StunProvider + * Dispatches the response then cancels itself and notifies the StunStack * for its termination. + * * @param evt the event that contains the newly received message */ void handleResponse(StunMessageEvent evt) diff --git a/src/org/ice4j/stack/StunProvider.java b/src/org/ice4j/stack/StunProvider.java deleted file mode 100644 index 93f16ca..0000000 --- a/src/org/ice4j/stack/StunProvider.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * 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.stack; - -import java.io.*; -import java.util.*; -import java.util.logging.*; - -import org.ice4j.*; -import org.ice4j.message.*; - -/** - * The StunProvider class is an implementation of a Stun Transaction Layer. STUN - * transactions are extremely simple and are only used to correlate requests and - * responses. In the Stun4J implementation it is the transaction layer that - * ensures reliable delivery. - * - * @author Emil Ivov - */ - -public class StunProvider - implements MessageEventHandler -{ - /** - * Our class logger. - */ - private static final Logger logger = - Logger.getLogger(StunProvider.class.getName()); - - /** - * Stores active client transactions mapped against TransactionID-s. - */ - private Hashtable clientTransactions - = new Hashtable(); - - /** - * Currently open server transactions. The vector contains transaction ids - * for transactions corresponding to all non-answered received requests. - */ - private Hashtable serverTransactions - = new Hashtable(); - - /** - * The stack that created us. - */ - private StunStack stunStack = null; - - /** - * A dispatcher for incoming requests event; - */ - private EventDispatcher eventDispatcher = new EventDispatcher(); - - //------------------ public interface - /** - * Creates the provider. - * @param stunStack The currently active stack instance. - */ - StunProvider(StunStack stunStack) - { - this.stunStack = stunStack; - } - - /** - * 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 - * - * @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, - * @throws StunException if message encoding fails, - * - */ - public void sendRequest( Request request, - TransportAddress sendTo, - TransportAddress sendThrough, - ResponseCollector collector ) - throws StunException, IOException, IllegalArgumentException - { - StunClientTransaction clientTransaction - = new StunClientTransaction(this, request, sendTo, sendThrough, - collector); - - clientTransactions.put(clientTransaction.getTransactionID(), - clientTransaction); - - clientTransaction.sendRequest(); - } - - /** - * Sends the specified response message through the specified access point. - * - * @param transactionID the id of the transaction to use when sending the - * response. Actually we are getting kind of redundant here as we already - * have the id in the response object, but I am bringing out as an extra - * parameter as the user might otherwise forget to explicitly set it. - * @param response the message to send. - * @param sendThrough the local address to use when sending the message. - * @param sendTo the destination of the message. - * - * @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, - * @throws StunException if message encoding fails, - */ - public void sendResponse(byte[] transactionID, - Response response, - TransportAddress sendThrough, - TransportAddress sendTo) - throws StunException, - IOException, - IllegalArgumentException - { - TransactionID tid = TransactionID.createTransactionID(transactionID); - StunServerTransaction sTran = - serverTransactions.get(tid); - - if(sTran == null || sTran.isReransmitting()) - { - throw new StunException(StunException.TRANSACTION_DOES_NOT_EXIST, - "The transaction specified in the response " - + "object does not exist or has already " - + "transmitted a response."); - } - else - { - sTran.sendResponse(response, sendThrough, sendTo); - } - - - - } - - /** - * Sets the listener that should be notified when a new Request is received. - * @param requestListener the listener interested in incoming requests. - */ - public void addRequestListener(RequestListener requestListener) - { - this.eventDispatcher.addRequestListener( requestListener ); - } - - /** - * Removes the specified listener from the local listener list. (If any - * instances of this listener have been registered for a particular - * access point, they will not be removed). - * @param listener the RequestListener listener to unregister - */ - public void removeRequestListener(RequestListener listener) - { - this.eventDispatcher.removeRequestListener(listener); - } - - /** - * Add a RequestListener for requests coming from a specific NetAccessPoint. - * The listener will be invoked only when a request event is received on - * that specific property. - * - * @param localAddress The local TransportAddress that we would - * like to listen on. - * @param listener The ConfigurationChangeListener to be added - */ - - public void addRequestListener( TransportAddress localAddress, - RequestListener listener) - { - synchronized(eventDispatcher) - { - eventDispatcher.addRequestListener(localAddress, listener); - } - } - -//------------- stack internals ------------------------------------------------ - /** - * Returns the currently active instance of NetAccessManager. Used by client - * transactions when sending messages. - * @return the currently active instance of NetAccessManager. - */ - NetAccessManager getNetAccessManager() - { - return stunStack.getNetAccessManager(); - } - - /** - * Removes a client transaction from this providers client transactions list. - * Method is used by StunClientTransaction-s themselves when a timeout occurs. - * @param tran the transaction to remove. - */ - synchronized void removeClientTransaction(StunClientTransaction tran) - { - clientTransactions.remove(tran.getTransactionID()); - } - - /** - * Removes a server transaction from this provider's server transactions - * list. - * Method is used by StunServerTransaction-s themselves when they expire. - * @param tran the transaction to remove. - */ - synchronized void removeServerTransaction(StunServerTransaction tran) - { - serverTransactions.remove(tran.getTransactionID()); - } - - /** - * Called to notify this provider for an incoming message. - * - * @param event the event object that contains the new message. - */ - public void handleMessageEvent(StunMessageEvent event) - { - Message msg = event.getMessage(); - - if(logger.isLoggable(Level.FINEST)) - logger.finest("Received a message on NetAP" - + event.getLocalAddress() - + " of type:" - + (int)msg.getMessageType()); - - //request - if(msg instanceof Request) - { - logger.finest("parsing request"); - TransactionID serverTid = TransactionID. - createTransactionID(msg.getTransactionID()); - - StunServerTransaction sTran = serverTransactions.get(serverTid); - if( sTran != null) - { - - //requests from this transaction have already been seen - //retransmit the response if there was any - logger.finest("found an existing transaction"); - - try - { - sTran.retransmitResponse(); - logger.finest("Response retransmitted"); - } - catch (Exception ex) - { - //we couldn't really do anything here .. apart from logging - logger.log(Level.WARNING, - "Failed to retransmit a stun response", ex); - } - - String propagate = System.getProperty( - "org.ice4j.PROPAGATE_RECEIVED_RETRANSMISSIONS"); - if(propagate == null - || !propagate.trim().equalsIgnoreCase("true")) - { - return; - } - } - else - { - logger.finest("exising transaction not found"); - sTran = - new StunServerTransaction(this, serverTid); - - serverTransactions.put(serverTid, sTran); - sTran.start(); - } - - eventDispatcher.fireMessageEvent(event); - } - //response - else if(msg instanceof Response) - { - TransactionID tid - = TransactionID.createTransactionID(msg.getTransactionID()); - - StunClientTransaction tran = - clientTransactions.remove(tid); - - if(tran != null) - { - tran.handleResponse(event); - } - else - { - //do nothing - just drop the phantom response. - logger.fine("Dropped response - " - + "no matching client tran found."); - logger.fine("response tid was - " + tid.toString()); - logger.fine("all tids in stock were" - + clientTransactions.toString()); - } - } - - } - - /** - * Cancels all running transactions and prepares for garbage collection - */ - void shutDown() - { - eventDispatcher.removeAllListeners(); - - Enumeration tids = clientTransactions.keys(); - while (tids.hasMoreElements()) - { - TransactionID item = (TransactionID)tids.nextElement(); - StunClientTransaction tran = clientTransactions.remove(item); - if(tran != null) - tran.cancel(); - - } - - tids = serverTransactions.keys(); - while (tids.hasMoreElements()) - { - TransactionID item = (TransactionID)tids.nextElement(); - StunServerTransaction tran = serverTransactions.remove(item); - if(tran != null) - tran.expire(); - - } - } - -} diff --git a/src/org/ice4j/stack/StunServerTransaction.java b/src/org/ice4j/stack/StunServerTransaction.java index 1967586..49bbbdb 100644 --- a/src/org/ice4j/stack/StunServerTransaction.java +++ b/src/org/ice4j/stack/StunServerTransaction.java @@ -42,9 +42,9 @@ class StunServerTransaction private long transactionLifetime = 16000; /** - * The StunProvider that created us. + * The StunStack that created us. */ - private StunProvider providerCallback = null; + private StunStack stackCallback = null; /** * The source of the transaction request. @@ -90,14 +90,14 @@ class StunServerTransaction /** * Creates a server transaction - * @param providerCallback the provider that created us. + * @param stackCallback the stack that created us. * @param tranID the transaction id contained by the request that was the * cause for this transaction. */ - public StunServerTransaction(StunProvider providerCallback, - TransactionID tranID) + public StunServerTransaction(StunStack stackCallback, + TransactionID tranID) { - this.providerCallback = providerCallback; + this.stackCallback = stackCallback; this.transactionID = tranID; @@ -127,7 +127,7 @@ class StunServerTransaction //let's get lost expire(); - providerCallback.removeServerTransaction(this); + stackCallback.removeServerTransaction(this); } /** @@ -186,7 +186,7 @@ class StunServerTransaction if(expired || !isRetransmitting) return; - providerCallback.getNetAccessManager().sendMessage(response, + stackCallback.getNetAccessManager().sendMessage(response, localAddress, responseDestination); } diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java index a20f030..814fddf 100644 --- a/src/org/ice4j/stack/StunStack.java +++ b/src/org/ice4j/stack/StunStack.java @@ -8,8 +8,11 @@ package org.ice4j.stack; import java.io.*; import java.net.*; +import java.util.*; +import java.util.logging.*; import org.ice4j.*; +import org.ice4j.message.*; /** * The entry point to the Stun4J stack. The class is used to start, stop and * configure the stack. @@ -17,6 +20,7 @@ import org.ice4j.*; * @author Emil Ivov */ public class StunStack + implements MessageEventHandler { /** * We shouldn't need more than one stack in the same application. @@ -34,9 +38,29 @@ public class StunStack public static final int DEFAULT_THREAD_POOL_SIZE = 3; /** - * This stack's provider. + * Our class logger. */ - private StunProvider stunProvider = null; + private static final Logger logger = + Logger.getLogger(StunStack.class.getName()); + + /** + * Stores active client transactions mapped against TransactionID-s. + */ + private Hashtable clientTransactions + = new Hashtable(); + + /** + * Currently open server transactions. The vector contains transaction ids + * for transactions corresponding to all non-answered received requests. + */ + private Hashtable serverTransactions + = new Hashtable(); + + /** + * A dispatcher for incoming requests event; + */ + private EventDispatcher eventDispatcher = new EventDispatcher(); + /** * Returns a reference to the singleton StunStack instance. If the stack @@ -90,27 +114,12 @@ public class StunStack } - - - /** - * Returns a StunProvider instance to be used for sending and receiving - * messages. - * - * @return an instance of StunProvider - */ - public StunProvider getProvider() - { - return stunProvider; - } - /** * Private constructor as we want a singleton pattern. */ private StunStack() { - stunProvider = new StunProvider(this); - - netAccessManager = new NetAccessManager(stunProvider); + netAccessManager = new NetAccessManager(this); } /** @@ -121,4 +130,257 @@ public class StunStack { return netAccessManager; } + + /** + * 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 + * + * @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, + * @throws StunException if message encoding fails, + * + */ + public void sendRequest( Request request, + TransportAddress sendTo, + TransportAddress sendThrough, + ResponseCollector collector ) + throws StunException, IOException, IllegalArgumentException + { + StunClientTransaction clientTransaction + = new StunClientTransaction(this, request, sendTo, sendThrough, + collector); + + clientTransactions.put(clientTransaction.getTransactionID(), + clientTransaction); + + clientTransaction.sendRequest(); + } + + /** + * Sends the specified response message through the specified access point. + * + * @param transactionID the id of the transaction to use when sending the + * response. Actually we are getting kind of redundant here as we already + * have the id in the response object, but I am bringing out as an extra + * parameter as the user might otherwise forget to explicitly set it. + * @param response the message to send. + * @param sendThrough the local address to use when sending the message. + * @param sendTo the destination of the message. + * + * @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, + * @throws StunException if message encoding fails, + */ + public void sendResponse(byte[] transactionID, + Response response, + TransportAddress sendThrough, + TransportAddress sendTo) + throws StunException, + IOException, + IllegalArgumentException + { + TransactionID tid = TransactionID.createTransactionID(transactionID); + StunServerTransaction sTran = + serverTransactions.get(tid); + + if(sTran == null || sTran.isReransmitting()) + { + throw new StunException(StunException.TRANSACTION_DOES_NOT_EXIST, + "The transaction specified in the response " + + "object does not exist or has already " + + "transmitted a response."); + } + else + { + sTran.sendResponse(response, sendThrough, sendTo); + } + + + + } + + /** + * Sets the listener that should be notified when a new Request is received. + * @param requestListener the listener interested in incoming requests. + */ + public void addRequestListener(RequestListener requestListener) + { + this.eventDispatcher.addRequestListener( requestListener ); + } + + /** + * Removes the specified listener from the local listener list. (If any + * instances of this listener have been registered for a particular + * access point, they will not be removed). + * @param listener the RequestListener listener to unregister + */ + public void removeRequestListener(RequestListener listener) + { + this.eventDispatcher.removeRequestListener(listener); + } + + /** + * Add a RequestListener for requests coming from a specific NetAccessPoint. + * The listener will be invoked only when a request event is received on + * that specific property. + * + * @param localAddress The local TransportAddress that we would + * like to listen on. + * @param listener The ConfigurationChangeListener to be added + */ + + public void addRequestListener( TransportAddress localAddress, + RequestListener listener) + { + synchronized(eventDispatcher) + { + eventDispatcher.addRequestListener(localAddress, listener); + } + } + + /** + * Removes a client transaction from this providers client transactions list. + * Method is used by StunClientTransaction-s themselves when a timeout occurs. + * @param tran the transaction to remove. + */ + synchronized void removeClientTransaction(StunClientTransaction tran) + { + clientTransactions.remove(tran.getTransactionID()); + } + + /** + * Removes a server transaction from this provider's server transactions + * list. + * Method is used by StunServerTransaction-s themselves when they expire. + * @param tran the transaction to remove. + */ + synchronized void removeServerTransaction(StunServerTransaction tran) + { + serverTransactions.remove(tran.getTransactionID()); + } + + /** + * Called to notify this provider for an incoming message. + * + * @param event the event object that contains the new message. + */ + public void handleMessageEvent(StunMessageEvent event) + { + Message msg = event.getMessage(); + + if(logger.isLoggable(Level.FINEST)) + logger.finest("Received a message on NetAP" + + event.getLocalAddress() + + " of type:" + + (int)msg.getMessageType()); + + //request + if(msg instanceof Request) + { + logger.finest("parsing request"); + TransactionID serverTid = TransactionID. + createTransactionID(msg.getTransactionID()); + + StunServerTransaction sTran = serverTransactions.get(serverTid); + if( sTran != null) + { + + //requests from this transaction have already been seen + //retransmit the response if there was any + logger.finest("found an existing transaction"); + + try + { + sTran.retransmitResponse(); + logger.finest("Response retransmitted"); + } + catch (Exception ex) + { + //we couldn't really do anything here .. apart from logging + logger.log(Level.WARNING, + "Failed to retransmit a stun response", ex); + } + + String propagate = System.getProperty( + "org.ice4j.PROPAGATE_RECEIVED_RETRANSMISSIONS"); + if(propagate == null + || !propagate.trim().equalsIgnoreCase("true")) + { + return; + } + } + else + { + logger.finest("exising transaction not found"); + sTran = + new StunServerTransaction(this, serverTid); + + serverTransactions.put(serverTid, sTran); + sTran.start(); + } + + eventDispatcher.fireMessageEvent(event); + } + //response + else if(msg instanceof Response) + { + TransactionID tid + = TransactionID.createTransactionID(msg.getTransactionID()); + + StunClientTransaction tran = + clientTransactions.remove(tid); + + if(tran != null) + { + tran.handleResponse(event); + } + else + { + //do nothing - just drop the phantom response. + logger.fine("Dropped response - " + + "no matching client tran found."); + logger.fine("response tid was - " + tid.toString()); + logger.fine("all tids in stock were" + + clientTransactions.toString()); + } + } + + } + + /** + * Cancels all running transactions and prepares for garbage collection + */ + public void shutDown() + { + eventDispatcher.removeAllListeners(); + + Enumeration tids = clientTransactions.keys(); + while (tids.hasMoreElements()) + { + TransactionID item = (TransactionID)tids.nextElement(); + StunClientTransaction tran = clientTransactions.remove(item); + if(tran != null) + tran.cancel(); + + } + + tids = serverTransactions.keys(); + while (tids.hasMoreElements()) + { + TransactionID item = (TransactionID)tids.nextElement(); + StunServerTransaction tran = serverTransactions.remove(item); + if(tran != null) + tran.expire(); + + } + } } diff --git a/src/org/ice4j/stunclient/BlockingRequestSender.java b/src/org/ice4j/stunclient/BlockingRequestSender.java index c711a24..05b0201 100644 --- a/src/org/ice4j/stunclient/BlockingRequestSender.java +++ b/src/org/ice4j/stunclient/BlockingRequestSender.java @@ -42,9 +42,9 @@ class BlockingRequestSender = Logger.getLogger(BlockingRequestSender.class.getName()); /** - * The provider that we are using to send requests through. + * The stack that we are using to send requests through. */ - private StunProvider stunProvider = null; + private StunStack stunStack = null; /** * The transport address that we are bound on. @@ -69,15 +69,15 @@ class BlockingRequestSender /** * Creates a new request sender. - * @param stunProvider the provider that the sender should send requests + * @param stunStack the stack that the sender should send requests * through. * @param localAddress the TransportAddress that requests should be * leaving from. */ - BlockingRequestSender(StunProvider stunProvider, + BlockingRequestSender(StunStack stunStack, TransportAddress localAddress) { - this.stunProvider = stunProvider; + this.stunStack = stunStack; this.localAddress = localAddress; } @@ -128,7 +128,7 @@ class BlockingRequestSender IOException { synchronized(sendLock){ - stunProvider.sendRequest(request, serverAddress, localAddress, + stunStack.sendRequest(request, serverAddress, localAddress, BlockingRequestSender.this); } diff --git a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java index 9d40aca..588e6a9 100644 --- a/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java +++ b/src/org/ice4j/stunclient/NetworkConfigurationDiscoveryProcess.java @@ -100,11 +100,6 @@ public class NetworkConfigurationDiscoveryProcess */ private StunStack stunStack = null; - /** - * The provider to send our messages through - */ - private StunProvider stunProvider = null; - /** * The point where we'll be listening. */ @@ -149,7 +144,6 @@ public class NetworkConfigurationDiscoveryProcess public void shutDown() { stunStack = null; - stunProvider = null; localAddress = null; requestSender = null; @@ -171,8 +165,7 @@ public class NetworkConfigurationDiscoveryProcess stunStack.addSocket(sock); - stunProvider = stunStack.getProvider(); - requestSender = new BlockingRequestSender(stunProvider, localAddress); + requestSender = new BlockingRequestSender(stunStack, localAddress); started = true; } diff --git a/src/org/ice4j/stunclient/SimpleAddressDetector.java b/src/org/ice4j/stunclient/SimpleAddressDetector.java index 16edc12..d08177b 100644 --- a/src/org/ice4j/stunclient/SimpleAddressDetector.java +++ b/src/org/ice4j/stunclient/SimpleAddressDetector.java @@ -36,11 +36,6 @@ public class SimpleAddressDetector */ private StunStack stunStack = null; - /** - * The provider to send our messages through - */ - private StunProvider stunProvider = null; - /** * The address of the stun server */ @@ -81,7 +76,6 @@ public class SimpleAddressDetector public void shutDown() { stunStack = null; - stunProvider = null; requestSender = null; } @@ -91,8 +85,6 @@ public class SimpleAddressDetector public void start() { stunStack = StunStack.getInstance(); - - stunProvider = stunStack.getProvider(); } @@ -114,7 +106,7 @@ public class SimpleAddressDetector stunStack.addSocket(socket); - requestSender = new BlockingRequestSender(stunProvider, localAddress); + requestSender = new BlockingRequestSender(stunStack, localAddress); StunMessageEvent evt = null; try {