diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index 2b8a0c4..45c9303 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -19,6 +19,8 @@ import org.ice4j.ice.checks.*; import org.ice4j.ice.harvest.*; import org.ice4j.stack.*; +import sun.management.resources.*; + /** * An Agent could be described as the main class (i.e. the chef d'orchestre) * of an ICE implementation. @@ -67,6 +69,12 @@ public class Agent private final FoundationsRegistry foundationsRegistry = new FoundationsRegistry(); + /** + * the value of Ta as specified by the application or -1 + * if non was specified and we should calculate one ourselves. + */ + private long taValue = -1; + /** * The List of remote addresses that we have discovered through * incoming connectivity checks, before actually receiving a session @@ -829,8 +837,11 @@ public class Agent * The pair is inserted into the check list based on its priority * Its state is set to Waiting [and it] is enqueued into the * triggered check queue. - * Emil: This means we actually don't have anything to do here. */ + IceMediaStream parentStream = triggeredPair.getLocalCandidate() + .getParentComponent().getParentStream(); + + parentStream.addToCheckList(triggeredPair); } /** @@ -841,6 +852,141 @@ public class Agent * Emil: This actually applies for all case. */ connCheckClient.scheduleTriggeredCheck(triggeredPair); + } + /** + * Returns the number of host {@link Candidate}s in this {@link Agent}. + * + * @return the number of host {@link Candidate}s in this {@link Agent}. + */ + protected int countHostCandidates() + { + int num = 0; + + synchronized (mediaStreams) + { + Collection streamsCol = mediaStreams.values(); + + for( IceMediaStream stream : streamsCol) + { + num += stream.countHostCandidates(); + } + } + + return num; + } + + /** + * Lets the application specify a custom value for the Ta timer + * so that we don't calculate one. + * + * @param taValue the value of the Ta timer that the application + * would like us to use rather than calculate one. + */ + public void setTa(long taValue) + { + this.taValue = taValue; + } + + /** + * Calculates the value of the Ta pace timer according to the + * number and type of {@link IceMediaStream}s this agent will be using. + *

+ * During the gathering phase of ICE (Section 4.1.1) and while ICE is + * performing connectivity checks (Section 7), an agent sends STUN and + * TURN transactions. These transactions are paced at a rate of one + * every Ta milliseconds. + *

+ * As per RFC 5245, the value of Ta should be configurable so if + * someone has set a value of their own, we return that value rather than + * calculating a new one. + * + * @return the value of the Ta pace timer according to the + * number and type of {@link IceMediaStream}s this agent will be using or + * a pre-configured value if the application has set one. + *

+ */ + public long calculateTa() + { + //if application specified a value - use it. other wise return .... + // eeeer ... a "dynamically" calculated one ;) + if (taValue != -1) + return taValue; + + /* RFC 5245 says that Ta is: + * + * Ta_i = (stun_packet_size / rtp_packet_size) * rtp_ptime + * + * 1 + * Ta = MAX (20ms, ------------------- ) + * k + * ---- + * \ 1 + * > ------ + * / Ta_i + * ---- + * i=1 + * + * In this implementation we assume equal values of + * stun_packet_size and rtp_packet_size. rtp_ptime is also assumed to be + * 20ms. One day we should probably let the application modify them. + * Until then however the above formula would always be equal to. + * 1 + * Ta = MAX (20ms, ------- ) + * k + * --- + * 20 + * which gives us Ta = MAX (20ms, 20/k) which is always 20. + */ + return 20; + } + + /** + * Calculates the value of the retransmission timer to use in STUN + * transactions, while harvesting addresses (not to confuse with the RTO + * for the STUN transactions used in connectivity checks). + * + * @return the value of the retransmission timer to use in STUN + * transactions, while harvesting addresses. + */ + public long calculateStunHarvestRTO() + { + /* RFC 5245 says: + * RTO = MAX (100ms, Ta * (number of pairs)) + * where the number of pairs refers to the number of pairs of candidates + * with STUN or TURN servers. + * + * Go figure what "pairs of candidates with STUN or TURN servers" means. + * Let's assume they meant the number stun transactions we'll start + * while harvesting. + */ + + return Math.max(100, calculateTa() * 2 * countHostCandidates()); + } + + /** + * Calculates the value of the retransmission timer to use in STUN + * transactions, used in connectivity checks (not to confused with the RTO + * for the STUN address harvesting). + * + * @return the value of the retransmission timer to use in STUN connectivity + * check transactions.. + */ + public long calculateStunConnCheckRTO() + { + /* RFC 5245 says: + * For connectivity checks, RTO SHOULD be configurable and SHOULD have + * a default of: + * + * RTO = MAX (100ms, Ta*N * (Num-Waiting + Num-In-Progress)) + * + * where Num-Waiting is the number of checks in the check list in the + * Waiting state, Num-In-Progress is the number of checks in the + * In-Progress state, and N is the number of checks to be performed. + * + * Emil: I am not sure I like the formula so we'll simply be returning + * 100 for the time being. + */ + return 100; } } diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index cc9c5d2..e75af0c 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -622,4 +622,24 @@ public class Component return null; } + + /** + * Returns the number of host {@link Candidate}s in this Component. + * + * @return the number of host {@link Candidate}s in this Component. + */ + protected int coundHostCandidates() + { + int num = 0; + synchronized (localCandidates) + { + for(LocalCandidate cand : localCandidates) + { + if (cand.getType() == CandidateType.HOST_CANDIDATE) + num++; + } + } + + return num; + } } diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java index fbdfd99..562f735 100644 --- a/src/org/ice4j/ice/IceMediaStream.java +++ b/src/org/ice4j/ice/IceMediaStream.java @@ -556,4 +556,41 @@ public class IceMediaStream return null; } + + /** + * Returns the number of host {@link Candidate}s in this stream. + * + * @return the number of host {@link Candidate}s in this stream. + */ + protected int countHostCandidates() + { + int num = 0; + + synchronized (components) + { + Collection cmpCol = components.values(); + + for( Component cmp : cmpCol) + { + num += cmp.coundHostCandidates(); + } + } + + return num; + } + + /** + * Adds candidatePair to this stream's check list. The method + * is meant for use during the connectivity checks phase when new pairs + * with remote PEER-REFLEXIVE-CANDIDATEs are discovered. + * + * @param candidatePair the pair that we'd like to add to this streams. + */ + protected void addToCheckList(CandidatePair candidatePair) + { + synchronized(checkList) + { + this.checkList.add(candidatePair); + } + } } diff --git a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java index 042230a..e19aa8c 100644 --- a/src/org/ice4j/ice/checks/ConnectivityCheckClient.java +++ b/src/org/ice4j/ice/checks/ConnectivityCheckClient.java @@ -177,6 +177,7 @@ public class ConnectivityCheckClient */ private static class PaceMaker extends Thread { + public boolean isRunning = true; /** * The {@link ConnectivityCheckClient} that created us. */ @@ -195,9 +196,24 @@ public class ConnectivityCheckClient this.parentClient = parentClient; } - public void run() + /** + * Sends connectivity checks at the pace determined by the {@link + * Agent#calculateTa()} method and using either the trigger check queue + * or the regular check lists. + */ + public synchronized void run() { - + while(isRunning) + { + try + { + wait(parentClient.parentAgent.calculateTa()); + } + catch (InterruptedException e) + { + logger.log(Level.FINER, "PaceMake got interrupted", e); + } + } } } }