feat: Implements different strategies for

selecting the candidate pairs to be kept alive.
This commit is contained in:
Boris Grozev 2016-12-13 15:10:31 -06:00
parent b340fa22f0
commit 5f554565b1
3 changed files with 194 additions and 9 deletions

View File

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

View File

@ -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<CandidatePair> keepAlivePairs
= Collections.newSetFromMap(
new ConcurrentHashMap<CandidatePair, Boolean>());
/**
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
* as a child of the specified <tt>IceMediaStream</tt>.
@ -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<CandidatePair> getKeepAlivePairs()
{
return keepAlivePairs;
}
/**
* {@inheritDoc}
* </p>
* 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");
}
}

View File

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