feat: Splits the API to obtain sockets for a candidate/candidate pair.

The public-facing API returns the socket instance for the associated
Component, while the internal API (getCandidateIceSocket) returns
the internal socket instance for the candidate.
This commit is contained in:
Boris Grozev 2016-11-28 16:11:24 -06:00
parent 91858ba6a3
commit 15a2df1e59
12 changed files with 85 additions and 125 deletions

View File

@ -722,7 +722,9 @@ public class CandidatePair
* <tt>CandidatePair</tt>. * <tt>CandidatePair</tt>.
* @return the UDP <tt>DatagramSocket</tt> (if any) for this * @return the UDP <tt>DatagramSocket</tt> (if any) for this
* <tt>CandidatePair</tt>. * <tt>CandidatePair</tt>.
* @deprecated use {@link Component#getSocket()} instead.
*/ */
@Deprecated
public DatagramSocket getDatagramSocket() public DatagramSocket getDatagramSocket()
{ {
IceSocketWrapper wrapper = getIceSocketWrapper(); IceSocketWrapper wrapper = getIceSocketWrapper();
@ -732,38 +734,22 @@ public class CandidatePair
/** /**
* Returns the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>. * Returns the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>.
* @return the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>. * @return the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>.
* @deprecated use {@link Component#getSocket()} instead.
*/ */
@Deprecated
public Socket getSocket() public Socket getSocket()
{ {
IceSocketWrapper wrapper = getIceSocketWrapper(); return null;
return wrapper == null ? null : wrapper.getTCPSocket();
} }
/** /**
* Returns the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>. * Returns the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>.
* @return the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>. * @return the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>.
* @deprecated use {@link Component#getSocket()} instead.
*/ */
@Deprecated
public IceSocketWrapper getIceSocketWrapper() public IceSocketWrapper getIceSocketWrapper()
{ {
LocalCandidate localCandidate = getLocalCandidate(); return getParentComponent().getSocketWrapper();
if (localCandidate == null)
{
return null;
}
LocalCandidate base = localCandidate.getBase();
if (base != null)
localCandidate = base;
RemoteCandidate remoteCandidate = getRemoteCandidate();
if (remoteCandidate != null)
{
SocketAddress remoteAddress
= remoteCandidate.getTransportAddress();
if (remoteAddress != null)
return localCandidate.getIceSocketWrapper(remoteAddress);
}
return localCandidate.getIceSocketWrapper();
} }
} }

View File

@ -191,9 +191,10 @@ public class GoogleRelayedCandidate
* *
* @return the <tt>DatagramSocket</tt> associated with this * @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt> * <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper() * @see LocalCandidate#getCandidateIceSocketWrapper()
*/ */
public synchronized IceSocketWrapper getIceSocketWrapper() @Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{ {
if (socket == null) if (socket == null)
{ {

View File

@ -131,13 +131,12 @@ public class HostCandidate extends LocalCandidate
} }
/** /**
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>. * {@inheritDoc}
* * SHOULD NOT be used outside ice4j. Only exposed for use in the
* @return the <tt>DatagramSocket</tt> associated with this * org.ice4j.socket package.
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
*/ */
public IceSocketWrapper getIceSocketWrapper() @Override
public IceSocketWrapper getCandidateIceSocketWrapper()
{ {
return socket; return socket;
} }

View File

@ -108,8 +108,7 @@ public abstract class LocalCandidate
* <tt>Candidate</tt> * <tt>Candidate</tt>
* *
* @deprecated This should be used by the library only. Users of ice4j * @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getDatagramSocket()} * should use {@link org.ice4j.ice.Component#getSocket()} instead.
* on the appropriate <tt>CandidatePair</tt> instead.
*/ */
@Deprecated @Deprecated
public DatagramSocket getDatagramSocket() public DatagramSocket getDatagramSocket()
@ -126,39 +125,34 @@ public abstract class LocalCandidate
* <tt>Candidate</tt> * <tt>Candidate</tt>
* *
* @deprecated This should be used by the library only. Users of ice4j * @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getSocket()} on the * should use {@link org.ice4j.ice.Component#getSocket()} instead.
* appropriate <tt>CandidatePair</tt> instead.
*/ */
@Deprecated @Deprecated
public Socket getSocket() public Socket getSocket()
{ {
IceSocketWrapper wrapper = getIceSocketWrapper(); return null;
return wrapper == null ? null : wrapper.getTCPSocket();
} }
/** /**
* Gets the <tt>IceSocketWrapper</tt> associated with this * @return the {@link IceSocketWrapper} instance of the {@link Component}
* <tt>Candidate</tt>. * which owns this {@link LocalCandidate}. Note that this IS NOT an
* * instance specific to this {@link LocalCandidate}. See
* @return the <tt>IceSocketWrapper</tt> associated with this * {@link #getCandidateIceSocketWrapper()}.
* <tt>Candidate</tt>
*/ */
protected abstract IceSocketWrapper getIceSocketWrapper(); protected IceSocketWrapper getIceSocketWrapper()
/**
* Gets the <tt>IceSocketWrapper</tt> associated with this
* <tt>Candidate</tt> which is to be used for a specific remote address.
* This default implementation ignores the remote address.
*
* @param remoteAddress the remote address for which to get a socket.
* @return the <tt>IceSocketWrapper</tt> associated with this
* <tt>Candidate</tt> which is to be used for a specific remote address.
*/
protected IceSocketWrapper getIceSocketWrapper(SocketAddress remoteAddress)
{ {
return getIceSocketWrapper(); return getParentComponent().getSocketWrapper();
} }
/**
* @return the {@link IceSocketWrapper} instance, if any, associated with
* this candidate. Note that this IS NOT the instance which should be used
* for reading and writing by the application, and SHOULD NOT be used from
* outside ice4j (even if a subclass exposes it as public). Also see
* {@link #getIceSocketWrapper()}.
*/
protected abstract IceSocketWrapper getCandidateIceSocketWrapper();
/** /**
* Creates if necessary and returns a <tt>DatagramSocket</tt> that would * Creates if necessary and returns a <tt>DatagramSocket</tt> that would
* capture all STUN packets arriving on this candidate's socket. If the * capture all STUN packets arriving on this candidate's socket. If the
@ -174,7 +168,7 @@ public abstract class LocalCandidate
*/ */
public IceSocketWrapper getStunSocket(TransportAddress serverAddress) public IceSocketWrapper getStunSocket(TransportAddress serverAddress)
{ {
IceSocketWrapper hostSocket = getIceSocketWrapper(); IceSocketWrapper hostSocket = getCandidateIceSocketWrapper();
if (hostSocket != null if (hostSocket != null
&& hostSocket.getTCPSocket() != null) && hostSocket.getTCPSocket() != null)
@ -323,7 +317,7 @@ public abstract class LocalCandidate
protected void free() protected void free()
{ {
// Close the socket associated with this LocalCandidate. // Close the socket associated with this LocalCandidate.
IceSocketWrapper socket = getIceSocketWrapper(); IceSocketWrapper socket = getCandidateIceSocketWrapper();
if (socket != null) if (socket != null)
{ {
@ -331,7 +325,7 @@ public abstract class LocalCandidate
if ((base == null) if ((base == null)
|| (base == this) || (base == this)
|| (base.getIceSocketWrapper() != socket)) || (base.getCandidateIceSocketWrapper() != socket))
{ {
//remove our socket from the stack. //remove our socket from the stack.
getStunStack().removeSocket(getTransportAddress()); getStunStack().removeSocket(getTransportAddress());

View File

@ -64,15 +64,11 @@ public class PeerReflexiveCandidate
} }
/** /**
* Returns the <tt>DatagramSocket</tt> associated with this * {@inheritDoc}
* <tt>Candidate</tt>.
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
*/ */
public IceSocketWrapper getIceSocketWrapper() @Override
public IceSocketWrapper getCandidateIceSocketWrapper()
{ {
return getBase().getIceSocketWrapper(); return getBase().getCandidateIceSocketWrapper();
} }
} }

View File

@ -101,7 +101,7 @@ public class RelayedCandidate
* @return the <tt>RelayedCandidateDatagramSocket</tt> of this * @return the <tt>RelayedCandidateDatagramSocket</tt> of this
* <tt>RelayedCandidate</tt> * <tt>RelayedCandidate</tt>
*/ */
public synchronized RelayedCandidateDatagramSocket private synchronized RelayedCandidateDatagramSocket
getRelayedCandidateDatagramSocket() getRelayedCandidateDatagramSocket()
{ {
if (relayedCandidateDatagramSocket == null) if (relayedCandidateDatagramSocket == null)
@ -127,9 +127,9 @@ public class RelayedCandidate
* *
* @return the <tt>DatagramSocket</tt> associated with this * @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt> * <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
*/ */
public synchronized IceSocketWrapper getIceSocketWrapper() @Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{ {
if (socket == null) if (socket == null)
{ {

View File

@ -104,16 +104,12 @@ public class ServerReflexiveCandidate
} }
/** /**
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>. * {@inheritDoc}
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
*/ */
@Override @Override
public IceSocketWrapper getIceSocketWrapper() public IceSocketWrapper getCandidateIceSocketWrapper()
{ {
return getBase().getIceSocketWrapper(); return getBase().getCandidateIceSocketWrapper();
} }
/** /**

View File

@ -83,16 +83,4 @@ public class TcpHostCandidate
super.free(); super.free();
} }
@Override
protected IceSocketWrapper getIceSocketWrapper(SocketAddress remoteAddress)
{
for (IceSocketWrapper socket : sockets)
{
if (socket.getTCPSocket().getRemoteSocketAddress()
.equals(remoteAddress))
return socket;
}
return null;
}
} }

View File

@ -79,7 +79,7 @@ public class UPNPCandidate
{ {
} }
IceSocketWrapper socket = getIceSocketWrapper(); IceSocketWrapper socket = getCandidateIceSocketWrapper();
if(socket != null) if(socket != null)
{ {
socket.close(); socket.close();
@ -89,13 +89,19 @@ public class UPNPCandidate
} }
/** /**
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>. * {@inheritDoc}
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
*/ */
@Override @Override
public IceSocketWrapper getIceSocketWrapper() protected IceSocketWrapper getCandidateIceSocketWrapper()
{
return getBase().getCandidateIceSocketWrapper();
}
/**
* {@inheritDoc}
*/
@Override
protected IceSocketWrapper getIceSocketWrapper()
{ {
return getBase().getIceSocketWrapper(); return getBase().getIceSocketWrapper();
} }

View File

@ -230,26 +230,27 @@ public class SinglePortUdpHarvester
* The collection of <tt>IceSocketWrapper</tt>s that can potentially * The collection of <tt>IceSocketWrapper</tt>s that can potentially
* be used by the ice4j user to read/write from/to this candidate. * be used by the ice4j user to read/write from/to this candidate.
* The keys are the remote addresses for each socket. * The keys are the remote addresses for each socket.
* * <p>
* There are wrappers over <tt>MultiplexedDatagramSocket</tt>s over * There are wrappers over <tt>MultiplexedDatagramSocket</tt>s over
* a corresponding socket in {@link #sockets}. * a corresponding socket in {@link #sockets}.
*/ */
private final Map<SocketAddress, IceSocketWrapper> candidateSockets private final Map<SocketAddress, IceSocketWrapper> candidateSockets
= new HashMap<>(); = new HashMap<>();
/** /**
* The collection of <tt>DatagramSocket</tt>s added to this candidate. * The collection of <tt>DatagramSocket</tt>s added to this candidate.
* The keys are the remote addresses for each socket. * The keys are the remote addresses for each socket.
* * <p>
* These are the "raw" sockets, before any wrappers are added for * These are the "raw" sockets, before any wrappers are added for
* the STUN stack or the user of ice4j. * the STUN stack or the user of ice4j.
*/ */
private final Map<SocketAddress, DatagramSocket> sockets private final Map<SocketAddress, DatagramSocket> sockets
= new HashMap<>(); = new HashMap<>();
/** /**
* Initializes a new <tt>MyCandidate</tt> instance with the given * Initializes a new <tt>MyCandidate</tt> instance with the given
* <tt>Component</tt> and the given local username fragment. * <tt>Component</tt> and the given local username fragment.
*
* @param component the <tt>Component</tt> for which this candidate will * @param component the <tt>Component</tt> for which this candidate will
* serve. * serve.
* @param ufrag the local ICE username fragment for this candidate (and * @param ufrag the local ICE username fragment for this candidate (and
@ -263,7 +264,7 @@ public class SinglePortUdpHarvester
/** /**
* {@inheritDoc} * {@inheritDoc}
* * <p>
* Closes all sockets in use by this <tt>LocalCandidate</tt>. * Closes all sockets in use by this <tt>LocalCandidate</tt>.
*/ */
@Override @Override
@ -283,20 +284,21 @@ public class SinglePortUdpHarvester
StunStack stunStack = getStunStack(); StunStack stunStack = getStunStack();
for (Map.Entry<SocketAddress, DatagramSocket> e for (Map.Entry<SocketAddress, DatagramSocket> e
: sockets.entrySet()) : sockets.entrySet())
{ {
DatagramSocket socket = e.getValue(); DatagramSocket socket = e.getValue();
if (stunStack != null) if (stunStack != null)
{ {
TransportAddress localAddress TransportAddress localAddress
= new TransportAddress(socket.getLocalAddress(), = new TransportAddress(socket.getLocalAddress(),
socket.getLocalPort(), socket.getLocalPort(),
Transport.UDP); Transport.UDP);
TransportAddress remoteAddress TransportAddress remoteAddress
= new TransportAddress((InetSocketAddress) e.getKey(), = new TransportAddress(
(InetSocketAddress) e.getKey(),
Transport.UDP); Transport.UDP);
stunStack.removeSocket(localAddress, remoteAddress); stunStack.removeSocket(localAddress, remoteAddress);
} }
@ -318,11 +320,12 @@ public class SinglePortUdpHarvester
/** /**
* Adds a new <tt>Socket</tt> to this candidate, which is associated * Adds a new <tt>Socket</tt> to this candidate, which is associated
* with a particular remote address. * with a particular remote address.
*
* @param socket the socket to add. * @param socket the socket to add.
* @param remoteAddress the remote address for the socket. * @param remoteAddress the remote address for the socket.
*/ */
private synchronized void addSocket(DatagramSocket socket, private synchronized void addSocket(DatagramSocket socket,
InetSocketAddress remoteAddress) InetSocketAddress remoteAddress)
throws IOException throws IOException
{ {
if (freed) if (freed)
@ -337,16 +340,16 @@ public class SinglePortUdpHarvester
} }
IceProcessingState state IceProcessingState state
= component.getParentStream().getParentAgent().getState(); = component.getParentStream().getParentAgent().getState();
if (!IceProcessingState.WAITING.equals(state) if (!IceProcessingState.WAITING.equals(state)
&& !IceProcessingState.RUNNING.equals(state)) && !IceProcessingState.RUNNING.equals(state))
{ {
throw new IOException( throw new IOException(
"Agent state is " + state + ". Cannot add socket."); "Agent state is " + state + ". Cannot add socket.");
} }
MultiplexingDatagramSocket multiplexing MultiplexingDatagramSocket multiplexing
= new MultiplexingDatagramSocket(socket); = new MultiplexingDatagramSocket(socket);
// Socket to add to the candidate // Socket to add to the candidate
IceSocketWrapper candidateSocket IceSocketWrapper candidateSocket
@ -355,12 +358,12 @@ public class SinglePortUdpHarvester
// STUN-only filtered socket to add to the StunStack // STUN-only filtered socket to add to the StunStack
IceSocketWrapper stunSocket IceSocketWrapper stunSocket
= new IceUdpSocketWrapper( = new IceUdpSocketWrapper(
multiplexing.getSocket(new StunDatagramPacketFilter())); multiplexing.getSocket(new StunDatagramPacketFilter()));
component.getParentStream().getParentAgent().getStunStack() component.getParentStream().getParentAgent().getStunStack()
.addSocket( .addSocket(
stunSocket, stunSocket,
new TransportAddress(remoteAddress, Transport.UDP)); new TransportAddress(remoteAddress, Transport.UDP));
// TODO: maybe move this code to the candidates. // TODO: maybe move this code to the candidates.
component.getSocket().add(multiplexing); component.getSocket().add(multiplexing);
@ -377,14 +380,5 @@ public class SinglePortUdpHarvester
sockets.put(remoteAddress, socket); sockets.put(remoteAddress, socket);
} }
} }
/**
* {@inheritDoc}
*/
@Override
public IceSocketWrapper getIceSocketWrapper(SocketAddress remoteAddress)
{
return candidateSockets.get(remoteAddress);
}
} }
} }

View File

@ -208,8 +208,8 @@ public class RelayedCandidateDatagramSocket
this); this);
DatagramSocket hostSocket DatagramSocket hostSocket
= this.turnCandidateHarvest.hostCandidate.getIceSocketWrapper(). = this.turnCandidateHarvest.hostCandidate
getUDPSocket(); .getCandidateIceSocketWrapper().getUDPSocket();
if (hostSocket instanceof MultiplexingDatagramSocket) if (hostSocket instanceof MultiplexingDatagramSocket)
{ {

View File

@ -386,7 +386,7 @@ public class IcePseudoTcp
private static class LocalPseudoTcpJob extends Thread implements Runnable private static class LocalPseudoTcpJob extends Thread implements Runnable
{ {
private DatagramSocket dgramSocket; private DatagramSocket dgramSocket;
public LocalPseudoTcpJob(DatagramSocket socket) public LocalPseudoTcpJob(DatagramSocket socket)
throws UnknownHostException throws UnknownHostException