From b4a5e2eb83c1ee700219d662f26da6d795bef33c Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Thu, 1 Dec 2016 12:01:10 -0600 Subject: [PATCH] refactor: Refactors the Component's socket API. --- src/main/java/org/ice4j/ice/Component.java | 59 ++++++++++++------- .../java/org/ice4j/ice/ComponentSocket.java | 3 +- .../harvest/GoogleTurnCandidateHarvest.java | 2 +- .../GoogleTurnSSLCandidateHarvester.java | 2 +- .../ice/harvest/HostCandidateHarvester.java | 2 +- .../ice/harvest/SinglePortUdpHarvester.java | 6 +- .../ice/harvest/StunCandidateHarvester.java | 2 +- .../org/ice4j/ice/harvest/TcpHarvester.java | 2 +- .../ice/harvest/TurnCandidateHarvest.java | 2 +- .../org/ice4j/ice/harvest/UPNPHarvester.java | 2 +- .../socket/IceTcpServerSocketWrapper.java | 2 +- 11 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index e8147b6..a306fd3 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -124,16 +124,27 @@ public class Component private final Logger logger; /** - * The {@link MergingDatagramSocket} instance which will serve as the - * single socket instance for this {@link Component}, merging received - * packets from all of its candidates. + * The single {@link ComponentSocket} instance for this {@link Component}, + * which will merge the multiple sockets from the candidate/pairs. */ - private final MergingDatagramSocket mergingDatagramSocket; + private final ComponentSocket componentSocket; /** - * A wrapper around {@link #mergingDatagramSocket}. + * The public view of {@link #componentSocket}, wrapped in a + * {@link MultiplexingDatagramSocket} for the convenience of users of the + * library. + * This is the instance which should be used by applications for + * reading/writing application data. */ - private final IceUdpSocketWrapper mergingDatagramSocketWrapper; + private final MultiplexingDatagramSocket socket; + + /** + * A wrapper around {@link #socket}, kept only to help preserve the old + * API (see {@link LocalCandidate#getIceSocketWrapper()} and + * {@link CandidatePair#getIceSocketWrapper()}) + */ + @Deprecated + private final IceSocketWrapper socketWrapper; /** * Creates a new Component with the specified componentID @@ -152,11 +163,9 @@ public class Component try { - mergingDatagramSocket = new ComponentSocket(this); - mergingDatagramSocketWrapper - = new IceUdpSocketWrapper( - new MultiplexingDatagramSocket( - mergingDatagramSocket)); + componentSocket = new ComponentSocket(this); + socket = new MultiplexingDatagramSocket(componentSocket); + socketWrapper = new IceUdpSocketWrapper(socket); } catch (SocketException se) { @@ -807,7 +816,7 @@ public class Component } } - getSocket().close(); + getComponentSocket().close(); } /** @@ -941,24 +950,30 @@ public class Component } /** - * @return the single socket for this {@link Component} which should be - * used for reading and writing data. + * @return the internal merging socket for this component. This is for + * ice4j use only. + * For reading/writing application data, use {@link #getSocket()}. */ - public MergingDatagramSocket getSocket() + public ComponentSocket getComponentSocket() { - return mergingDatagramSocket; + return componentSocket; + } + + /** + * @return the socket for this {@link Component}, which should be used for + * reading/writing application data. + */ + public MultiplexingDatagramSocket getSocket() + { + return socket; } /** * @return an {@link IceSocketWrapper} instance wrapping the socket for this - * candidate (see {@link #getSocket()}). - * @deprecated Use {@link #getSocket()} directly. This is only introduced - * to ease the transition of applications which are already written to use - * an {@link IceSocketWrapper} instance. + * candidate (see {@link #getComponentSocket()}). */ - @Deprecated public IceSocketWrapper getSocketWrapper() { - return mergingDatagramSocketWrapper; + return socketWrapper; } } diff --git a/src/main/java/org/ice4j/ice/ComponentSocket.java b/src/main/java/org/ice4j/ice/ComponentSocket.java index 6760320..4ff2267 100644 --- a/src/main/java/org/ice4j/ice/ComponentSocket.java +++ b/src/main/java/org/ice4j/ice/ComponentSocket.java @@ -29,7 +29,7 @@ import java.util.logging.*; * Extends {@link MergingDatagramSocket} with functionality specific to * an ICE {@link Component}. */ -class ComponentSocket +public class ComponentSocket extends MergingDatagramSocket implements PropertyChangeListener { @@ -155,6 +155,7 @@ class ComponentSocket } finally { + component.getParentStream().removePairStateChangeListener(this); component = null; } } diff --git a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java index 215717e..4e8e1aa 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java @@ -139,7 +139,7 @@ public class GoogleTurnCandidateHarvest = relayedCandidate.getCandidateIceSocketWrapper(); Component component = relayedCandidate.getParentComponent(); - component.getSocket().add(candidateSocket); + component.getComponentSocket().add(candidateSocket); addCandidate(relayedCandidate); } diff --git a/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java index 18639f1..87d913d 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java @@ -158,7 +158,7 @@ public class GoogleTurnSSLCandidateHarvester .getParentAgent() .getStunStack() .addSocket(cand.getStunSocket(null)); - parentComponent.getSocket().add(multiplexing); + parentComponent.getComponentSocket().add(multiplexing); } } catch (Exception e) diff --git a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java index 5a7b93a..1dedbf2 100644 --- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -418,7 +418,7 @@ public class HostCandidateHarvester // checks. In case we have enabled STUN, we are going to use // them as well while harvesting reflexive candidates. createAndRegisterStunSocket(candidate); - component.getSocket().add(sock); + component.getComponentSocket().add(sock); } } } diff --git a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java index 9ffb0bc..c0df2e3 100644 --- a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -18,18 +18,14 @@ package org.ice4j.ice.harvest; import org.ice4j.*; -import org.ice4j.attribute.*; import org.ice4j.ice.*; -import org.ice4j.message.*; import org.ice4j.socket.*; import org.ice4j.stack.*; -import org.ice4j.util.*; import java.io.*; import java.net.*; import java.util.*; import java.util.concurrent.*; -import java.util.logging.*; import java.util.logging.Logger; // Disambiguation /** @@ -366,7 +362,7 @@ public class SinglePortUdpHarvester new TransportAddress(remoteAddress, Transport.UDP)); // TODO: maybe move this code to the candidates. - component.getSocket().add(multiplexing); + component.getComponentSocket().add(multiplexing); // XXX is this necessary? synchronized (candidateSockets) diff --git a/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java index 3503ffa..db5390e 100644 --- a/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -458,7 +458,7 @@ public class StunCandidateHarvester hostCand.getParentComponent().getParentStream(). getParentAgent().getStunStack().addSocket( cand.getStunSocket(null)); - hostCand.getParentComponent().getSocket().add(multiplexing); + hostCand.getParentComponent().getComponentSocket().add(multiplexing); } catch(Exception io) { diff --git a/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java b/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java index 07ba748..ecd8aa4 100644 --- a/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java @@ -520,7 +520,7 @@ public class TcpHarvester candidate.addSocket(candidateSocket); // TODO: Maybe move this code to the candidate. - component.getSocket().add(multiplexing); + component.getComponentSocket().add(multiplexing); // the socket is not our responsibility anymore. It is up to // the candidate/component to close/free it. diff --git a/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java b/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java index cb237c4..47eb199 100644 --- a/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java @@ -258,7 +258,7 @@ public class TurnCandidateHarvest harvester.getStunStack().addSocket( relayedCandidate.getStunSocket(null)); - relayedCandidate.getParentComponent().getSocket().add( + relayedCandidate.getParentComponent().getComponentSocket().add( relayedCandidate.getCandidateIceSocketWrapper()); addCandidate(relayedCandidate); } diff --git a/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java b/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java index 8ae0677..80b0c5e 100644 --- a/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java @@ -223,7 +223,7 @@ public class UPNPHarvester = new UPNPCandidate(addr, base, component, device); IceSocketWrapper stunSocket = candidate.getStunSocket(null); candidate.getStunStack().addSocket(stunSocket); - component.getSocket().add(candidate.getCandidateIceSocketWrapper()); + component.getComponentSocket().add(candidate.getCandidateIceSocketWrapper()); ret.add(candidate); ret.add(base); diff --git a/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java b/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java index 3a38d7d..5535dcf 100644 --- a/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java +++ b/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java @@ -195,7 +195,7 @@ public class IceTcpServerSocketWrapper component.getParentStream().getParentAgent(). getStunStack().addSocket( new IceTcpSocketWrapper(multiplexingSocket)); - component.getSocket().add(multiplexingSocket); + component.getComponentSocket().add(multiplexingSocket); sockets.add(multiplexingSocket); }