From 15a2df1e597ab41f26b392257ba8547647ab71cb Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Mon, 28 Nov 2016 16:11:24 -0600 Subject: [PATCH] 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. --- .../java/org/ice4j/ice/CandidatePair.java | 30 +++-------- .../org/ice4j/ice/GoogleRelayedCandidate.java | 5 +- .../java/org/ice4j/ice/HostCandidate.java | 11 ++-- .../java/org/ice4j/ice/LocalCandidate.java | 48 ++++++++--------- .../org/ice4j/ice/PeerReflexiveCandidate.java | 12 ++--- .../java/org/ice4j/ice/RelayedCandidate.java | 6 +-- .../ice4j/ice/ServerReflexiveCandidate.java | 10 ++-- .../java/org/ice4j/ice/TcpHostCandidate.java | 12 ----- .../java/org/ice4j/ice/UPNPCandidate.java | 18 ++++--- .../ice/harvest/SinglePortUdpHarvester.java | 52 ++++++++----------- .../RelayedCandidateDatagramSocket.java | 4 +- src/test/java/test/IcePseudoTcp.java | 2 +- 12 files changed, 85 insertions(+), 125 deletions(-) diff --git a/src/main/java/org/ice4j/ice/CandidatePair.java b/src/main/java/org/ice4j/ice/CandidatePair.java index 902e804..f0a165f 100644 --- a/src/main/java/org/ice4j/ice/CandidatePair.java +++ b/src/main/java/org/ice4j/ice/CandidatePair.java @@ -722,7 +722,9 @@ public class CandidatePair * CandidatePair. * @return the UDP DatagramSocket (if any) for this * CandidatePair. + * @deprecated use {@link Component#getSocket()} instead. */ + @Deprecated public DatagramSocket getDatagramSocket() { IceSocketWrapper wrapper = getIceSocketWrapper(); @@ -732,38 +734,22 @@ public class CandidatePair /** * Returns the TCP Socket (if any) for this CandidatePair. * @return the TCP Socket (if any) for this CandidatePair. + * @deprecated use {@link Component#getSocket()} instead. */ + @Deprecated public Socket getSocket() { - IceSocketWrapper wrapper = getIceSocketWrapper(); - return wrapper == null ? null : wrapper.getTCPSocket(); + return null; } /** * Returns the IceSocketWrapper for this CandidatePair. * @return the IceSocketWrapper for this CandidatePair. + * @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(); } } diff --git a/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java b/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java index 5c6b81d..9a97198 100644 --- a/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java +++ b/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java @@ -191,9 +191,10 @@ public class GoogleRelayedCandidate * * @return the DatagramSocket associated with this * Candidate - * @see LocalCandidate#getIceSocketWrapper() + * @see LocalCandidate#getCandidateIceSocketWrapper() */ - public synchronized IceSocketWrapper getIceSocketWrapper() + @Override + protected synchronized IceSocketWrapper getCandidateIceSocketWrapper() { if (socket == null) { diff --git a/src/main/java/org/ice4j/ice/HostCandidate.java b/src/main/java/org/ice4j/ice/HostCandidate.java index d9cc77c..236c4d2 100644 --- a/src/main/java/org/ice4j/ice/HostCandidate.java +++ b/src/main/java/org/ice4j/ice/HostCandidate.java @@ -131,13 +131,12 @@ public class HostCandidate extends LocalCandidate } /** - * Gets the DatagramSocket associated with this Candidate. - * - * @return the DatagramSocket associated with this - * Candidate - * @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; } diff --git a/src/main/java/org/ice4j/ice/LocalCandidate.java b/src/main/java/org/ice4j/ice/LocalCandidate.java index 8e05090..560938c 100644 --- a/src/main/java/org/ice4j/ice/LocalCandidate.java +++ b/src/main/java/org/ice4j/ice/LocalCandidate.java @@ -108,8 +108,7 @@ public abstract class LocalCandidate * Candidate * * @deprecated This should be used by the library only. Users of ice4j - * should use {@link org.ice4j.ice.CandidatePair#getDatagramSocket()} - * on the appropriate CandidatePair instead. + * should use {@link org.ice4j.ice.Component#getSocket()} instead. */ @Deprecated public DatagramSocket getDatagramSocket() @@ -126,39 +125,34 @@ public abstract class LocalCandidate * Candidate * * @deprecated This should be used by the library only. Users of ice4j - * should use {@link org.ice4j.ice.CandidatePair#getSocket()} on the - * appropriate CandidatePair 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 IceSocketWrapper associated with this - * Candidate. - * - * @return the IceSocketWrapper associated with this - * Candidate + * @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 IceSocketWrapper associated with this - * Candidate 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 IceSocketWrapper associated with this - * Candidate 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 DatagramSocket 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()); diff --git a/src/main/java/org/ice4j/ice/PeerReflexiveCandidate.java b/src/main/java/org/ice4j/ice/PeerReflexiveCandidate.java index 02ea7ab..9ad34f5 100644 --- a/src/main/java/org/ice4j/ice/PeerReflexiveCandidate.java +++ b/src/main/java/org/ice4j/ice/PeerReflexiveCandidate.java @@ -64,15 +64,11 @@ public class PeerReflexiveCandidate } /** - * Returns the DatagramSocket associated with this - * Candidate. - * - * @return the DatagramSocket associated with this - * Candidate - * @see LocalCandidate#getIceSocketWrapper() + * {@inheritDoc} */ - public IceSocketWrapper getIceSocketWrapper() + @Override + public IceSocketWrapper getCandidateIceSocketWrapper() { - return getBase().getIceSocketWrapper(); + return getBase().getCandidateIceSocketWrapper(); } } diff --git a/src/main/java/org/ice4j/ice/RelayedCandidate.java b/src/main/java/org/ice4j/ice/RelayedCandidate.java index 08a89c5..9adf723 100644 --- a/src/main/java/org/ice4j/ice/RelayedCandidate.java +++ b/src/main/java/org/ice4j/ice/RelayedCandidate.java @@ -101,7 +101,7 @@ public class RelayedCandidate * @return the RelayedCandidateDatagramSocket of this * RelayedCandidate */ - public synchronized RelayedCandidateDatagramSocket + private synchronized RelayedCandidateDatagramSocket getRelayedCandidateDatagramSocket() { if (relayedCandidateDatagramSocket == null) @@ -127,9 +127,9 @@ public class RelayedCandidate * * @return the DatagramSocket associated with this * Candidate - * @see LocalCandidate#getIceSocketWrapper() */ - public synchronized IceSocketWrapper getIceSocketWrapper() + @Override + protected synchronized IceSocketWrapper getCandidateIceSocketWrapper() { if (socket == null) { diff --git a/src/main/java/org/ice4j/ice/ServerReflexiveCandidate.java b/src/main/java/org/ice4j/ice/ServerReflexiveCandidate.java index d0105f2..6dc21d5 100644 --- a/src/main/java/org/ice4j/ice/ServerReflexiveCandidate.java +++ b/src/main/java/org/ice4j/ice/ServerReflexiveCandidate.java @@ -104,16 +104,12 @@ public class ServerReflexiveCandidate } /** - * Gets the DatagramSocket associated with this Candidate. - * - * @return the DatagramSocket associated with this - * Candidate - * @see LocalCandidate#getIceSocketWrapper() + * {@inheritDoc} */ @Override - public IceSocketWrapper getIceSocketWrapper() + public IceSocketWrapper getCandidateIceSocketWrapper() { - return getBase().getIceSocketWrapper(); + return getBase().getCandidateIceSocketWrapper(); } /** diff --git a/src/main/java/org/ice4j/ice/TcpHostCandidate.java b/src/main/java/org/ice4j/ice/TcpHostCandidate.java index 8cd96d4..2b70642 100644 --- a/src/main/java/org/ice4j/ice/TcpHostCandidate.java +++ b/src/main/java/org/ice4j/ice/TcpHostCandidate.java @@ -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; - } } diff --git a/src/main/java/org/ice4j/ice/UPNPCandidate.java b/src/main/java/org/ice4j/ice/UPNPCandidate.java index 74646b3..95589e2 100644 --- a/src/main/java/org/ice4j/ice/UPNPCandidate.java +++ b/src/main/java/org/ice4j/ice/UPNPCandidate.java @@ -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 DatagramSocket associated with this Candidate. - * - * @return the DatagramSocket associated with this - * Candidate + * {@inheritDoc} */ @Override - public IceSocketWrapper getIceSocketWrapper() + protected IceSocketWrapper getCandidateIceSocketWrapper() + { + return getBase().getCandidateIceSocketWrapper(); + } + + /** + * {@inheritDoc} + */ + @Override + protected IceSocketWrapper getIceSocketWrapper() { return getBase().getIceSocketWrapper(); } diff --git a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java index be9e25c..9ffb0bc 100644 --- a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -230,26 +230,27 @@ public class SinglePortUdpHarvester * The collection of IceSocketWrappers 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. - * + *

* There are wrappers over MultiplexedDatagramSockets over * a corresponding socket in {@link #sockets}. */ private final Map candidateSockets - = new HashMap<>(); + = new HashMap<>(); /** * The collection of DatagramSockets added to this candidate. * The keys are the remote addresses for each socket. - * + *

* These are the "raw" sockets, before any wrappers are added for * the STUN stack or the user of ice4j. */ private final Map sockets - = new HashMap<>(); + = new HashMap<>(); /** * Initializes a new MyCandidate instance with the given * Component and the given local username fragment. + * * @param component the Component 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} - * + *

* Closes all sockets in use by this LocalCandidate. */ @Override @@ -283,20 +284,21 @@ public class SinglePortUdpHarvester StunStack stunStack = getStunStack(); for (Map.Entry 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 Socket 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); - } } } diff --git a/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java b/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java index 1c45e27..63b327b 100644 --- a/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/RelayedCandidateDatagramSocket.java @@ -208,8 +208,8 @@ public class RelayedCandidateDatagramSocket this); DatagramSocket hostSocket - = this.turnCandidateHarvest.hostCandidate.getIceSocketWrapper(). - getUDPSocket(); + = this.turnCandidateHarvest.hostCandidate + .getCandidateIceSocketWrapper().getUDPSocket(); if (hostSocket instanceof MultiplexingDatagramSocket) { diff --git a/src/test/java/test/IcePseudoTcp.java b/src/test/java/test/IcePseudoTcp.java index 76de9d6..5faa490 100755 --- a/src/test/java/test/IcePseudoTcp.java +++ b/src/test/java/test/IcePseudoTcp.java @@ -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