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>.
* @return the UDP <tt>DatagramSocket</tt> (if any) for this
* <tt>CandidatePair</tt>.
* @deprecated use {@link Component#getSocket()} instead.
*/
@Deprecated
public DatagramSocket getDatagramSocket()
{
IceSocketWrapper wrapper = getIceSocketWrapper();
@ -732,38 +734,22 @@ public class CandidatePair
/**
* 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>.
* @deprecated use {@link Component#getSocket()} instead.
*/
@Deprecated
public Socket getSocket()
{
IceSocketWrapper wrapper = getIceSocketWrapper();
return wrapper == null ? null : wrapper.getTCPSocket();
return null;
}
/**
* Returns 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()
{
LocalCandidate localCandidate = getLocalCandidate();
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();
return getParentComponent().getSocketWrapper();
}
}

View File

@ -191,9 +191,10 @@ public class GoogleRelayedCandidate
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
* @see LocalCandidate#getCandidateIceSocketWrapper()
*/
public synchronized IceSocketWrapper getIceSocketWrapper()
@Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{
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>.
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
* {@inheritDoc}
* SHOULD NOT be used outside ice4j. Only exposed for use in the
* org.ice4j.socket package.
*/
public IceSocketWrapper getIceSocketWrapper()
@Override
public IceSocketWrapper getCandidateIceSocketWrapper()
{
return socket;
}

View File

@ -108,8 +108,7 @@ public abstract class LocalCandidate
* <tt>Candidate</tt>
*
* @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getDatagramSocket()}
* on the appropriate <tt>CandidatePair</tt> instead.
* should use {@link org.ice4j.ice.Component#getSocket()} instead.
*/
@Deprecated
public DatagramSocket getDatagramSocket()
@ -126,39 +125,34 @@ public abstract class LocalCandidate
* <tt>Candidate</tt>
*
* @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getSocket()} on the
* appropriate <tt>CandidatePair</tt> instead.
* should use {@link org.ice4j.ice.Component#getSocket()} instead.
*/
@Deprecated
public Socket getSocket()
{
IceSocketWrapper wrapper = getIceSocketWrapper();
return wrapper == null ? null : wrapper.getTCPSocket();
return null;
}
/**
* Gets the <tt>IceSocketWrapper</tt> associated with this
* <tt>Candidate</tt>.
*
* @return the <tt>IceSocketWrapper</tt> associated with this
* <tt>Candidate</tt>
* @return the {@link IceSocketWrapper} instance of the {@link Component}
* which owns this {@link LocalCandidate}. Note that this IS NOT an
* instance specific to this {@link LocalCandidate}. See
* {@link #getCandidateIceSocketWrapper()}.
*/
protected abstract 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)
protected IceSocketWrapper getIceSocketWrapper()
{
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
* 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)
{
IceSocketWrapper hostSocket = getIceSocketWrapper();
IceSocketWrapper hostSocket = getCandidateIceSocketWrapper();
if (hostSocket != null
&& hostSocket.getTCPSocket() != null)
@ -323,7 +317,7 @@ public abstract class LocalCandidate
protected void free()
{
// Close the socket associated with this LocalCandidate.
IceSocketWrapper socket = getIceSocketWrapper();
IceSocketWrapper socket = getCandidateIceSocketWrapper();
if (socket != null)
{
@ -331,7 +325,7 @@ public abstract class LocalCandidate
if ((base == null)
|| (base == this)
|| (base.getIceSocketWrapper() != socket))
|| (base.getCandidateIceSocketWrapper() != socket))
{
//remove our socket from the stack.
getStunStack().removeSocket(getTransportAddress());

View File

@ -64,15 +64,11 @@ public class PeerReflexiveCandidate
}
/**
* Returns the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>.
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
* {@inheritDoc}
*/
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
* <tt>RelayedCandidate</tt>
*/
public synchronized RelayedCandidateDatagramSocket
private synchronized RelayedCandidateDatagramSocket
getRelayedCandidateDatagramSocket()
{
if (relayedCandidateDatagramSocket == null)
@ -127,9 +127,9 @@ public class RelayedCandidate
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* @see LocalCandidate#getIceSocketWrapper()
*/
public synchronized IceSocketWrapper getIceSocketWrapper()
@Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{
if (socket == null)
{

View File

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

View File

@ -83,16 +83,4 @@ public class TcpHostCandidate
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)
{
socket.close();
@ -89,13 +89,19 @@ public class UPNPCandidate
}
/**
* Gets the <tt>DatagramSocket</tt> associated with this <tt>Candidate</tt>.
*
* @return the <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
* {@inheritDoc}
*/
@Override
public IceSocketWrapper getIceSocketWrapper()
protected IceSocketWrapper getCandidateIceSocketWrapper()
{
return getBase().getCandidateIceSocketWrapper();
}
/**
* {@inheritDoc}
*/
@Override
protected IceSocketWrapper getIceSocketWrapper()
{
return getBase().getIceSocketWrapper();
}

View File

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

View File

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