diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java
index eccfded..f729ed0 100644
--- a/src/org/ice4j/ice/Agent.java
+++ b/src/org/ice4j/ice/Agent.java
@@ -1509,7 +1509,7 @@ public class Agent
//
if(triggerPair.getParentComponent().getSelectedPair() == null)
logger.info("Add peer CandidatePair with new reflexive " +
- "address to checkList");
+ "address to checkList: " + triggerPair);
triggerPair.setUseCandidateReceived();
parentStream.addToCheckList(triggerPair);
}
diff --git a/src/org/ice4j/stack/Connector.java b/src/org/ice4j/stack/Connector.java
index 975e8f8..c36af94 100644
--- a/src/org/ice4j/stack/Connector.java
+++ b/src/org/ice4j/stack/Connector.java
@@ -77,12 +77,14 @@ class Connector
* @param errorHandler the instance to notify when errors occur.
*/
protected Connector(IceSocketWrapper socket,
+ TransportAddress remoteAddress,
MessageQueue messageQueue,
ErrorHandler errorHandler)
{
this.sock = socket;
this.messageQueue = messageQueue;
this.errorHandler = errorHandler;
+ this.remoteAddress = remoteAddress;
Transport transport
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
@@ -91,19 +93,6 @@ class Connector
= new TransportAddress(socket.getLocalAddress(),
socket.getLocalPort(),
transport);
- if (transport == Transport.UDP)
- {
- remoteAddress = null;
- }
- else
- {
- Socket tcpSocket = socket.getTCPSocket();
-
- remoteAddress
- = new TransportAddress(tcpSocket.getInetAddress(),
- tcpSocket.getPort(),
- transport);
- }
}
/**
@@ -335,11 +324,11 @@ class Connector
}
/**
- * Returns the remote TransportAddress in case of TCP, or
- * null in case of UDP.
+ * Returns the remote TransportAddress or null if none
+ * is specified.
*
- * @return the remote TransportAddress in case of TCP, or
- * null in case of UDP.
+ * @return the remote TransportAddress or null if none
+ * is specified.
*/
TransportAddress getRemoteAddress()
{
diff --git a/src/org/ice4j/stack/NetAccessManager.java b/src/org/ice4j/stack/NetAccessManager.java
index c722322..de85f40 100644
--- a/src/org/ice4j/stack/NetAccessManager.java
+++ b/src/org/ice4j/stack/NetAccessManager.java
@@ -36,37 +36,34 @@ class NetAccessManager
= Logger.getLogger(NetAccessManager.class.getName());
/**
- * Synchronization root for access to {@link #netUDPAccessPoints} and
- * {@link #netTCPAccessPoints} and the lists contained in
- * {@link #netTCPAccessPoints}.
- */
- private final Object connectorsSyncRoot = new Object();
-
- /**
- * All access points currently in use with UDP. The table maps local
- * TransportAddresses to Connectors.
+ * All Connectors currently in use with UDP. The table maps a local
+ * TransportAddress and and a remote TransportAddress to
+ * a Connector. We allow a Connector to be added without
+ * a specified remote address, under the null key.
*
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
* cannot override and it causes problem to store both UDP and TCP address
* (i.e. 192.168.0.3:5000/tcp has the same hashcode as 192.168.0.3:5000/udp
* because InetSocketAddress does not take into account transport).
*/
- private Map netUDPAccessPoints
- = new Hashtable();
+ private final Map>
+ udpConnectors
+ = new HashMap>();
/**
- * All access points currently in use with TCP. The table maps local
- * TransportAddresses to a list of Connectors with that
- * local address. To find the required Connector in the list, the
- * remote address needs to be used.
+ * All Connectors currently in use with TCP. The table maps a local
+ * TransportAddress and and a remote TransportAddress to
+ * a Connector. We allow a Connector to be added without
+ * a specified remote address, under the null key.
*
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
* cannot override and it causes problem to store both UDP and TCP address
* (i.e. 192.168.0.3:5000/tcp has the same hashcode as 192.168.0.3:5000/udp
* because InetSocketAddress does not take into account transport).
*/
- private Map> netTCPAccessPoints
- = new Hashtable>();
+ private final Map>
+ tcpConnectors
+ = new HashMap>();
/**
* A synchronized FIFO where incoming messages are stocked for processing.
@@ -243,12 +240,12 @@ class NetAccessManager
{
if (callingThread instanceof Connector)
{
- Connector ap = (Connector)callingThread;
+ Connector connector = (Connector)callingThread;
//make sure nothing's left and notify user
- removeSocket(ap.getListenAddress(),
- ap.getRemoteAddress());
- logger.log(Level.WARNING, "Removing connector:" + ap, error);
+ removeSocket(connector.getListenAddress(),
+ connector.getRemoteAddress());
+ logger.log(Level.WARNING, "Removing connector:" + connector, error);
}
else if( callingThread instanceof MessageProcessor )
{
@@ -277,118 +274,109 @@ class NetAccessManager
protected void addSocket(IceSocketWrapper socket)
{
//no null check - let it through as a NullPointerException
+ Socket tcpSocket = socket.getTCPSocket();
+
+ TransportAddress remoteAddress = null;
+ if (tcpSocket != null)
+ {
+ // In case of TCP we can extract the remote address from the actual
+ // Socket.
+ remoteAddress
+ = new TransportAddress(tcpSocket.getInetAddress(),
+ tcpSocket.getPort(),
+ Transport.TCP);
+ }
+
+ addSocket(socket, remoteAddress);
+ }
+
+ /**
+ * Creates and starts a new access point based on the specified socket.
+ * If the specified access point has already been installed the method
+ * has no effect.
+ *
+ * @param socket the socket that the access point should use.
+ */
+ protected void addSocket(IceSocketWrapper socket,
+ TransportAddress remoteAddress)
+ {
Transport transport
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
- TransportAddress localAddr
+ TransportAddress localAddress
= new TransportAddress(
socket.getLocalAddress(),
socket.getLocalPort(),
transport);
- synchronized (connectorsSyncRoot)
+ final Map>
+ connectorsMap
+ = (transport == Transport.UDP)
+ ? udpConnectors
+ : tcpConnectors;
+
+ synchronized (connectorsMap)
{
- switch (transport)
+ Map connectorsForLocalAddress
+ = connectorsMap.get(localAddress);
+
+ if (connectorsForLocalAddress == null)
{
- case UDP:
- if (!netUDPAccessPoints.containsKey(localAddr))
- {
- Connector connector
- = new Connector(socket, messageQueue, this);
+ connectorsForLocalAddress
+ = new HashMap();
+ connectorsMap.put(localAddress, connectorsForLocalAddress);
+ }
- netUDPAccessPoints.put(localAddr, connector);
- connector.start();
- }
- break;
-
- case TCP:
- List connectors = netTCPAccessPoints.get(localAddr);
-
- if (connectors == null)
- {
- connectors = new LinkedList();
- netTCPAccessPoints.put(localAddr, connectors);
- }
-
- Socket tcpSocket = socket.getTCPSocket();
+ if (!connectorsForLocalAddress.containsKey(remoteAddress))
+ {
Connector connector
- = findTCPConnectorByRemoteAddress(
- connectors,
- new TransportAddress(
- tcpSocket.getInetAddress(),
- tcpSocket.getPort(),
- Transport.TCP));
+ = new Connector(socket, remoteAddress, messageQueue, this);
- if (connector == null)
- {
- connector = new Connector(socket, messageQueue, this);
- connectors.add(connector);
- connector.start();
- }
- break;
+ connectorsForLocalAddress.put(remoteAddress, connector);
+ connector.start();
}
- }
- }
-
- /**
- * From a list of Connectors with TCP sockets finds the one with
- * remote address equal to dst.
- * @param list the list of Connectors to search.
- * @param dst the remote address of the connector to search for.
- * @return the first Connector in list with a remote
- * address equal to dst.
- */
- private Connector findTCPConnectorByRemoteAddress(
- List list,
- TransportAddress dst)
- {
- if (dst == null)
- return null;
-
- for (Connector connector : list)
- {
- if (dst.equals(connector.getRemoteAddress()))
+ else
{
- return connector;
+ logger.info("Not creating a new Connector, because we already "
+ + "have one for the given address pair: "
+ + localAddress + " -> " + remoteAddress);
}
}
-
- return null;
}
/**
* Stops and deletes the specified access point.
*
- * @param src the local address of the connector to remove.
- * @param dst the remote address of the connector to remote. For TCP only,
- * use null for UDP.
+ * @param localAddress the local address of the connector to remove.
+ * @param remoteAddress the remote address of the connector to remote. Use
+ * null to match the Connector with no specified remote
+ * address.
*/
- protected void removeSocket(TransportAddress src, TransportAddress dst)
+ protected void removeSocket(TransportAddress localAddress,
+ TransportAddress remoteAddress)
{
Connector connector = null;
- synchronized (connectorsSyncRoot)
+ final Map>
+ connectorsMap
+ = (localAddress.getTransport() == Transport.UDP)
+ ? udpConnectors
+ : tcpConnectors;
+
+ synchronized (connectorsMap)
{
- switch (src.getTransport())
+ Map connectorsForLocalAddress
+ = connectorsMap.get(localAddress);
+
+ if (connectorsForLocalAddress != null)
{
- case UDP:
- connector = netUDPAccessPoints.remove(src);
- break;
+ connector = connectorsForLocalAddress.get(remoteAddress);
- case TCP:
- List connectors = netTCPAccessPoints.remove(src);
-
- if (connectors != null)
+ if (connector != null)
{
- connector
- = findTCPConnectorByRemoteAddress(connectors, dst);
- if (connector != null)
- {
- connectors.remove(connector);
- if (connectors.isEmpty())
- netTCPAccessPoints.remove(src);
- }
+ connectorsForLocalAddress.remove(remoteAddress);
+ if (connectorsForLocalAddress.isEmpty())
+ connectorsMap.remove(localAddress);
}
- break;
}
}
@@ -398,43 +386,73 @@ class NetAccessManager
/**
* Stops NetAccessManager and all of its MessageProcessor.
- * TODO: close all connectors as well?
*/
+ @SuppressWarnings("unchecked")
public void stop()
{
for(MessageProcessor mp : messageProcessors)
{
mp.stop();
}
+
+ for (Object o : new Object[]{udpConnectors, tcpConnectors})
+ {
+ Map>
+ connectorsMap
+ = (Map>)o;
+
+ synchronized (connectorsMap)
+ {
+ for (Map connectorsForLocalAddress
+ : connectorsMap.values())
+ {
+ for (Connector connector : connectorsForLocalAddress.values())
+ {
+ connector.stop();
+ }
+ }
+ }
+
+ }
}
/**
* Returns the Connector responsible for a particular source
* address and a particular destination address.
*
- * @param src the source address.
- * @param dst the destination address.
+ * @param localAddress the source address.
+ * @param remoteAddress the destination address.
* Returns the Connector responsible for a particular source
* address and a particular destination address, or null if there's
* none.
*/
- private Connector getConnector(TransportAddress src, TransportAddress dst)
+ private Connector getConnector(TransportAddress localAddress,
+ TransportAddress remoteAddress)
{
- synchronized (connectorsSyncRoot)
- {
- switch (src.getTransport())
- {
- case UDP:
- return netUDPAccessPoints.get(src);
- case TCP:
- List connectors = netTCPAccessPoints.get(src);
+ boolean udp = localAddress.getTransport() == Transport.UDP;
+ final Map>
+ connectorsMap
+ = udp
+ ? udpConnectors
+ : tcpConnectors;
+ Connector connector = null;
- if (connectors != null)
- return findTCPConnectorByRemoteAddress(connectors, dst);
+ synchronized (connectorsMap)
+ {
+ Map connectorsForLocalAddress
+ = connectorsMap.get(localAddress);
+
+ if (connectorsForLocalAddress != null)
+ {
+ connector = connectorsForLocalAddress.get(remoteAddress);
+
+ // Fallback to the socket with no specific remote address
+ if (udp && connector == null)
+ connector = connectorsForLocalAddress.get(null);
}
}
- return null;
+ return connector;
}
//---------------thread pool implementation --------------------------------
diff --git a/src/org/ice4j/stack/StunStack.java b/src/org/ice4j/stack/StunStack.java
index d995a10..1ea1294 100644
--- a/src/org/ice4j/stack/StunStack.java
+++ b/src/org/ice4j/stack/StunStack.java
@@ -110,8 +110,8 @@ public class StunStack
}
/**
- * Creates and starts the specified Network Access Point based on the
- * specified socket and returns a relevant descriptor.
+ * Creates and starts a Network Access Point (Connector) based on the
+ * specified socket.
*
* @param sock The socket that the new access point should represent.
*/
@@ -120,6 +120,17 @@ public class StunStack
netAccessManager.addSocket(sock);
}
+ /**
+ * Creates and starts a Network Access Point (Connector) based on the
+ * specified socket and the specified remote address.
+ *
+ * @param sock The socket that the new access point should represent.
+ */
+ public void addSocket(IceSocketWrapper sock, TransportAddress remoteAddress)
+ {
+ netAccessManager.addSocket(sock, remoteAddress);
+ }
+
/**
* Stops and deletes the connector listening on the specified local address.
* Note this removes connectors with UDP sockets only, use
@@ -473,10 +484,6 @@ public class StunStack
getNetAccessManager().sendMessage(
udpMessage.getBytes(), sendThrough, sendTo);
}
- catch (StunException stex)
- {
- throw stex;
- }
catch (IllegalArgumentException iaex)
{
throw new StunException(