fix: Makes the keep alive strategy final.

This commit is contained in:
Boris Grozev 2016-12-15 11:15:31 -06:00
parent 19e86e3bc9
commit 0d769bb46d
3 changed files with 60 additions and 18 deletions

View File

@ -448,18 +448,62 @@ public class Agent
int preferredPort, int preferredPort,
int minPort, int minPort,
int maxPort) int maxPort)
throws IllegalArgumentException,
IOException,
BindException
{
return createComponent(
stream, transport,
preferredPort, minPort, maxPort,
KeepAliveStrategy.SELECTED_ONLY);
}
/**
* 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.
*
* @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)
throws IllegalArgumentException, throws IllegalArgumentException,
IOException, IOException,
BindException BindException
{ {
if(transport != Transport.UDP) if (transport != Transport.UDP)
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"This implementation does not currently support transport: " "This implementation does not currently support transport: "
+ transport); + transport);
} }
Component component = stream.createComponent(); Component component = stream.createComponent(keepAliveStrategy);
gatherCandidates(component, preferredPort, minPort, maxPort); gatherCandidates(component, preferredPort, minPort, maxPort);

View File

@ -153,8 +153,7 @@ public class Component
* The {@link KeepAliveStrategy} used by this component to select which * The {@link KeepAliveStrategy} used by this component to select which
* pairs are to be kept alive. * pairs are to be kept alive.
*/ */
private KeepAliveStrategy keepAliveStrategy private final KeepAliveStrategy keepAliveStrategy;
= KeepAliveStrategy.SELECTED_ONLY;
/** /**
* The set of pairs which this component wants to keep alive. * The set of pairs which this component wants to keep alive.
@ -172,11 +171,14 @@ public class Component
* parent of this component. * parent of this component.
*/ */
protected Component(int componentID, protected Component(int componentID,
IceMediaStream mediaStream) IceMediaStream mediaStream,
KeepAliveStrategy keepAliveStrategy)
{ {
// the max value for componentID is 256 // the max value for componentID is 256
this.componentID = componentID; this.componentID = componentID;
this.parentStream = mediaStream; this.parentStream = mediaStream;
this.keepAliveStrategy
= Objects.requireNonNull(keepAliveStrategy, "keepAliveStrategy");
Logger agentLogger = mediaStream.getParentAgent().getLogger(); Logger agentLogger = mediaStream.getParentAgent().getLogger();
@ -981,7 +983,9 @@ public class Component
*/ */
public static Component build(int componentID, IceMediaStream mediaStream) public static Component build(int componentID, IceMediaStream mediaStream)
{ {
return new Component(componentID, mediaStream); return
new Component(
componentID, mediaStream, KeepAliveStrategy.SELECTED_ONLY);
} }
/** /**
@ -1084,14 +1088,4 @@ public class Component
keepAlivePairs.add(pair); keepAlivePairs.add(pair);
} }
} }
/**
* Sets the {@link KeepAliveStrategy} for this {@link Component}.
* @param strategy the strategy to set.
*/
public void setKeepAliveStrategy(KeepAliveStrategy strategy)
{
keepAliveStrategy = Objects.requireNonNull(strategy, "strategy");
}
} }

View File

@ -173,16 +173,20 @@ public class IceMediaStream
* when creating the component so make sure you keep that in mind in case * when creating the component so make sure you keep that in mind in case
* assigning a specific component ID is important to you. * assigning a specific component ID is important to you.
* *
* @param keepAliveStrategy the keep-alive strategy, which dictates which
* candidates pairs are going to be kept alive.
*
* @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() protected Component createComponent(KeepAliveStrategy keepAliveStrategy)
{ {
Component component; Component component;
synchronized (components) synchronized (components)
{ {
component = new Component(++lastComponentID, this); component
= new Component(++lastComponentID, this, keepAliveStrategy);
components.put(component.getComponentID(), component); components.put(component.getComponentID(), component);
} }