diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 5da7f61..9f09f70 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -448,18 +448,62 @@ public class Agent int preferredPort, int minPort, int maxPort) + throws IllegalArgumentException, + IOException, + BindException + { + return createComponent( + stream, transport, + preferredPort, minPort, maxPort, + KeepAliveStrategy.SELECTED_ONLY); + } + + /** + * 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. + * + * @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) throws IllegalArgumentException, IOException, BindException { - if(transport != Transport.UDP) + if (transport != Transport.UDP) { throw new IllegalArgumentException( "This implementation does not currently support transport: " + transport); } - Component component = stream.createComponent(); + Component component = stream.createComponent(keepAliveStrategy); gatherCandidates(component, preferredPort, minPort, maxPort); diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 1587ed3..fc96851 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -153,8 +153,7 @@ public class Component * The {@link KeepAliveStrategy} used by this component to select which * pairs are to be kept alive. */ - private KeepAliveStrategy keepAliveStrategy - = KeepAliveStrategy.SELECTED_ONLY; + private final KeepAliveStrategy keepAliveStrategy; /** * The set of pairs which this component wants to keep alive. @@ -172,11 +171,14 @@ public class Component * parent of this component. */ protected Component(int componentID, - IceMediaStream mediaStream) + IceMediaStream mediaStream, + KeepAliveStrategy keepAliveStrategy) { // the max value for componentID is 256 this.componentID = componentID; this.parentStream = mediaStream; + this.keepAliveStrategy + = Objects.requireNonNull(keepAliveStrategy, "keepAliveStrategy"); Logger agentLogger = mediaStream.getParentAgent().getLogger(); @@ -981,7 +983,9 @@ public class Component */ 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); } } - - /** - * Sets the {@link KeepAliveStrategy} for this {@link Component}. - * @param strategy the strategy to set. - */ - public void setKeepAliveStrategy(KeepAliveStrategy strategy) - { - keepAliveStrategy = Objects.requireNonNull(strategy, "strategy"); - } - } diff --git a/src/main/java/org/ice4j/ice/IceMediaStream.java b/src/main/java/org/ice4j/ice/IceMediaStream.java index b7039ab..564f180 100644 --- a/src/main/java/org/ice4j/ice/IceMediaStream.java +++ b/src/main/java/org/ice4j/ice/IceMediaStream.java @@ -173,16 +173,20 @@ public class IceMediaStream * when creating the component so make sure you keep that in mind in case * 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 Component after adding it to * the stream first. */ - protected Component createComponent() + protected Component createComponent(KeepAliveStrategy keepAliveStrategy) { Component component; synchronized (components) { - component = new Component(++lastComponentID, this); + component + = new Component(++lastComponentID, this, keepAliveStrategy); components.put(component.getComponentID(), component); }