From 7d2ad8f9a2168295dac1cfb2ba17c983be5ee2f8 Mon Sep 17 00:00:00 2001 From: bgrozev Date: Fri, 5 Oct 2018 17:15:00 -0500 Subject: [PATCH] feat: Makes the use of a component socket optional. (#147) * feat: Makes the use of a component socket optional. --- src/main/java/org/ice4j/StackProperties.java | 14 ++++ src/main/java/org/ice4j/ice/Agent.java | 74 +++++++++++++++-- .../java/org/ice4j/ice/CandidatePair.java | 37 ++++++++- src/main/java/org/ice4j/ice/Component.java | 81 +++++++++++++++---- .../java/org/ice4j/ice/IceMediaStream.java | 12 ++- .../harvest/GoogleTurnCandidateHarvest.java | 8 +- .../GoogleTurnSSLCandidateHarvester.java | 9 ++- .../ice/harvest/HostCandidateHarvester.java | 10 ++- .../ice/harvest/SinglePortUdpHarvester.java | 8 +- .../ice/harvest/StunCandidateHarvester.java | 16 +++- .../org/ice4j/ice/harvest/TcpHarvester.java | 25 ++++-- .../ice/harvest/TurnCandidateHarvest.java | 9 ++- .../org/ice4j/ice/harvest/UPNPHarvester.java | 8 +- .../socket/IceTcpServerSocketWrapper.java | 7 +- 14 files changed, 263 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/ice4j/StackProperties.java b/src/main/java/org/ice4j/StackProperties.java index b533aaa..a8ee83c 100644 --- a/src/main/java/org/ice4j/StackProperties.java +++ b/src/main/java/org/ice4j/StackProperties.java @@ -17,6 +17,8 @@ */ package org.ice4j; +import org.ice4j.ice.*; + import java.util.*; import java.util.logging.*; @@ -247,6 +249,18 @@ public class StackProperties public static final String HARVESTING_TIMEOUT = "org.ice4j.ice.harvest.HARVESTING_TIMEOUT"; + /** + * The name of the property which specifies whether the per-component + * merging socket should be enabled by default (the default value can be + * overridden with {@link Agent#createComponent(IceMediaStream, Transport, + * int, int, int, KeepAliveStrategy, boolean)}. + * If enabled, the user of the library must use the socket instance provided + * by {@link Component#getSocket()}. Otherwise, the socket instance from the + * desired {@link CandidatePair} must be used. + */ + public static final String USE_COMPONENT_SOCKET + = "org.ice4j.ice.USE_COMPONENT_SOCKET"; + /** * Returns the String value of the specified property (minus all * encompassing whitespaces)and null in case no property value was mapped diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index d8a9fe0..da070b3 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -455,7 +455,9 @@ public class Agent return createComponent( stream, transport, preferredPort, minPort, maxPort, - KeepAliveStrategy.SELECTED_ONLY); + KeepAliveStrategy.SELECTED_ONLY, + StackProperties.getBoolean( + StackProperties.USE_COMPONENT_SOCKET, true)); } /** @@ -486,12 +488,67 @@ public class Agent * minPort and maxPort before reaching the maximum allowed * number of retries. */ - public Component createComponent( IceMediaStream stream, - Transport transport, - int preferredPort, - int minPort, - int maxPort, - KeepAliveStrategy keepAliveStrategy) + public Component createComponent( + IceMediaStream stream, + Transport transport, + int preferredPort, + int minPort, + int maxPort, + KeepAliveStrategy keepAliveStrategy) + throws IllegalArgumentException, + IOException, + BindException + { + return createComponent( + stream, + transport, + preferredPort, + minPort, + maxPort, + keepAliveStrategy, + true); + } + + /** + * Creates a new {@link Component} for the specified stream and + * allocates potentially all local candidates that should belong to it. + * + * @param stream the {@link IceMediaStream} that the new {@link Component} + * should belong to. + * @param transport the transport protocol used by the component + * @param preferredPort the port number that should be tried first when + * binding local Candidate sockets for this Component. + * @param minPort the port number where we should first try to bind before + * moving to the next one (i.e. minPort + 1) + * @param maxPort the maximum port number where we should try binding + * before giving up and throwing an exception. + * @param keepAliveStrategy the keep-alive strategy, which dictates which + * candidates pairs are going to be kept alive. + * @param useComponentSocket whether to use the component socket mode (in + * which case the socket is available through the {@link Component} directly), + * or not (in which case the socket is available through the selected + * {@link CandidatePair}). + * + * @return the newly created {@link Component} and with a list containing + * all and only local candidates. + * + * @throws IllegalArgumentException if either minPort or + * maxPort is not a valid port number or if minPort > + * maxPort, or if transport is not currently supported. + * @throws IOException if an error occurs while the underlying resolver lib + * is using sockets. + * @throws BindException if we couldn't find a free port between + * minPort and maxPort before reaching the maximum allowed + * number of retries. + */ + public Component createComponent( + IceMediaStream stream, + Transport transport, + int preferredPort, + int minPort, + int maxPort, + KeepAliveStrategy keepAliveStrategy, + boolean useComponentSocket) throws IllegalArgumentException, IOException, BindException @@ -503,7 +560,8 @@ public class Agent + transport); } - Component component = stream.createComponent(keepAliveStrategy); + Component component + = stream.createComponent(keepAliveStrategy, useComponentSocket); gatherCandidates(component, preferredPort, minPort, maxPort); diff --git a/src/main/java/org/ice4j/ice/CandidatePair.java b/src/main/java/org/ice4j/ice/CandidatePair.java index 03a54b8..2d1a617 100644 --- a/src/main/java/org/ice4j/ice/CandidatePair.java +++ b/src/main/java/org/ice4j/ice/CandidatePair.java @@ -722,7 +722,6 @@ public class CandidatePair * CandidatePair. * @return the UDP DatagramSocket (if any) for this * CandidatePair. - * @deprecated use {@link Component#getSocket()} instead. */ @Deprecated public DatagramSocket getDatagramSocket() @@ -734,7 +733,6 @@ 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() @@ -745,11 +743,42 @@ public class CandidatePair /** * Returns the IceSocketWrapper for this CandidatePair. * @return the IceSocketWrapper for this CandidatePair. - * @deprecated use {@link Component#getSocket()} instead. */ @Deprecated public IceSocketWrapper getIceSocketWrapper() { - return getParentComponent().getSocketWrapper(); + IceSocketWrapper componentSocket + = getParentComponent().getSocketWrapper(); + // If the merging socket is used, all candidate pairs just refer to the + // component. + if (componentSocket != null) + { + return getParentComponent().getSocketWrapper(); + } + + 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.getCandidateIceSocketWrapper(remoteAddress); + } + } + + return localCandidate.getCandidateIceSocketWrapper(); } } diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 398fbfe..62976a3 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -18,6 +18,7 @@ package org.ice4j.ice; import java.beans.*; +import java.io.*; import java.net.*; import java.util.*; import java.util.concurrent.*; @@ -127,8 +128,9 @@ public class Component private final Logger logger; /** - * The single {@link ComponentSocket} instance for this {@link Component}, - * which will merge the multiple sockets from the candidate/pairs. + * The {@link ComponentSocket} instance which, if enabled, will serve as the + * single socket instance for this {@link Component}, merging received + * packets from all of its candidates. */ private final ComponentSocket componentSocket; @@ -169,10 +171,28 @@ public class Component * @param componentID the id of this component. * @param mediaStream the {@link IceMediaStream} instance that would be the * parent of this component. + * @deprecated */ - protected Component(int componentID, + protected Component(int componentID, IceMediaStream mediaStream, KeepAliveStrategy keepAliveStrategy) + { + this(componentID, mediaStream, keepAliveStrategy, true); + } + + /** + * Creates a new Component with the specified componentID + * as a child of the specified IceMediaStream. + * + * @param componentID the id of this component. + * @param mediaStream the {@link IceMediaStream} instance that would be the + * parent of this component. + * @param useComponentSocket whether the component socket should be used. + */ + protected Component(int componentID, + IceMediaStream mediaStream, + KeepAliveStrategy keepAliveStrategy, + boolean useComponentSocket) { // the max value for componentID is 256 this.componentID = componentID; @@ -182,15 +202,24 @@ public class Component Logger agentLogger = mediaStream.getParentAgent().getLogger(); - try + if (useComponentSocket) { - componentSocket = new ComponentSocket(this, agentLogger); - socket = new MultiplexingDatagramSocket(componentSocket); - socketWrapper = new IceUdpSocketWrapper(socket); + try + { + componentSocket = new ComponentSocket(this, agentLogger); + socket = new MultiplexingDatagramSocket(componentSocket); + socketWrapper = new IceUdpSocketWrapper(socket); + } + catch (SocketException se) + { + throw new RuntimeException(se); + } } - catch (SocketException se) + else { - throw new RuntimeException(se); + componentSocket = null; + socket = null; + socketWrapper = null; } mediaStream.addPairChangeListener(this); @@ -839,8 +868,14 @@ public class Component getParentStream().removePairStateChangeListener(this); keepAlivePairs.clear(); - getComponentSocket().close(); - socket.close(); + if (componentSocket != null) + { + componentSocket.close(); + } + if (socket != null) + { + socket.close(); + } } /** @@ -986,13 +1021,20 @@ public class Component { return new Component( - componentID, mediaStream, KeepAliveStrategy.SELECTED_ONLY); + componentID, + mediaStream, + KeepAliveStrategy.SELECTED_ONLY, + StackProperties.getBoolean( + StackProperties.USE_COMPONENT_SOCKET, true)); } /** - * @return the internal merging socket for this component. This is for - * ice4j use only. - * For reading/writing application data, use {@link #getSocket()}. + * @return the single socket for this {@link Component} which should be + * used for reading and writing data, if the component socket is enabled + * and {@code null} if it is not enabled. + *

+ * This is for ice4j internal use only! For reading/writing application + * data, use {@link #getSocket()}. */ public ComponentSocket getComponentSocket() { @@ -1001,7 +1043,9 @@ public class Component /** * @return the socket for this {@link Component}, which should be used for - * reading/writing application data. + * reading/writing application data. If the component socket is not enabled, + * this returns {@code null} and users of the library should use the socket + * instance from the desired candidate pair instead. */ public MultiplexingDatagramSocket getSocket() { @@ -1010,7 +1054,10 @@ public class Component /** * @return an {@link IceSocketWrapper} instance wrapping the socket for this - * candidate (see {@link #getComponentSocket()}). + * 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 + * a {@link IceSocketWrapper} instance. */ public IceSocketWrapper getSocketWrapper() { diff --git a/src/main/java/org/ice4j/ice/IceMediaStream.java b/src/main/java/org/ice4j/ice/IceMediaStream.java index 564f180..8056e3a 100644 --- a/src/main/java/org/ice4j/ice/IceMediaStream.java +++ b/src/main/java/org/ice4j/ice/IceMediaStream.java @@ -175,18 +175,26 @@ public class IceMediaStream * * @param keepAliveStrategy the keep-alive strategy, which dictates which * candidates pairs are going to be kept alive. + * @param useComponentSocket whether the "component socket" should be used + * or not. See {@link StackProperties#USE_COMPONENT_SOCKET}. * * @return the newly created stream Component after adding it to * the stream first. */ - protected Component createComponent(KeepAliveStrategy keepAliveStrategy) + protected Component createComponent( + KeepAliveStrategy keepAliveStrategy, + boolean useComponentSocket) { Component component; synchronized (components) { component - = new Component(++lastComponentID, this, keepAliveStrategy); + = new Component( + ++lastComponentID, + this, + keepAliveStrategy, + useComponentSocket); components.put(component.getComponentID(), component); } diff --git a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java index 4e8e1aa..7fc42bc 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java @@ -138,8 +138,12 @@ public class GoogleTurnCandidateHarvest IceSocketWrapper candidateSocket = relayedCandidate.getCandidateIceSocketWrapper(); - Component component = relayedCandidate.getParentComponent(); - component.getComponentSocket().add(candidateSocket); + ComponentSocket componentSocket + = relayedCandidate.getParentComponent().getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 6ba1a6f..1144c4a 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java @@ -209,7 +209,7 @@ public class GoogleTurnSSLCandidateHarvester OutputStream outputStream = sock.getOutputStream(); InputStream inputStream = sock.getInputStream(); - if(sslHandshake(inputStream, outputStream)) + if (sslHandshake(inputStream, outputStream)) { Component parentComponent = hostCand.getParentComponent(); MultiplexingSocket multiplexing = new MultiplexingSocket(sock); @@ -224,7 +224,12 @@ public class GoogleTurnSSLCandidateHarvester .getParentAgent() .getStunStack() .addSocket(cand.getStunSocket(null)); - parentComponent.getComponentSocket().add(multiplexing); + ComponentSocket componentSocket + = parentComponent.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 4e6c4b2..1d53fcd 100644 --- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -410,7 +410,7 @@ public class HostCandidateHarvester NetworkUtils.isInterfaceVirtual(iface)); component.addLocalCandidate(candidate); - if(transport == Transport.TCP) + if (transport == Transport.TCP) { // have to wait a client connection to add a STUN socket // to the StunStack @@ -422,7 +422,13 @@ 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.getComponentSocket().add(sock); + + ComponentSocket componentSocket + = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 92d7cef..f189553 100644 --- a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -371,8 +371,12 @@ public class SinglePortUdpHarvester stunSocket, new TransportAddress(remoteAddress, Transport.UDP)); - // TODO: maybe move this code to the candidates. - component.getComponentSocket().add(multiplexing); + MergingDatagramSocket componentSocket + = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 db5390e..43d5cac 100644 --- a/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -444,21 +444,29 @@ public class StunCandidateHarvester HostCandidate cand; // create a new TCP HostCandidate - if(hostCand.getTransport() == Transport.TCP) + if (hostCand.getTransport() == Transport.TCP) { try { Socket sock = new Socket(stunServer.getAddress(), stunServer.getPort()); MultiplexingSocket multiplexing = new MultiplexingSocket(sock); + Component component = hostCand.getParentComponent(); cand = new HostCandidate( new IceTcpSocketWrapper(multiplexing), - hostCand.getParentComponent(), Transport.TCP); - hostCand.getParentComponent().getParentStream(). + component, + Transport.TCP); + component.getParentStream(). getParentAgent().getStunStack().addSocket( cand.getStunSocket(null)); - hostCand.getParentComponent().getComponentSocket().add(multiplexing); + + ComponentSocket componentSocket + = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 ecd8aa4..78768d1 100644 --- a/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/TcpHarvester.java @@ -488,11 +488,22 @@ public class TcpHarvester IceProcessingState state = component.getParentStream().getParentAgent().getState(); - if (logger.isLoggable(Level.FINE) - && !IceProcessingState.WAITING.equals(state) + if (!IceProcessingState.WAITING.equals(state) && !IceProcessingState.RUNNING.equals(state)) { - logger.fine("Adding a socket to an Agent in state " + state); + // If we are using the merging socket, we can still make use of the + // new socket. Otherwise, we have no use for it, so we better close + // it (and log a warning) early. + if (component.getComponentSocket() == null) + { + throw new IllegalStateException( + "The associated Agent is in state " + state + + " and we are not using a component socket"); + } + else if (logger.isLoggable(Level.FINE)) + { + logger.fine("Adding a socket to an Agent in state " + state); + } } // Socket to add to the candidate @@ -519,9 +530,11 @@ public class TcpHarvester .addSocket(stunSocket); candidate.addSocket(candidateSocket); - // TODO: Maybe move this code to the candidate. - component.getComponentSocket().add(multiplexing); - + MergingDatagramSocket componentSocket = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 47eb199..eedf3d8 100644 --- a/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java @@ -258,8 +258,13 @@ public class TurnCandidateHarvest harvester.getStunStack().addSocket( relayedCandidate.getStunSocket(null)); - relayedCandidate.getParentComponent().getComponentSocket().add( - relayedCandidate.getCandidateIceSocketWrapper()); + ComponentSocket componentSocket + = relayedCandidate.getParentComponent().getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 80b0c5e..64521b0 100644 --- a/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java @@ -207,11 +207,9 @@ public class UPNPHarvester * @param device the UPnP gateway device * @return a new UPNPCandidate instance which * represents the specified TransportAddress - * @throws Exception if something goes wrong during candidate creation */ private List createUPNPCandidate(IceSocketWrapper socket, String externalIP, int port, Component component, GatewayDevice device) - throws Exception { List ret = new ArrayList<>(); TransportAddress addr @@ -223,7 +221,11 @@ public class UPNPHarvester = new UPNPCandidate(addr, base, component, device); IceSocketWrapper stunSocket = candidate.getStunSocket(null); candidate.getStunStack().addSocket(stunSocket); - component.getComponentSocket().add(candidate.getCandidateIceSocketWrapper()); + ComponentSocket componentSocket = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.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 5535dcf..0f8dd6a 100644 --- a/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java +++ b/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java @@ -195,7 +195,12 @@ public class IceTcpServerSocketWrapper component.getParentStream().getParentAgent(). getStunStack().addSocket( new IceTcpSocketWrapper(multiplexingSocket)); - component.getComponentSocket().add(multiplexingSocket); + ComponentSocket componentSocket + = component.getComponentSocket(); + if (componentSocket != null) + { + componentSocket.add(multiplexingSocket); + } sockets.add(multiplexingSocket); }