diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java
index 3cc5a46..3304831 100644
--- a/src/org/ice4j/ice/Agent.java
+++ b/src/org/ice4j/ice/Agent.java
@@ -15,10 +15,11 @@ import java.util.*;
import java.util.logging.*;
import org.ice4j.*;
+import org.ice4j.ice.checks.*;
import org.ice4j.ice.harvest.*;
/**
- * An IceAgent could be described as the main class (i.e. the chef d'orchestre)
+ * An Agent could be described as the main class (i.e. the chef d'orchestre)
* of an ICE implementation.
*
* As defined in RFC 3264, an agent is the protocol implementation involved in
@@ -27,9 +28,9 @@ import org.ice4j.ice.harvest.*;
*
*
* @author Emil Ivov
- * @author Namal Senarathne
*/
public class Agent
+ implements RemoteAddressHandler
{
/**
* Our class logger.
@@ -68,7 +69,29 @@ public class Agent
* we assign within a session (i.e. the entire life time of an
* Agent)
*/
- private FoundationsRegistry foundationsRegistry = new FoundationsRegistry();
+ private final FoundationsRegistry foundationsRegistry
+ = new FoundationsRegistry();
+
+ /**
+ * The triggeredCheckQueue is a FIFO queue containing candidate
+ * pairs for which checks are to be sent at the next available opportunity.
+ * A pair would get into a triggered check queue as soon as we receive
+ * a check on its local candidate.
+ */
+ private final List triggeredCheckQueue
+ = new Vector();
+
+ /**
+ * The List of remote addresses that we have discovered through
+ * incoming connectivity checks, before actually receiving a session
+ * description from the peer and that may potentially contain peer reflexive
+ * addresses. This list is stored and only if and while connectivity checks
+ * are not running. Once they start, we are able to determine whether the
+ * addresses in here are actually peer-reflexive or not, and schedule
+ * the necessary triggered checks.
+ */
+ private final List preDiscoveredRemoteCandidatesQueue
+ = new Vector();
/**
* The user fragment that we should use for the ice-ufrag attribute.
@@ -105,14 +128,25 @@ public class Agent
/**
* The entity that will be taking care of outgoing connectivity checks.
*/
- private final ConnectivityCheckClient connCheckHandler
+ private final ConnectivityCheckClient connCheckClient
= new ConnectivityCheckClient(this);
/**
* The entity that will be taking care of incoming connectivity checks.
*/
private final ConnectivityCheckServer connCheckServer
- = new ConnectivityCheckServer(this);
+ = new ConnectivityCheckServer(this, this);
+
+ /**
+ * Indicates whether this agent has both sent and received candidate lists
+ * and started connectivity establishment. This is important in cases like
+ * determining whether a remote address we've just discovered is peer
+ * reflexive or not. If iceStarted is true and we don't know about the
+ * address then we should add it to the list of candidates. Otherwise
+ * we should wait for the remote party to send their media description
+ * before being able to determine.
+ */
+ private boolean iceStarted = false;
/**
* Creates an empty Agent with no streams, and no address
@@ -253,20 +287,53 @@ public class Agent
/**
* Initializes all stream check lists and begins the checks.
*/
- public void startChecks()
+ public void startConnectivityEstablishment()
{
- initCheckLists();
-
- List streams = getStreams();
-
- for(IceMediaStream stream : streams)
+ synchronized(this)
{
- CheckList list = stream.getCheckList();
+ initCheckLists();
- connCheckHandler.startChecks(list);
+ List streams = getStreams();
+
+ for(IceMediaStream stream : streams)
+ {
+ CheckList list = stream.getCheckList();
+
+ connCheckClient.startChecks(list);
+ }
+
+ iceStarted = true;
}
}
+ /**
+ * Indicates whether this {@link Agent} is currently in the process of
+ * running connectivity checks and establishing connectivity. Connectivity
+ * establishment is considered to have started after both {@link Agent}s
+ * have exchanged their media descriptions. Determining whether the actual
+ * process has started is important, for example, when determining whether
+ * a remote address we've just discovered is peer reflexive or not.
+ * If CE has started and we don't know about the address then we should
+ * add it to the list of candidates. Otherwise we should hold to it until
+ * it does and check later.
+ *
+ * Note that an {@link Agent} would be ready to and will send responses to
+ * connectivity checks as soon as it streams get created, which is well
+ * before we actually start the checks.
+ *
+ * @return true after media descriptions have been exchanged both
+ * ways and connectivity checks have started (regardless of their current
+ * state) and false otherwise.
+ */
+ public boolean isStarted()
+ {
+ synchronized(this)
+ {
+ return iceStarted;
+ }
+
+ }
+
/**
* Creates, initializes and orders the list of candidate pairs that would
* be used for the connectivity checks for all components in this stream.
@@ -582,4 +649,72 @@ public class Agent
{
return isControlling;
}
+
+ /**
+ * Returns the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this {@link Agent}'s
+ * streams or null if it doesn't.
+ *
+ * @param localAddress the {@link TransportAddress} we are looking for.
+ *
+ * @return the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this {@link Agent}'s
+ * streams or null if it doesn't.
+ */
+ public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ {
+ Collection streamsCollection = mediaStreams.values();
+
+ for( IceMediaStream cmp : streamsCollection)
+ {
+ LocalCandidate cnd = cmp.findLocalCandidate(localAddress);
+
+ if(cnd != null)
+ return cnd;
+ }
+
+ return null;
+ }
+
+ /**
+ * Notifies the implementation that the {@link ConnectivityCheckServer} has
+ * just received a message on localAddress originating at
+ * remoteAddress carrying the specified priority. This
+ * will cause us to schedule a triggered check for the corresponding
+ * remote candidate and potentially to the discovery of a PEER-REFLEXIVE
+ * candidate.
+ *
+ * @param remoteAddress the address that we've just seen, and that is
+ * potentially a peer-reflexive address.
+ * @param localAddress the address that we were contacted on.
+ * @param priority the priority that the remote party assigned to
+ * @param remoteUFrag the user fragment that we should be using when and if
+ * we decide to send a check to remoteAddress.
+ *
+ */
+ public void handleRemoteAddress(TransportAddress remoteAddress,
+ TransportAddress localAddress,
+ long priority,
+ String remoteUFrag)
+ {
+ this.createMediaStream(null).createComponent(null).
+
+ if(iceStarted)
+ {
+ /*
+ if remoteAddress is not known
+ {
+ create peer reflexive candidate
+ }
+
+ schedule triggered check
+ */
+ }
+ else
+ {
+ RemoteCandidate remoteCand = new RemoteCandidate(
+ remoteAddres, );
+ }
+
+ }
}
diff --git a/src/org/ice4j/ice/CheckList.java b/src/org/ice4j/ice/CheckList.java
index 0337e53..2d178f7 100644
--- a/src/org/ice4j/ice/CheckList.java
+++ b/src/org/ice4j/ice/CheckList.java
@@ -13,11 +13,13 @@ import java.util.*;
* CheckListState). The pairs in a check list are those that an ICE
* agent will run STUN connectivity checks for. There is one check list per
* in-use media stream resulting from the offer/answer exchange.
- *
+ *
+ * Given the asynchronous nature of ice, a check list may be accessed from
+ * different locations. This class therefore stores pairs in a Vector
* @author Emil Ivov
*/
public class CheckList
- extends LinkedList
+ extends Vector
{
/**
* A dummy serialization id.
diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java
index 9265491..51a493d 100644
--- a/src/org/ice4j/ice/Component.java
+++ b/src/org/ice4j/ice/Component.java
@@ -576,4 +576,27 @@ public class Component
}
}
}
+
+ /**
+ * Returns the local LocalCandidate with the specified
+ * localAddress if it belongs to this component or null
+ * if it doesn't.
+ *
+ * @param localAddress the {@link TransportAddress} we are looking for.
+ *
+ * @return the local LocalCandidate with the specified
+ * localAddress if it belongs to this component or null
+ * if it doesn't.
+ */
+ public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ {
+
+ for( LocalCandidate localCnd : localCandidates)
+ {
+ if(localCnd.getTransportAddress().equals(localAddress))
+ return localCnd;
+ }
+
+ return null;
+ }
}
diff --git a/src/org/ice4j/ice/ConnectivityCheckClient.java b/src/org/ice4j/ice/ConnectivityCheckClient.java
deleted file mode 100644
index 090fad7..0000000
--- a/src/org/ice4j/ice/ConnectivityCheckClient.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
- * Maintained by the SIP Communicator community (http://sip-communicator.org).
- *
- * Distributable under LGPL license. See terms of license at gnu.org.
- */
-package org.ice4j.ice;
-
-import java.net.*;
-import java.util.logging.*;
-
-import org.ice4j.*;
-import org.ice4j.attribute.*;
-import org.ice4j.message.*;
-import org.ice4j.stack.*;
-
-/**
- * @author Emil Ivov
- */
-public class ConnectivityCheckClient
- implements ResponseCollector
-{
- /**
- * The Logger used by the ConnectivityCheckClient
- * class and its instances for logging output.
- */
- private static final Logger logger = Logger
- .getLogger(ConnectivityCheckClient.class.getName());
-
- /**
- * The agent that created us.
- */
- private final Agent parentAgent;
-
- /**
- * The stun stack that we will use for connectivity checks.
- */
- StunStack stunStack = StunStack.getInstance();
-
- /**
- * Creates a new ConnectivityCheckHandler setting
- * parentAgent as the agent that will be used for retrieving
- * information such as user fragments for example.
- *
- * @param parentAgent the Agent that is creating this instance.
- */
- public ConnectivityCheckClient(Agent parentAgent)
- {
- this.parentAgent = parentAgent;
- }
-
- public void startChecks(CheckList checkList)
- {
- for(CandidatePair pair : checkList)
- {
- startCheckForPair(pair);
- }
- }
-
-
- private void startCheckForPair(CandidatePair pair)
- {
- //we don't need to do a canReach() verification here as it has been
- //already verified during the gathering process.
- DatagramSocket stunSocket = ((HostCandidate)pair.getLocalCandidate())
- .getStunSocket(null);
-
- Request request = MessageFactory.createBindingRequest();
-
- //priority
- PriorityAttribute priority = AttributeFactory.createPriorityAttribute(
- pair.getLocalCandidate().computePriorityForType(
- CandidateType.PEER_REFLEXIVE_CANDIDATE));
-
- request.addAttribute(priority);
-
- //controlling controlled
- if (parentAgent.isControlling())
- {
- request.addAttribute(AttributeFactory
- .createIceControllingAttribute(parentAgent
- .getTieBreaker()));
- }
- else
- {
- request.addAttribute(AttributeFactory
- .createIceControlledAttribute(parentAgent
- .getTieBreaker()));
- }
-
- //credentials
- String localUserName = parentAgent.generateLocalUserName();
- UsernameAttribute unameAttr = AttributeFactory.createUsernameAttribute(
- localUserName);
-
- request.addAttribute(unameAttr);
-
- //todo: do this in the stun stack so that we could do the
- //calculation once the request is ready (we'd need the transaction id
- //for example.
- //todo: also implement SASL prepare
- MessageIntegrityAttribute msgIntegrity = AttributeFactory
- .createMessageIntegrityAttribute(localUserName);
-
-
- request.addAttribute(msgIntegrity);
-
- TransactionID tran;
-
- try
- {
- tran = stunStack.sendRequest(request,
- pair.getRemoteCandidate().getTransportAddress(), stunSocket,
- this);
-System.out.println("checking pair " + pair + " with tran=" + tran.toString());
- }
- catch (Exception exception)
- {
- logger.log( Level.INFO,
- "Failed to send " + request + " through "
- + stunSocket.getLocalSocketAddress(),
- exception);
- return;
- }
- }
-
- public void processResponse(StunMessageEvent response)
- {
- }
-
- public void processTimeout(StunTimeoutEvent event)
- {
- System.out.println("timeout event=" + event);
- }
-
- public void processUnreachable(StunFailureEvent event)
- {
- System.out.println("failure event=" + event);
- }
-
-
-}
diff --git a/src/org/ice4j/ice/ConnectivityCheckServer.java b/src/org/ice4j/ice/ConnectivityCheckServer.java
deleted file mode 100644
index f192c47..0000000
--- a/src/org/ice4j/ice/ConnectivityCheckServer.java
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
- * Maintained by the SIP Communicator community (http://sip-communicator.org).
- *
- * Distributable under LGPL license. See terms of license at gnu.org.
- */
-package org.ice4j.ice;
-
-import java.util.logging.*;
-
-import org.ice4j.*;
-import org.ice4j.attribute.*;
-import org.ice4j.message.*;
-import org.ice4j.security.*;
-import org.ice4j.stack.*;
-/**
- * @author Emil Ivov
- */
-public class ConnectivityCheckServer
- implements RequestListener,
- CredentialsAuthority
-{
- /**
- * The Logger used by the ConnectivityCheckServer
- * class and its instances for logging output.
- */
- private static final Logger logger = Logger
- .getLogger(ConnectivityCheckServer.class.getName());
-
- /**
- * The agent that created us.
- */
- private final Agent parentAgent;
-
- /**
- * The stun stack that we will use for connectivity checks.
- */
- StunStack stunStack = StunStack.getInstance();
-
- /**
- * Creates a new ConnectivityCheckServer setting
- * parentAgent as the agent that will be used for retrieving
- * information such as user fragments for example.
- *
- * @param parentAgent the Agent that is creating this instance.
- */
- public ConnectivityCheckServer(Agent parentAgent)
- {
- this.parentAgent = parentAgent;
- stunStack.addRequestListener(this);
- stunStack.getCredentialsManager().registerAuthority(this);
- }
-
- public void requestReceived(StunMessageEvent evt)
- {
- Request request = (Request)evt.getMessage();
-
- //ignore incoming requests that are not meant for the local user.
- //normally the stack will get rid of faulty user names but we could
- //still see messages not meant for this server if both peers or running
- //on this same instance of the stack.
- UsernameAttribute uname = (UsernameAttribute)request
- .getAttribute(Attribute.USERNAME);
-
- if(uname == null
- || !checkLocalUserName(new String(uname.getUsername())))
- {
- return;
- }
-
- //check message integrity.
- Response response = MessageFactory.createBindingResponse(
- request, evt.getRemoteAddress());
- try
- {
- stunStack.sendResponse(evt.getTransactionID().getTransactionID(),
- response, evt.getLocalAddress(), evt.getRemoteAddress());
- }
- catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- /**
- * Verifies whether username is currently known to this server
- * and returns true if so. Returns false otherwise.
- *
- * @param username the user name whose validity we'd like to check.
- *
- * @return true if username is known to this
- * ConnectivityCheckServer and false otherwise.
- */
- public boolean checkLocalUserName(String username)
- {
- boolean accept = false;
- int colon = username.indexOf(":");
- if( colon < 0)
- {
- //caller gave us a ufrag
- if(username.equals(parentAgent.getLocalUfrag()))
- accept = true;
- }
- else
- {
- //caller gave us the entire username.
- if(username.substring(0, colon).equals(parentAgent.getLocalUfrag()))
- accept = true;
- }
- return accept;
- }
-
- /**
- * Implements the {@link CredentialsAuthority#getLocalKey(String)} method in a
- * way that would return this handler's parent agent password if
- * username is either the local ufrag or the username that the
- * agent's remote peer was expected to use.
- *
- * @param username the local ufrag that we should return a password for.
- *
- * @return this handler's parent agent local password if username
- * equals the local ufrag and null otherwise.
- */
- public byte[] getLocalKey(String username)
- {
- //support both the case where username is the local fragment or the
- //entire user name.
- int colon = username.indexOf(":");
- if( colon < 0)
- {
- //caller gave us a ufrag
- if (username.equals(parentAgent.getLocalUfrag()))
- return parentAgent.getLocalPassword().getBytes();
- }
- else
- {
- //caller gave us the entire username.
- if(username.substring(0, colon).equals(parentAgent.getLocalUfrag()))
- return parentAgent.getLocalPassword().getBytes();
- }
-
- return null;
- }
-
- /**
- * Implements the {@link CredentialsAuthority#getRemoteKey(String)} method
- * in a way that would return this handler's parent agent remote password if
- * username is either the remote ufrag or the username that we
- * are expected to use when querying the remote peer.
- *
- * @param username the remote ufrag that we should return a password for.
- *
- * @return this handler's parent agent remote password if username
- * equals the remote ufrag and null otherwise.
- */
- public byte[] getRemoteKey(String username)
- {
- //support both the case where username is the local fragment or the
- //entire user name.
- int colon = username.indexOf(":");
- if( colon < 0)
- {
- //caller gave us a ufrag
- if (username.equals(parentAgent.getRemoteUfrag()))
- return parentAgent.getRemotePassword().getBytes();
- }
- else
- {
- //caller gave us the entire username.
- if (username.equals(parentAgent.generateLocalUserName()))
- return parentAgent.getRemotePassword().getBytes();
- }
-
- return null;
- }
-}
diff --git a/src/org/ice4j/ice/IceMediaStream.java b/src/org/ice4j/ice/IceMediaStream.java
index 491f614..0d447f2 100644
--- a/src/org/ice4j/ice/IceMediaStream.java
+++ b/src/org/ice4j/ice/IceMediaStream.java
@@ -467,4 +467,30 @@ public class IceMediaStream
{
this.maxCheckListSize = nSize;
}
+
+ /**
+ * Returns the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this stream's components
+ * or null otherwise.
+ *
+ * @param localAddress the {@link TransportAddress} we are looking for.
+ *
+ * @return the local LocalCandidate with the specified
+ * localAddress if it belongs to any of this stream's components
+ * or null otherwise.
+ */
+ public LocalCandidate findLocalCandidate(TransportAddress localAddress)
+ {
+ Collection cmpCol = components.values();
+
+ for( Component cmp : cmpCol)
+ {
+ LocalCandidate cnd = cmp.findLocalCandidate(localAddress);
+
+ if(cnd != null)
+ return cnd;
+ }
+
+ return null;
+ }
}
diff --git a/src/org/ice4j/ice/checks/RemoteAddressHandler.java b/src/org/ice4j/ice/checks/RemoteAddressHandler.java
index b394d23..8226481 100644
--- a/src/org/ice4j/ice/checks/RemoteAddressHandler.java
+++ b/src/org/ice4j/ice/checks/RemoteAddressHandler.java
@@ -22,7 +22,10 @@ public interface RemoteAddressHandler
/**
* Notifies the implementation that the {@link ConnectivityCheckServer} has
* just received a message on localAddress originating at
- * remoteAddress carrying the specified priority.
+ * remoteAddress carrying the specified priority. This
+ * will cause us to schedule a triggered check for the corresponding
+ * remote candidate and potentially to the discovery of a PEER-REFLEXIVE
+ * candidate.
*
* @param remoteAddress the address that we've just seen, and that is
* potentially a peer-reflexive address.