Saves TCP sockets in NetAccessManager on the basis of both the local and remote address, in order to allow multiple Sockets/Connectors for a single local address.
This commit is contained in:
parent
3c34615d2b
commit
026bfff995
@ -62,6 +62,12 @@ class Connector
|
||||
*/
|
||||
private final TransportAddress listenAddress;
|
||||
|
||||
/**
|
||||
* The remote address of the socket of this <tt>Connector</tt> if it is
|
||||
* a TCP socket, or <tt>null</tt> if it is UDP.
|
||||
*/
|
||||
private final TransportAddress remoteAddress;
|
||||
|
||||
/**
|
||||
* Creates a network access point.
|
||||
* @param socket the socket that this access point is supposed to use for
|
||||
@ -76,9 +82,19 @@ class Connector
|
||||
this.sock = socket;
|
||||
this.messageQueue = messageQueue;
|
||||
this.errorHandler = errorHandler;
|
||||
this.listenAddress = new TransportAddress(socket.getLocalAddress(),
|
||||
socket.getLocalPort(), socket.getUDPSocket() != null ?
|
||||
Transport.UDP : Transport.TCP);
|
||||
|
||||
Transport transport
|
||||
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
|
||||
this.listenAddress
|
||||
= new TransportAddress(socket.getLocalAddress(),
|
||||
socket.getLocalPort(),
|
||||
transport);
|
||||
this.remoteAddress
|
||||
= transport == Transport.UDP
|
||||
? null
|
||||
: new TransportAddress(socket.getTCPSocket().getInetAddress(),
|
||||
socket.getTCPSocket().getPort(),
|
||||
transport);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,7 +214,8 @@ class Connector
|
||||
{
|
||||
logger.log(
|
||||
Level.WARNING,
|
||||
"Connector died: " + listenAddress,
|
||||
"Connector died: " + listenAddress + " -> "
|
||||
+ remoteAddress,
|
||||
ex);
|
||||
|
||||
stop();
|
||||
@ -292,4 +309,16 @@ class Connector
|
||||
{
|
||||
return listenAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remote <tt>TransportAddress</tt> in case of TCP, or
|
||||
* <tt>null</tt> in case of UDP.
|
||||
*
|
||||
* @return the remote <tt>TransportAddress</tt> in case of TCP, or
|
||||
* <tt>null</tt> in case of UDP.
|
||||
*/
|
||||
TransportAddress getRemoteAddress()
|
||||
{
|
||||
return remoteAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package org.ice4j.stack;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.util.logging.*;
|
||||
|
||||
@ -23,6 +24,7 @@ import org.ice4j.socket.*;
|
||||
*
|
||||
* @author Emil Ivov
|
||||
* @author Aakash Garg
|
||||
* @author Boris Grozev
|
||||
*/
|
||||
class NetAccessManager
|
||||
implements ErrorHandler
|
||||
@ -34,7 +36,14 @@ class NetAccessManager
|
||||
= Logger.getLogger(NetAccessManager.class.getName());
|
||||
|
||||
/**
|
||||
* All access points currently in use with UDP. The table maps
|
||||
* 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
|
||||
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s.
|
||||
*
|
||||
* Due to the final hashCode() method of InetSocketAddress, TransportAddress
|
||||
@ -46,16 +55,18 @@ class NetAccessManager
|
||||
= new Hashtable<TransportAddress, Connector>();
|
||||
|
||||
/**
|
||||
* All access points currently in use with TCP. The table maps
|
||||
* <tt>TransportAddress</tt>es to <tt>Connector</tt>s.
|
||||
* All access points currently in use with TCP. The table maps local
|
||||
* <tt>TransportAddress</tt>es to a list of <tt>Connector</tt>s with that
|
||||
* local address. To find the required <tt>Connector</tt> in the list, the
|
||||
* remote address needs to be used.
|
||||
*
|
||||
* 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<TransportAddress, Connector> netTCPAccessPoints
|
||||
= new Hashtable<TransportAddress, Connector>();
|
||||
private Map<TransportAddress, List<Connector>> netTCPAccessPoints
|
||||
= new Hashtable<TransportAddress, List<Connector>>();
|
||||
|
||||
/**
|
||||
* A synchronized FIFO where incoming messages are stocked for processing.
|
||||
@ -235,7 +246,8 @@ class NetAccessManager
|
||||
Connector ap = (Connector)callingThread;
|
||||
|
||||
//make sure nothing's left and notify user
|
||||
removeSocket(ap.getListenAddress());
|
||||
removeSocket(ap.getListenAddress(),
|
||||
ap.getRemoteAddress());
|
||||
logger.log(Level.WARNING, "Removing connector:" + ap, error);
|
||||
}
|
||||
else if( callingThread instanceof MessageProcessor )
|
||||
@ -265,56 +277,119 @@ class NetAccessManager
|
||||
protected void addSocket(IceSocketWrapper socket)
|
||||
{
|
||||
//no null check - let it through as a NullPointerException
|
||||
Transport transport
|
||||
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
|
||||
TransportAddress localAddr
|
||||
= new TransportAddress(
|
||||
socket.getLocalAddress(),
|
||||
socket.getLocalPort(),
|
||||
socket.getUDPSocket() != null ? Transport.UDP :
|
||||
Transport.TCP);
|
||||
transport);
|
||||
|
||||
if (socket.getUDPSocket() != null &&
|
||||
!netUDPAccessPoints.containsKey(localAddr))
|
||||
synchronized (connectorsSyncRoot)
|
||||
{
|
||||
Connector ap = new Connector(socket, messageQueue, this);
|
||||
switch (transport)
|
||||
{
|
||||
case UDP:
|
||||
if (!netUDPAccessPoints.containsKey(localAddr))
|
||||
{
|
||||
Connector connector
|
||||
= new Connector(socket, messageQueue, this);
|
||||
|
||||
netUDPAccessPoints.put(localAddr, ap);
|
||||
ap.start();
|
||||
netUDPAccessPoints.put(localAddr, connector);
|
||||
connector.start();
|
||||
}
|
||||
break;
|
||||
case TCP:
|
||||
List<Connector> connectors = netTCPAccessPoints.get(localAddr);
|
||||
if (connectors == null)
|
||||
{
|
||||
connectors = new LinkedList<Connector>();
|
||||
netTCPAccessPoints.put(localAddr, connectors);
|
||||
}
|
||||
|
||||
Connector connector
|
||||
= findTCPConnectorByRemoteAddress(
|
||||
connectors,
|
||||
new TransportAddress(
|
||||
socket.getTCPSocket().getInetAddress(),
|
||||
socket.getTCPSocket().getPort(),
|
||||
Transport.TCP));
|
||||
if (connector == null)
|
||||
{
|
||||
connector = new Connector(socket, messageQueue, this);
|
||||
|
||||
connectors.add(connector);
|
||||
connector.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* From a list of <tt>Connector</tt>s with TCP sockets finds the one with
|
||||
* remote address equal to <tt>dst</tt>.
|
||||
* @param list the list of <tt>Connector</tt>s to search.
|
||||
* @param dst the remote address of the connector to search for.
|
||||
* @return the first <tt>Connector</tt> in <tt>list</tt> with a remote
|
||||
* address equal to <tt>dst</tt>.
|
||||
*/
|
||||
private Connector findTCPConnectorByRemoteAddress(
|
||||
List<Connector> list,
|
||||
TransportAddress dst)
|
||||
{
|
||||
if (dst == null)
|
||||
return null;
|
||||
|
||||
for (Connector connector : list)
|
||||
{
|
||||
if (dst.equals(connector.getRemoteAddress()))
|
||||
{
|
||||
return connector;
|
||||
}
|
||||
}
|
||||
|
||||
if (socket.getTCPSocket() != null &&
|
||||
!netTCPAccessPoints.containsKey(localAddr))
|
||||
{
|
||||
Connector ap = new Connector(socket, messageQueue, this);
|
||||
|
||||
netTCPAccessPoints.put(localAddr, ap);
|
||||
ap.start();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and deletes the specified access point.
|
||||
*
|
||||
* @param address the address of the connector to remove.
|
||||
* @param src the local address of the connector to remove.
|
||||
* @param dst the remote address of the connector to remote. For TCP only,
|
||||
* use <tt>null</tt> for UDP.
|
||||
*/
|
||||
protected void removeSocket(TransportAddress address)
|
||||
protected void removeSocket(TransportAddress src,
|
||||
TransportAddress dst)
|
||||
{
|
||||
Connector ap;
|
||||
Connector connector = null;
|
||||
|
||||
switch (address.getTransport())
|
||||
synchronized (connectorsSyncRoot)
|
||||
{
|
||||
case TCP:
|
||||
ap = netTCPAccessPoints.remove(address);
|
||||
break;
|
||||
case UDP:
|
||||
ap = netUDPAccessPoints.remove(address);
|
||||
break;
|
||||
default:
|
||||
ap = null;
|
||||
break;
|
||||
switch (src.getTransport())
|
||||
{
|
||||
case UDP:
|
||||
connector = netUDPAccessPoints.remove(src);
|
||||
break;
|
||||
case TCP:
|
||||
List<Connector> connectors = netTCPAccessPoints.remove(src);
|
||||
if (connectors != null)
|
||||
{
|
||||
connector
|
||||
= findTCPConnectorByRemoteAddress(connectors, dst);
|
||||
|
||||
if (connector != null)
|
||||
{
|
||||
connectors.remove(connector);
|
||||
if (connectors.isEmpty())
|
||||
netTCPAccessPoints.remove(src);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(ap != null)
|
||||
ap.stop();
|
||||
if(connector != null)
|
||||
connector.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -404,6 +479,37 @@ class NetAccessManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <tt>Connector</tt> responsible for a particular source
|
||||
* address and a particular destination address.
|
||||
*
|
||||
* @param src the source address.
|
||||
* @param dst the destination address.
|
||||
* Returns the <tt>Connector</tt> responsible for a particular source
|
||||
* address and a particular destination address, or <tt>null</tt> if there's
|
||||
* none.
|
||||
*/
|
||||
private Connector getConnector(TransportAddress src,
|
||||
TransportAddress dst)
|
||||
{
|
||||
synchronized (connectorsSyncRoot)
|
||||
{
|
||||
switch (src.getTransport())
|
||||
{
|
||||
case UDP:
|
||||
return netUDPAccessPoints.get(src);
|
||||
case TCP:
|
||||
List<Connector> connectors = netTCPAccessPoints.get(src);
|
||||
if (connectors != null)
|
||||
{
|
||||
return findTCPConnectorByRemoteAddress(connectors, dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//--------------- SENDING MESSAGES -----------------------------------------
|
||||
/**
|
||||
* Sends the specified stun message through the specified access point.
|
||||
@ -426,16 +532,11 @@ class NetAccessManager
|
||||
{
|
||||
byte[] bytes = stunMessage.encode(stunStack);
|
||||
|
||||
Connector ap = null;
|
||||
|
||||
if (srcAddr.getTransport() == Transport.UDP)
|
||||
ap = netUDPAccessPoints.get(srcAddr);
|
||||
else if (srcAddr.getTransport() == Transport.TCP)
|
||||
ap = netTCPAccessPoints.get(srcAddr);
|
||||
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||
if (ap == null)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"No socket has been added for source address: " + srcAddr);
|
||||
"No socket has been added for: " + srcAddr + " <-> " + remoteAddr);
|
||||
}
|
||||
|
||||
ap.sendMessage(bytes, remoteAddr);
|
||||
@ -463,12 +564,7 @@ class NetAccessManager
|
||||
{
|
||||
byte[] bytes = channelData.encode();
|
||||
|
||||
Connector ap = null;
|
||||
|
||||
if (srcAddr.getTransport() == Transport.UDP)
|
||||
ap = netUDPAccessPoints.get(srcAddr);
|
||||
else if (srcAddr.getTransport() == Transport.TCP)
|
||||
ap = netTCPAccessPoints.get(srcAddr);
|
||||
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||
if (ap == null)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
@ -498,11 +594,7 @@ class NetAccessManager
|
||||
throws IllegalArgumentException,
|
||||
IOException, StunException
|
||||
{
|
||||
Connector ap = null;
|
||||
if (srcAddr.getTransport() == Transport.UDP)
|
||||
ap = netUDPAccessPoints.get(srcAddr);
|
||||
else if (srcAddr.getTransport() == Transport.TCP)
|
||||
ap = netTCPAccessPoints.get(srcAddr);
|
||||
Connector ap = getConnector(srcAddr, remoteAddr);
|
||||
if (ap == null)
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
|
||||
@ -122,15 +122,32 @@ public class StunStack
|
||||
|
||||
/**
|
||||
* Stops and deletes the connector listening on the specified local address.
|
||||
* Note this removes connectors with UDP sockets only, use
|
||||
* {@link #removeSocket(org.ice4j.TransportAddress, org.ice4j.TransportAddress)}
|
||||
* with the appropriate remote address for TCP.
|
||||
*
|
||||
* @param localAddr the access point to remove
|
||||
* @param localAddr the local address of the socket to remove.
|
||||
*/
|
||||
public void removeSocket(TransportAddress localAddr)
|
||||
{
|
||||
//first cancel all transactions using this address.
|
||||
cancelTransactionsForAddress(localAddr);
|
||||
removeSocket(localAddr, null);
|
||||
}
|
||||
|
||||
netAccessManager.removeSocket(localAddr);
|
||||
/**
|
||||
* Stops and deletes the connector listening on the specified local address
|
||||
* and remote address.
|
||||
*
|
||||
* @param localAddr the local address of the socket to remove.
|
||||
* @param remoteAddr the remote address of the socket to remove. Use
|
||||
* <tt>null</tt> for UDP.
|
||||
*/
|
||||
public void removeSocket(TransportAddress localAddr,
|
||||
TransportAddress remoteAddr)
|
||||
{
|
||||
//first cancel all transactions using this address.
|
||||
cancelTransactionsForAddress(localAddr, remoteAddr);
|
||||
|
||||
netAccessManager.removeSocket(localAddr, remoteAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,8 +256,13 @@ public class StunStack
|
||||
*
|
||||
* @param localAddr the <tt>TransportAddress</tt> that we'd like to remove
|
||||
* transactions for.
|
||||
* @param remoteAddr the remote <tt>TransportAddress</tt> that we'd like to
|
||||
* remove transactions for. If <tt>null</tt>, then it will not be taken
|
||||
* into account (that is, all transactions with for <tt>localAddr</tt> will
|
||||
* be cancelled).
|
||||
*/
|
||||
private void cancelTransactionsForAddress(TransportAddress localAddr)
|
||||
private void cancelTransactionsForAddress(TransportAddress localAddr,
|
||||
TransportAddress remoteAddr)
|
||||
{
|
||||
List<StunClientTransaction> clientTransactionsToCancel = null;
|
||||
|
||||
@ -253,7 +275,9 @@ public class StunStack
|
||||
{
|
||||
StunClientTransaction tran = clientTransactionsIter.next();
|
||||
|
||||
if (tran.getLocalAddress().equals(localAddr))
|
||||
if (tran.getLocalAddress().equals(localAddr)
|
||||
&& (remoteAddr == null
|
||||
|| remoteAddr.equals(tran.getRemoteAddress())))
|
||||
{
|
||||
clientTransactionsIter.remove();
|
||||
|
||||
@ -299,14 +323,18 @@ public class StunStack
|
||||
|| (sendingAddr != null
|
||||
&& sendingAddr.equals(localAddr)))
|
||||
{
|
||||
serverTransactionsIter.remove();
|
||||
|
||||
if (serverTransactionsToExpire == null)
|
||||
if (remoteAddr == null
|
||||
|| remoteAddr.equals(tran.getRequestSourceAddress()))
|
||||
{
|
||||
serverTransactionsToExpire
|
||||
= new LinkedList<StunServerTransaction>();
|
||||
serverTransactionsIter.remove();
|
||||
|
||||
if (serverTransactionsToExpire == null)
|
||||
{
|
||||
serverTransactionsToExpire
|
||||
= new LinkedList<StunServerTransaction>();
|
||||
}
|
||||
serverTransactionsToExpire.add(tran);
|
||||
}
|
||||
serverTransactionsToExpire.add(tran);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user