feat: Makes the use of a component socket optional. (#147)

* feat: Makes the use of a component socket optional.
This commit is contained in:
bgrozev 2018-10-05 17:15:00 -05:00 committed by GitHub
parent 4aef25b91f
commit 7d2ad8f9a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 263 additions and 55 deletions

View File

@ -17,6 +17,8 @@
*/ */
package org.ice4j; package org.ice4j;
import org.ice4j.ice.*;
import java.util.*; import java.util.*;
import java.util.logging.*; import java.util.logging.*;
@ -247,6 +249,18 @@ public class StackProperties
public static final String HARVESTING_TIMEOUT public static final String HARVESTING_TIMEOUT
= "org.ice4j.ice.harvest.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 * Returns the String value of the specified property (minus all
* encompassing whitespaces)and null in case no property value was mapped * encompassing whitespaces)and null in case no property value was mapped

View File

@ -455,7 +455,9 @@ public class Agent
return createComponent( return createComponent(
stream, transport, stream, transport,
preferredPort, minPort, maxPort, preferredPort, minPort, maxPort,
KeepAliveStrategy.SELECTED_ONLY); KeepAliveStrategy.SELECTED_ONLY,
StackProperties.getBoolean(
StackProperties.USE_COMPONENT_SOCKET, true));
} }
/** /**
@ -486,12 +488,67 @@ public class Agent
* <tt>minPort</tt> and <tt>maxPort</tt> before reaching the maximum allowed * <tt>minPort</tt> and <tt>maxPort</tt> before reaching the maximum allowed
* number of retries. * number of retries.
*/ */
public Component createComponent( IceMediaStream stream, public Component createComponent(
Transport transport, IceMediaStream stream,
int preferredPort, Transport transport,
int minPort, int preferredPort,
int maxPort, int minPort,
KeepAliveStrategy keepAliveStrategy) 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 <tt>stream</tt> 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 <tt>Candidate</tt> sockets for this <tt>Component</tt>.
* @param minPort the port number where we should first try to bind before
* moving to the next one (i.e. <tt>minPort + 1</tt>)
* @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 <tt>minPort</tt> or
* <tt>maxPort</tt> is not a valid port number or if <tt>minPort &gt;
* maxPort</tt>, or if <tt>transport</tt> 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
* <tt>minPort</tt> and <tt>maxPort</tt> 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, throws IllegalArgumentException,
IOException, IOException,
BindException BindException
@ -503,7 +560,8 @@ public class Agent
+ transport); + transport);
} }
Component component = stream.createComponent(keepAliveStrategy); Component component
= stream.createComponent(keepAliveStrategy, useComponentSocket);
gatherCandidates(component, preferredPort, minPort, maxPort); gatherCandidates(component, preferredPort, minPort, maxPort);

View File

@ -722,7 +722,6 @@ 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 @Deprecated
public DatagramSocket getDatagramSocket() public DatagramSocket getDatagramSocket()
@ -734,7 +733,6 @@ 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 @Deprecated
public Socket getSocket() public Socket getSocket()
@ -745,11 +743,42 @@ public class CandidatePair
/** /**
* 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 @Deprecated
public IceSocketWrapper getIceSocketWrapper() 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();
} }
} }

View File

@ -18,6 +18,7 @@
package org.ice4j.ice; package org.ice4j.ice;
import java.beans.*; import java.beans.*;
import java.io.*;
import java.net.*; import java.net.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -127,8 +128,9 @@ public class Component
private final Logger logger; private final Logger logger;
/** /**
* The single {@link ComponentSocket} instance for this {@link Component}, * The {@link ComponentSocket} instance which, if enabled, will serve as the
* which will merge the multiple sockets from the candidate/pairs. * single socket instance for this {@link Component}, merging received
* packets from all of its candidates.
*/ */
private final ComponentSocket componentSocket; private final ComponentSocket componentSocket;
@ -169,10 +171,28 @@ public class Component
* @param componentID the id of this component. * @param componentID the id of this component.
* @param mediaStream the {@link IceMediaStream} instance that would be the * @param mediaStream the {@link IceMediaStream} instance that would be the
* parent of this component. * parent of this component.
* @deprecated
*/ */
protected Component(int componentID, protected Component(int componentID,
IceMediaStream mediaStream, IceMediaStream mediaStream,
KeepAliveStrategy keepAliveStrategy) KeepAliveStrategy keepAliveStrategy)
{
this(componentID, mediaStream, keepAliveStrategy, true);
}
/**
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
* as a child of the specified <tt>IceMediaStream</tt>.
*
* @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 // the max value for componentID is 256
this.componentID = componentID; this.componentID = componentID;
@ -182,15 +202,24 @@ public class Component
Logger agentLogger = mediaStream.getParentAgent().getLogger(); Logger agentLogger = mediaStream.getParentAgent().getLogger();
try if (useComponentSocket)
{ {
componentSocket = new ComponentSocket(this, agentLogger); try
socket = new MultiplexingDatagramSocket(componentSocket); {
socketWrapper = new IceUdpSocketWrapper(socket); 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); mediaStream.addPairChangeListener(this);
@ -839,8 +868,14 @@ public class Component
getParentStream().removePairStateChangeListener(this); getParentStream().removePairStateChangeListener(this);
keepAlivePairs.clear(); keepAlivePairs.clear();
getComponentSocket().close(); if (componentSocket != null)
socket.close(); {
componentSocket.close();
}
if (socket != null)
{
socket.close();
}
} }
/** /**
@ -986,13 +1021,20 @@ public class Component
{ {
return return
new Component( 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 * @return the single socket for this {@link Component} which should be
* ice4j use only. * used for reading and writing data, if the component socket is enabled
* For reading/writing application data, use {@link #getSocket()}. * and {@code null} if it is not enabled.
* </p>
* This is for ice4j internal use only! For reading/writing application
* data, use {@link #getSocket()}.
*/ */
public ComponentSocket getComponentSocket() public ComponentSocket getComponentSocket()
{ {
@ -1001,7 +1043,9 @@ public class Component
/** /**
* @return the socket for this {@link Component}, which should be used for * @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() public MultiplexingDatagramSocket getSocket()
{ {
@ -1010,7 +1054,10 @@ public class Component
/** /**
* @return an {@link IceSocketWrapper} instance wrapping the socket for this * @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() public IceSocketWrapper getSocketWrapper()
{ {

View File

@ -175,18 +175,26 @@ public class IceMediaStream
* *
* @param keepAliveStrategy the keep-alive strategy, which dictates which * @param keepAliveStrategy the keep-alive strategy, which dictates which
* candidates pairs are going to be kept alive. * 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 <tt>Component</tt> after adding it to * @return the newly created stream <tt>Component</tt> after adding it to
* the stream first. * the stream first.
*/ */
protected Component createComponent(KeepAliveStrategy keepAliveStrategy) protected Component createComponent(
KeepAliveStrategy keepAliveStrategy,
boolean useComponentSocket)
{ {
Component component; Component component;
synchronized (components) synchronized (components)
{ {
component component
= new Component(++lastComponentID, this, keepAliveStrategy); = new Component(
++lastComponentID,
this,
keepAliveStrategy,
useComponentSocket);
components.put(component.getComponentID(), component); components.put(component.getComponentID(), component);
} }

View File

@ -138,8 +138,12 @@ public class GoogleTurnCandidateHarvest
IceSocketWrapper candidateSocket IceSocketWrapper candidateSocket
= relayedCandidate.getCandidateIceSocketWrapper(); = relayedCandidate.getCandidateIceSocketWrapper();
Component component = relayedCandidate.getParentComponent(); ComponentSocket componentSocket
component.getComponentSocket().add(candidateSocket); = relayedCandidate.getParentComponent().getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(candidateSocket);
}
addCandidate(relayedCandidate); addCandidate(relayedCandidate);
} }

View File

@ -209,7 +209,7 @@ public class GoogleTurnSSLCandidateHarvester
OutputStream outputStream = sock.getOutputStream(); OutputStream outputStream = sock.getOutputStream();
InputStream inputStream = sock.getInputStream(); InputStream inputStream = sock.getInputStream();
if(sslHandshake(inputStream, outputStream)) if (sslHandshake(inputStream, outputStream))
{ {
Component parentComponent = hostCand.getParentComponent(); Component parentComponent = hostCand.getParentComponent();
MultiplexingSocket multiplexing = new MultiplexingSocket(sock); MultiplexingSocket multiplexing = new MultiplexingSocket(sock);
@ -224,7 +224,12 @@ public class GoogleTurnSSLCandidateHarvester
.getParentAgent() .getParentAgent()
.getStunStack() .getStunStack()
.addSocket(cand.getStunSocket(null)); .addSocket(cand.getStunSocket(null));
parentComponent.getComponentSocket().add(multiplexing); ComponentSocket componentSocket
= parentComponent.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(multiplexing);
}
} }
} }
catch (Exception e) catch (Exception e)

View File

@ -410,7 +410,7 @@ public class HostCandidateHarvester
NetworkUtils.isInterfaceVirtual(iface)); NetworkUtils.isInterfaceVirtual(iface));
component.addLocalCandidate(candidate); component.addLocalCandidate(candidate);
if(transport == Transport.TCP) if (transport == Transport.TCP)
{ {
// have to wait a client connection to add a STUN socket // have to wait a client connection to add a STUN socket
// to the StunStack // to the StunStack
@ -422,7 +422,13 @@ public class HostCandidateHarvester
// checks. In case we have enabled STUN, we are going to use // checks. In case we have enabled STUN, we are going to use
// them as well while harvesting reflexive candidates. // them as well while harvesting reflexive candidates.
createAndRegisterStunSocket(candidate); createAndRegisterStunSocket(candidate);
component.getComponentSocket().add(sock);
ComponentSocket componentSocket
= component.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(sock);
}
} }
} }
} }

View File

@ -371,8 +371,12 @@ public class SinglePortUdpHarvester
stunSocket, stunSocket,
new TransportAddress(remoteAddress, Transport.UDP)); new TransportAddress(remoteAddress, Transport.UDP));
// TODO: maybe move this code to the candidates. MergingDatagramSocket componentSocket
component.getComponentSocket().add(multiplexing); = component.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(multiplexing);
}
// XXX is this necessary? // XXX is this necessary?
synchronized (candidateSockets) synchronized (candidateSockets)

View File

@ -444,21 +444,29 @@ public class StunCandidateHarvester
HostCandidate cand; HostCandidate cand;
// create a new TCP HostCandidate // create a new TCP HostCandidate
if(hostCand.getTransport() == Transport.TCP) if (hostCand.getTransport() == Transport.TCP)
{ {
try try
{ {
Socket sock = new Socket(stunServer.getAddress(), Socket sock = new Socket(stunServer.getAddress(),
stunServer.getPort()); stunServer.getPort());
MultiplexingSocket multiplexing = new MultiplexingSocket(sock); MultiplexingSocket multiplexing = new MultiplexingSocket(sock);
Component component = hostCand.getParentComponent();
cand cand
= new HostCandidate( = new HostCandidate(
new IceTcpSocketWrapper(multiplexing), new IceTcpSocketWrapper(multiplexing),
hostCand.getParentComponent(), Transport.TCP); component,
hostCand.getParentComponent().getParentStream(). Transport.TCP);
component.getParentStream().
getParentAgent().getStunStack().addSocket( getParentAgent().getStunStack().addSocket(
cand.getStunSocket(null)); cand.getStunSocket(null));
hostCand.getParentComponent().getComponentSocket().add(multiplexing);
ComponentSocket componentSocket
= component.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(multiplexing);
}
} }
catch(Exception io) catch(Exception io)
{ {

View File

@ -488,11 +488,22 @@ public class TcpHarvester
IceProcessingState state IceProcessingState state
= component.getParentStream().getParentAgent().getState(); = component.getParentStream().getParentAgent().getState();
if (logger.isLoggable(Level.FINE) if (!IceProcessingState.WAITING.equals(state)
&& !IceProcessingState.WAITING.equals(state)
&& !IceProcessingState.RUNNING.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 // Socket to add to the candidate
@ -519,9 +530,11 @@ public class TcpHarvester
.addSocket(stunSocket); .addSocket(stunSocket);
candidate.addSocket(candidateSocket); candidate.addSocket(candidateSocket);
// TODO: Maybe move this code to the candidate. MergingDatagramSocket componentSocket = component.getComponentSocket();
component.getComponentSocket().add(multiplexing); if (componentSocket != null)
{
componentSocket.add(multiplexing);
}
// the socket is not our responsibility anymore. It is up to // the socket is not our responsibility anymore. It is up to
// the candidate/component to close/free it. // the candidate/component to close/free it.
} }

View File

@ -258,8 +258,13 @@ public class TurnCandidateHarvest
harvester.getStunStack().addSocket( harvester.getStunStack().addSocket(
relayedCandidate.getStunSocket(null)); relayedCandidate.getStunSocket(null));
relayedCandidate.getParentComponent().getComponentSocket().add( ComponentSocket componentSocket
relayedCandidate.getCandidateIceSocketWrapper()); = relayedCandidate.getParentComponent().getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(
relayedCandidate.getCandidateIceSocketWrapper());
}
addCandidate(relayedCandidate); addCandidate(relayedCandidate);
} }
} }

View File

@ -207,11 +207,9 @@ public class UPNPHarvester
* @param device the UPnP gateway device * @param device the UPnP gateway device
* @return a new <tt>UPNPCandidate</tt> instance which * @return a new <tt>UPNPCandidate</tt> instance which
* represents the specified <tt>TransportAddress</tt> * represents the specified <tt>TransportAddress</tt>
* @throws Exception if something goes wrong during candidate creation
*/ */
private List<LocalCandidate> createUPNPCandidate(IceSocketWrapper socket, private List<LocalCandidate> createUPNPCandidate(IceSocketWrapper socket,
String externalIP, int port, Component component, GatewayDevice device) String externalIP, int port, Component component, GatewayDevice device)
throws Exception
{ {
List<LocalCandidate> ret = new ArrayList<>(); List<LocalCandidate> ret = new ArrayList<>();
TransportAddress addr TransportAddress addr
@ -223,7 +221,11 @@ public class UPNPHarvester
= new UPNPCandidate(addr, base, component, device); = new UPNPCandidate(addr, base, component, device);
IceSocketWrapper stunSocket = candidate.getStunSocket(null); IceSocketWrapper stunSocket = candidate.getStunSocket(null);
candidate.getStunStack().addSocket(stunSocket); 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(candidate);
ret.add(base); ret.add(base);

View File

@ -195,7 +195,12 @@ public class IceTcpServerSocketWrapper
component.getParentStream().getParentAgent(). component.getParentStream().getParentAgent().
getStunStack().addSocket( getStunStack().addSocket(
new IceTcpSocketWrapper(multiplexingSocket)); new IceTcpSocketWrapper(multiplexingSocket));
component.getComponentSocket().add(multiplexingSocket); ComponentSocket componentSocket
= component.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(multiplexingSocket);
}
sockets.add(multiplexingSocket); sockets.add(multiplexingSocket);
} }