Refactors NetAccessManager. Allows UDP sockets to be associated with a

specific remote address.
This commit is contained in:
boris 2015-02-27 16:42:25 +00:00
parent f322f67f93
commit 5ebfe11c55
4 changed files with 156 additions and 142 deletions

View File

@ -1509,7 +1509,7 @@ public class Agent
// //
if(triggerPair.getParentComponent().getSelectedPair() == null) if(triggerPair.getParentComponent().getSelectedPair() == null)
logger.info("Add peer CandidatePair with new reflexive " + logger.info("Add peer CandidatePair with new reflexive " +
"address to checkList"); "address to checkList: " + triggerPair);
triggerPair.setUseCandidateReceived(); triggerPair.setUseCandidateReceived();
parentStream.addToCheckList(triggerPair); parentStream.addToCheckList(triggerPair);
} }

View File

@ -77,12 +77,14 @@ class Connector
* @param errorHandler the instance to notify when errors occur. * @param errorHandler the instance to notify when errors occur.
*/ */
protected Connector(IceSocketWrapper socket, protected Connector(IceSocketWrapper socket,
TransportAddress remoteAddress,
MessageQueue messageQueue, MessageQueue messageQueue,
ErrorHandler errorHandler) ErrorHandler errorHandler)
{ {
this.sock = socket; this.sock = socket;
this.messageQueue = messageQueue; this.messageQueue = messageQueue;
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
this.remoteAddress = remoteAddress;
Transport transport Transport transport
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP; = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
@ -91,19 +93,6 @@ class Connector
= new TransportAddress(socket.getLocalAddress(), = new TransportAddress(socket.getLocalAddress(),
socket.getLocalPort(), socket.getLocalPort(),
transport); 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 <tt>TransportAddress</tt> in case of TCP, or * Returns the remote <tt>TransportAddress</tt> or <tt>null</tt> if none
* <tt>null</tt> in case of UDP. * is specified.
* *
* @return the remote <tt>TransportAddress</tt> in case of TCP, or * @return the remote <tt>TransportAddress</tt> or <tt>null</tt> if none
* <tt>null</tt> in case of UDP. * is specified.
*/ */
TransportAddress getRemoteAddress() TransportAddress getRemoteAddress()
{ {

View File

@ -36,37 +36,34 @@ class NetAccessManager
= Logger.getLogger(NetAccessManager.class.getName()); = Logger.getLogger(NetAccessManager.class.getName());
/** /**
* Synchronization root for access to {@link #netUDPAccessPoints} and * All <tt>Connectors</tt> currently in use with UDP. The table maps a local
* {@link #netTCPAccessPoints} and the lists contained in * <tt>TransportAddress</tt> and and a remote <tt>TransportAddress</tt> to
* {@link #netTCPAccessPoints}. * a <tt>Connector</tt>. We allow a <tt>Connector</tt> to be added without
*/ * a specified remote address, under the <tt>null</tt> key.
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 * Due to the final hashCode() method of InetSocketAddress, TransportAddress
* cannot override and it causes problem to store both UDP and TCP address * 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 * (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). * because InetSocketAddress does not take into account transport).
*/ */
private Map<TransportAddress, Connector> netUDPAccessPoints private final Map<TransportAddress, Map<TransportAddress, Connector>>
= new Hashtable<TransportAddress, Connector>(); udpConnectors
= new HashMap<TransportAddress, Map<TransportAddress, Connector>>();
/** /**
* All access points currently in use with TCP. The table maps local * All <tt>Connectors</tt> currently in use with TCP. The table maps a local
* <tt>TransportAddress</tt>es to a list of <tt>Connector</tt>s with that * <tt>TransportAddress</tt> and and a remote <tt>TransportAddress</tt> to
* local address. To find the required <tt>Connector</tt> in the list, the * a <tt>Connector</tt>. We allow a <tt>Connector</tt> to be added without
* remote address needs to be used. * a specified remote address, under the <tt>null</tt> key.
* *
* Due to the final hashCode() method of InetSocketAddress, TransportAddress * Due to the final hashCode() method of InetSocketAddress, TransportAddress
* cannot override and it causes problem to store both UDP and TCP address * 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 * (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). * because InetSocketAddress does not take into account transport).
*/ */
private Map<TransportAddress, List<Connector>> netTCPAccessPoints private final Map<TransportAddress, Map<TransportAddress, Connector>>
= new Hashtable<TransportAddress, List<Connector>>(); tcpConnectors
= new HashMap<TransportAddress, Map<TransportAddress, Connector>>();
/** /**
* A synchronized FIFO where incoming messages are stocked for processing. * A synchronized FIFO where incoming messages are stocked for processing.
@ -243,12 +240,12 @@ class NetAccessManager
{ {
if (callingThread instanceof Connector) if (callingThread instanceof Connector)
{ {
Connector ap = (Connector)callingThread; Connector connector = (Connector)callingThread;
//make sure nothing's left and notify user //make sure nothing's left and notify user
removeSocket(ap.getListenAddress(), removeSocket(connector.getListenAddress(),
ap.getRemoteAddress()); connector.getRemoteAddress());
logger.log(Level.WARNING, "Removing connector:" + ap, error); logger.log(Level.WARNING, "Removing connector:" + connector, error);
} }
else if( callingThread instanceof MessageProcessor ) else if( callingThread instanceof MessageProcessor )
{ {
@ -277,119 +274,110 @@ class NetAccessManager
protected void addSocket(IceSocketWrapper socket) protected void addSocket(IceSocketWrapper socket)
{ {
//no null check - let it through as a NullPointerException //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 Transport transport
= socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP; = socket.getUDPSocket() != null ? Transport.UDP : Transport.TCP;
TransportAddress localAddr TransportAddress localAddress
= new TransportAddress( = new TransportAddress(
socket.getLocalAddress(), socket.getLocalAddress(),
socket.getLocalPort(), socket.getLocalPort(),
transport); transport);
synchronized (connectorsSyncRoot) final Map<TransportAddress, Map<TransportAddress, Connector>>
connectorsMap
= (transport == Transport.UDP)
? udpConnectors
: tcpConnectors;
synchronized (connectorsMap)
{ {
switch (transport) Map<TransportAddress, Connector> connectorsForLocalAddress
= connectorsMap.get(localAddress);
if (connectorsForLocalAddress == null)
{ {
case UDP: connectorsForLocalAddress
if (!netUDPAccessPoints.containsKey(localAddr)) = new HashMap<TransportAddress, Connector>();
connectorsMap.put(localAddress, connectorsForLocalAddress);
}
if (!connectorsForLocalAddress.containsKey(remoteAddress))
{ {
Connector connector Connector connector
= new Connector(socket, messageQueue, this); = new Connector(socket, remoteAddress, messageQueue, this);
netUDPAccessPoints.put(localAddr, connector); connectorsForLocalAddress.put(remoteAddress, connector);
connector.start(); connector.start();
} }
break; else
case TCP:
List<Connector> connectors = netTCPAccessPoints.get(localAddr);
if (connectors == null)
{ {
connectors = new LinkedList<Connector>(); logger.info("Not creating a new Connector, because we already "
netTCPAccessPoints.put(localAddr, connectors); + "have one for the given address pair: "
} + localAddress + " -> " + remoteAddress);
Socket tcpSocket = socket.getTCPSocket();
Connector connector
= findTCPConnectorByRemoteAddress(
connectors,
new TransportAddress(
tcpSocket.getInetAddress(),
tcpSocket.getPort(),
Transport.TCP));
if (connector == null)
{
connector = new Connector(socket, messageQueue, this);
connectors.add(connector);
connector.start();
}
break;
} }
} }
} }
/**
* 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;
}
}
return null;
}
/** /**
* Stops and deletes the specified access point. * Stops and deletes the specified access point.
* *
* @param src the local address of the connector to remove. * @param localAddress the local address of the connector to remove.
* @param dst the remote address of the connector to remote. For TCP only, * @param remoteAddress the remote address of the connector to remote. Use
* use <tt>null</tt> for UDP. * <tt>null</tt> to match the <tt>Connector</tt> with no specified remote
* address.
*/ */
protected void removeSocket(TransportAddress src, TransportAddress dst) protected void removeSocket(TransportAddress localAddress,
TransportAddress remoteAddress)
{ {
Connector connector = null; Connector connector = null;
synchronized (connectorsSyncRoot) final Map<TransportAddress, Map<TransportAddress, Connector>>
{ connectorsMap
switch (src.getTransport()) = (localAddress.getTransport() == Transport.UDP)
{ ? udpConnectors
case UDP: : tcpConnectors;
connector = netUDPAccessPoints.remove(src);
break;
case TCP: synchronized (connectorsMap)
List<Connector> connectors = netTCPAccessPoints.remove(src);
if (connectors != null)
{ {
connector Map<TransportAddress, Connector> connectorsForLocalAddress
= findTCPConnectorByRemoteAddress(connectors, dst); = connectorsMap.get(localAddress);
if (connectorsForLocalAddress != null)
{
connector = connectorsForLocalAddress.get(remoteAddress);
if (connector != null) if (connector != null)
{ {
connectors.remove(connector); connectorsForLocalAddress.remove(remoteAddress);
if (connectors.isEmpty()) if (connectorsForLocalAddress.isEmpty())
netTCPAccessPoints.remove(src); connectorsMap.remove(localAddress);
} }
} }
break;
}
} }
if(connector != null) if(connector != null)
@ -398,43 +386,73 @@ class NetAccessManager
/** /**
* Stops <tt>NetAccessManager</tt> and all of its <tt>MessageProcessor</tt>. * Stops <tt>NetAccessManager</tt> and all of its <tt>MessageProcessor</tt>.
* TODO: close all connectors as well?
*/ */
@SuppressWarnings("unchecked")
public void stop() public void stop()
{ {
for(MessageProcessor mp : messageProcessors) for(MessageProcessor mp : messageProcessors)
{ {
mp.stop(); mp.stop();
} }
for (Object o : new Object[]{udpConnectors, tcpConnectors})
{
Map<TransportAddress, Map<TransportAddress, Connector>>
connectorsMap
= (Map<TransportAddress, Map<TransportAddress, Connector>>)o;
synchronized (connectorsMap)
{
for (Map<TransportAddress, Connector> connectorsForLocalAddress
: connectorsMap.values())
{
for (Connector connector : connectorsForLocalAddress.values())
{
connector.stop();
}
}
}
}
} }
/** /**
* Returns the <tt>Connector</tt> responsible for a particular source * Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address. * address and a particular destination address.
* *
* @param src the source address. * @param localAddress the source address.
* @param dst the destination address. * @param remoteAddress the destination address.
* Returns the <tt>Connector</tt> responsible for a particular source * Returns the <tt>Connector</tt> responsible for a particular source
* address and a particular destination address, or <tt>null</tt> if there's * address and a particular destination address, or <tt>null</tt> if there's
* none. * none.
*/ */
private Connector getConnector(TransportAddress src, TransportAddress dst) private Connector getConnector(TransportAddress localAddress,
TransportAddress remoteAddress)
{ {
synchronized (connectorsSyncRoot) boolean udp = localAddress.getTransport() == Transport.UDP;
{ final Map<TransportAddress, Map<TransportAddress, Connector>>
switch (src.getTransport()) connectorsMap
{ = udp
case UDP: ? udpConnectors
return netUDPAccessPoints.get(src); : tcpConnectors;
case TCP: Connector connector = null;
List<Connector> connectors = netTCPAccessPoints.get(src);
if (connectors != null) synchronized (connectorsMap)
return findTCPConnectorByRemoteAddress(connectors, dst); {
Map<TransportAddress, Connector> 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 -------------------------------- //---------------thread pool implementation --------------------------------

View File

@ -110,8 +110,8 @@ public class StunStack
} }
/** /**
* Creates and starts the specified Network Access Point based on the * Creates and starts a Network Access Point (Connector) based on the
* specified socket and returns a relevant descriptor. * specified socket.
* *
* @param sock The socket that the new access point should represent. * @param sock The socket that the new access point should represent.
*/ */
@ -120,6 +120,17 @@ public class StunStack
netAccessManager.addSocket(sock); 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. * Stops and deletes the connector listening on the specified local address.
* Note this removes connectors with UDP sockets only, use * Note this removes connectors with UDP sockets only, use
@ -473,10 +484,6 @@ public class StunStack
getNetAccessManager().sendMessage( getNetAccessManager().sendMessage(
udpMessage.getBytes(), sendThrough, sendTo); udpMessage.getBytes(), sendThrough, sendTo);
} }
catch (StunException stex)
{
throw stex;
}
catch (IllegalArgumentException iaex) catch (IllegalArgumentException iaex)
{ {
throw new StunException( throw new StunException(