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;
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

View File

@ -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,7 +488,8 @@ public class Agent
* <tt>minPort</tt> and <tt>maxPort</tt> before reaching the maximum allowed
* number of retries.
*/
public Component createComponent( IceMediaStream stream,
public Component createComponent(
IceMediaStream stream,
Transport transport,
int preferredPort,
int minPort,
@ -495,6 +498,60 @@ public class Agent
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,
IOException,
BindException
{
if (transport != Transport.UDP)
{
@ -503,7 +560,8 @@ public class Agent
+ transport);
}
Component component = stream.createComponent(keepAliveStrategy);
Component component
= stream.createComponent(keepAliveStrategy, useComponentSocket);
gatherCandidates(component, preferredPort, minPort, maxPort);

View File

@ -722,7 +722,6 @@ 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()
@ -734,7 +733,6 @@ 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()
@ -745,11 +743,42 @@ public class CandidatePair
/**
* 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()
{
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;
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,
IceMediaStream mediaStream,
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
this.componentID = componentID;
@ -182,6 +202,8 @@ public class Component
Logger agentLogger = mediaStream.getParentAgent().getLogger();
if (useComponentSocket)
{
try
{
componentSocket = new ComponentSocket(this, agentLogger);
@ -192,6 +214,13 @@ public class Component
{
throw new RuntimeException(se);
}
}
else
{
componentSocket = null;
socket = null;
socketWrapper = null;
}
mediaStream.addPairChangeListener(this);
logger = new Logger(classLogger, agentLogger);
@ -839,9 +868,15 @@ public class Component
getParentStream().removePairStateChangeListener(this);
keepAlivePairs.clear();
getComponentSocket().close();
if (componentSocket != null)
{
componentSocket.close();
}
if (socket != null)
{
socket.close();
}
}
/**
* Frees a specific <tt>LocalCandidate</tt> and swallows any
@ -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.
* </p>
* 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()
{

View File

@ -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 <tt>Component</tt> 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);
}

View File

@ -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);
}

View File

@ -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)

View File

@ -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);
}
}
}
}

View File

@ -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)

View File

@ -451,14 +451,22 @@ public class StunCandidateHarvester
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)
{

View File

@ -488,12 +488,23 @@ 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))
{
// 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
IceSocketWrapper candidateSocket = null;
@ -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.
}

View File

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

View File

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

View File

@ -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);
}