feat: Implements different strategies for
selecting the candidate pairs to be kept alive.
This commit is contained in:
parent
b340fa22f0
commit
5f554565b1
@ -2280,21 +2280,24 @@ public class Agent
|
|||||||
{
|
{
|
||||||
for(Component component : stream.getComponents())
|
for(Component component : stream.getComponents())
|
||||||
{
|
{
|
||||||
CandidatePair pair = component.getSelectedPair();
|
for (CandidatePair pair : component.getKeepAlivePairs())
|
||||||
|
|
||||||
if(pair != null)
|
|
||||||
{
|
{
|
||||||
if(performConsentFreshness)
|
|
||||||
|
if (pair != null)
|
||||||
{
|
{
|
||||||
connCheckClient.startCheckForPair(
|
if (performConsentFreshness)
|
||||||
|
{
|
||||||
|
connCheckClient.startCheckForPair(
|
||||||
pair,
|
pair,
|
||||||
originalConsentFreshnessWaitInterval,
|
originalConsentFreshnessWaitInterval,
|
||||||
maxConsentFreshnessWaitInterval,
|
maxConsentFreshnessWaitInterval,
|
||||||
consentFreshnessMaxRetransmissions);
|
consentFreshnessMaxRetransmissions);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
connCheckClient.sendBindingIndicationForPair(pair);
|
connCheckClient
|
||||||
|
.sendBindingIndicationForPair(pair);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,8 +17,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.ice4j.ice;
|
package org.ice4j.ice;
|
||||||
|
|
||||||
|
import java.beans.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
import org.ice4j.*;
|
import org.ice4j.*;
|
||||||
@ -38,6 +40,7 @@ import org.ice4j.util.Logger; //Disambiguation
|
|||||||
* @author Boris Grozev
|
* @author Boris Grozev
|
||||||
*/
|
*/
|
||||||
public class Component
|
public class Component
|
||||||
|
implements PropertyChangeListener
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Our class logger.
|
* Our class logger.
|
||||||
@ -146,6 +149,20 @@ public class Component
|
|||||||
@Deprecated
|
@Deprecated
|
||||||
private final IceSocketWrapper socketWrapper;
|
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>
|
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
|
||||||
* as a child of the specified <tt>IceMediaStream</tt>.
|
* as a child of the specified <tt>IceMediaStream</tt>.
|
||||||
@ -174,6 +191,7 @@ public class Component
|
|||||||
throw new RuntimeException(se);
|
throw new RuntimeException(se);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mediaStream.addPairChangeListener(this);
|
||||||
logger = new Logger(classLogger, agentLogger);
|
logger = new Logger(classLogger, agentLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -817,6 +835,8 @@ public class Component
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getParentStream().removePairStateChangeListener(this);
|
||||||
|
keepAlivePairs.clear();
|
||||||
getComponentSocket().close();
|
getComponentSocket().close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -902,6 +922,12 @@ public class Component
|
|||||||
*/
|
*/
|
||||||
protected void setSelectedPair(CandidatePair pair)
|
protected void setSelectedPair(CandidatePair pair)
|
||||||
{
|
{
|
||||||
|
if (keepAliveStrategy == KeepAliveStrategy.SELECTED_ONLY)
|
||||||
|
{
|
||||||
|
keepAlivePairs.clear();
|
||||||
|
}
|
||||||
|
keepAlivePairs.add(pair);
|
||||||
|
|
||||||
this.selectedPair = pair;
|
this.selectedPair = pair;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -977,4 +1003,87 @@ public class Component
|
|||||||
{
|
{
|
||||||
return socketWrapper;
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
73
src/main/java/org/ice4j/ice/KeepAliveStrategy.java
Normal file
73
src/main/java/org/ice4j/ice/KeepAliveStrategy.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user