From 5f554565b16c884be1727b4c214f8b6b1f67cfd9 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Tue, 13 Dec 2016 15:10:31 -0600 Subject: [PATCH] feat: Implements different strategies for selecting the candidate pairs to be kept alive. --- src/main/java/org/ice4j/ice/Agent.java | 21 ++-- src/main/java/org/ice4j/ice/Component.java | 109 ++++++++++++++++++ .../java/org/ice4j/ice/KeepAliveStrategy.java | 73 ++++++++++++ 3 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/ice4j/ice/KeepAliveStrategy.java diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 59be43b..dc732fa 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -2280,21 +2280,24 @@ public class Agent { for(Component component : stream.getComponents()) { - CandidatePair pair = component.getSelectedPair(); - - if(pair != null) + for (CandidatePair pair : component.getKeepAlivePairs()) { - if(performConsentFreshness) + + if (pair != null) { - connCheckClient.startCheckForPair( + if (performConsentFreshness) + { + connCheckClient.startCheckForPair( pair, originalConsentFreshnessWaitInterval, maxConsentFreshnessWaitInterval, consentFreshnessMaxRetransmissions); - } - else - { - connCheckClient.sendBindingIndicationForPair(pair); + } + else + { + connCheckClient + .sendBindingIndicationForPair(pair); + } } } } diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 2c720b9..0e18936 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -17,8 +17,10 @@ */ package org.ice4j.ice; +import java.beans.*; import java.net.*; import java.util.*; +import java.util.concurrent.*; import java.util.logging.*; import org.ice4j.*; @@ -38,6 +40,7 @@ import org.ice4j.util.Logger; //Disambiguation * @author Boris Grozev */ public class Component + implements PropertyChangeListener { /** * Our class logger. @@ -146,6 +149,20 @@ public class Component @Deprecated private final IceSocketWrapper socketWrapper; + /** + * The {@link KeepAliveStrategy} used by this component to select which + * pairs are to be kept alive. + */ + private KeepAliveStrategy keepAliveStrategy + = KeepAliveStrategy.SELECTED_ONLY; + + /** + * The set of pairs which this component wants to keep alive. + */ + private final Set keepAlivePairs + = Collections.newSetFromMap( + new ConcurrentHashMap()); + /** * Creates a new Component with the specified componentID * as a child of the specified IceMediaStream. @@ -174,6 +191,7 @@ public class Component throw new RuntimeException(se); } + mediaStream.addPairChangeListener(this); logger = new Logger(classLogger, agentLogger); } @@ -817,6 +835,8 @@ public class Component } } + getParentStream().removePairStateChangeListener(this); + keepAlivePairs.clear(); getComponentSocket().close(); } @@ -902,6 +922,12 @@ public class Component */ protected void setSelectedPair(CandidatePair pair) { + if (keepAliveStrategy == KeepAliveStrategy.SELECTED_ONLY) + { + keepAlivePairs.clear(); + } + keepAlivePairs.add(pair); + this.selectedPair = pair; } @@ -977,4 +1003,87 @@ public class Component { return socketWrapper; } + + /** + * @return the set of candidate pairs which are to be kept alive. + */ + Set getKeepAlivePairs() + { + return keepAlivePairs; + } + + /** + * {@inheritDoc} + *

+ * Handles events coming from candidate pairs. + */ + @Override + public void propertyChange(PropertyChangeEvent event) + { + String propertyName = event.getPropertyName(); + if (!(event.getSource() instanceof CandidatePair)) + { + return; + } + + CandidatePair pair = (CandidatePair) event.getSource(); + if (!equals(pair.getParentComponent())) + { + // Events are fired by the IceMediaStream, which might have + // multiple components. Make sure that we only handle events for + // this component. + return; + } + + boolean addToKeepAlive = false; + + // We handle this case in setSelectedPair + if (keepAliveStrategy == KeepAliveStrategy.SELECTED_ONLY) + return; + + if (IceMediaStream.PROPERTY_PAIR_STATE_CHANGED.equals(propertyName)) + { + CandidatePairState newState + = (CandidatePairState) event.getNewValue(); + + if (CandidatePairState.SUCCEEDED.equals(newState)) + { + if (keepAliveStrategy == KeepAliveStrategy.ALL_SUCCEEDED) + { + addToKeepAlive = true; + } + else if (keepAliveStrategy == KeepAliveStrategy.SELECTED_AND_TCP) + { + Transport transport + = pair.getLocalCandidate().getTransport(); + addToKeepAlive = transport == Transport.TCP + || transport == Transport.SSLTCP; + + // Pairs with a remote TCP port 9 cannot be checked. + // Instead, the corresponding pair with the peer reflexive + // candidate needs to be checked. However, we observe + // such pairs transitioning into the SUCCEEDED state. + // Ignore them. + addToKeepAlive + &= pair.getRemoteCandidate() + .getTransportAddress().getPort() != 9; + } + } + } + + if (addToKeepAlive && !keepAlivePairs.contains(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"); + } + } diff --git a/src/main/java/org/ice4j/ice/KeepAliveStrategy.java b/src/main/java/org/ice4j/ice/KeepAliveStrategy.java new file mode 100644 index 0000000..caac497 --- /dev/null +++ b/src/main/java/org/ice4j/ice/KeepAliveStrategy.java @@ -0,0 +1,73 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * + * Copyright @ 2015 Atlassian Pty Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.ice4j.ice; + +import java.util.*; + +/** + * An enumeration of strategies for selecting which candidate pairs to + * keep alive. + */ +public enum KeepAliveStrategy +{ + /** + * Only keep alive the selected pair. + */ + SELECTED_ONLY("selected_only"), + + /** + * Keep alive the selected pair and any TCP pairs. + */ + SELECTED_AND_TCP("selected_and_tcp"), + + /** + * Keep alive all succeeded pairs. + */ + ALL_SUCCEEDED("all_succeeded"); + + private String name; + + KeepAliveStrategy(String name) + { + this.name = name; + } + + /** + * @return the {@link KeepAliveStrategy} with name equal to the given + * string, or {@code null} if there is no such strategy. + * @param string the name of the strategy. + */ + public static KeepAliveStrategy fromString(String string) + { + for (KeepAliveStrategy strategy : KeepAliveStrategy.values()) + { + if (strategy.name.equals(string)) + return strategy; + } + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() + { + return name; + } +}