Permalink
Browse files

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

* feat: Makes the use of a component socket optional.
  • Loading branch information...
bgrozev committed Oct 5, 2018
1 parent 4aef25b commit 7d2ad8f9a2168295dac1cfb2ba17c983be5ee2f8
@@ -17,6 +17,8 @@
*/
package org.ice4j;
import org.ice4j.ice.*;
import java.util.*;
import java.util.logging.*;
@@ -247,6 +249,18 @@
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
@@ -455,7 +455,9 @@ public Component createComponent( IceMediaStream stream,
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 Component createComponent( IceMediaStream stream,
* <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)
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 <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
@@ -503,7 +560,8 @@ public Component createComponent( IceMediaStream stream,
+ transport);
}
Component component = stream.createComponent(keepAliveStrategy);
Component component
= stream.createComponent(keepAliveStrategy, useComponentSocket);
gatherCandidates(component, preferredPort, minPort, maxPort);
@@ -722,7 +722,6 @@ void setConsentFreshness(long consentFreshness)
* <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 DatagramSocket getDatagramSocket()
/**
* 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 Socket getSocket()
/**
* 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()
{
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();
}
}
@@ -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 @@
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 @@
* @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 <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,15 +202,24 @@ protected Component(int componentID,
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 @@ protected void free()
getParentStream().removePairStateChangeListener(this);
keepAlivePairs.clear();
getComponentSocket().close();
socket.close();
if (componentSocket != null)
{
componentSocket.close();
}
if (socket != null)
{
socket.close();
}
}
/**
@@ -986,13 +1021,20 @@ public static Component build(int componentID, IceMediaStream mediaStream)
{
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 ComponentSocket getComponentSocket()
/**
* @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 MultiplexingDatagramSocket getSocket()
/**
* @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()
{
@@ -175,18 +175,26 @@ protected IceMediaStream(Agent parentAgent, String name)
*
* @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);
}
@@ -138,8 +138,12 @@ private void createRelayedCandidate(Response response)
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);
}
@@ -209,7 +209,7 @@ protected HostCandidate getHostCandidate(HostCandidate hostCand)
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 @@ protected HostCandidate getHostCandidate(HostCandidate hostCand)
.getParentAgent()
.getStunStack()
.addSocket(cand.getStunSocket(null));
parentComponent.getComponentSocket().add(multiplexing);
ComponentSocket componentSocket
= parentComponent.getComponentSocket();
if (componentSocket != null)
{
componentSocket.add(multiplexing);
}
}
}
catch (Exception e)
Oops, something went wrong.

0 comments on commit 7d2ad8f

Please sign in to comment.