From c0e7cd56d6e7f089c55fcc4849a2a9d6e10a978e Mon Sep 17 00:00:00 2001 From: boris Date: Fri, 29 Aug 2014 08:27:20 +0000 Subject: [PATCH] Adds methods to access the sockets of a CandidatePair. --- src/org/ice4j/ice/CandidatePair.java | 85 ++++++++++++++++++++++--- src/org/ice4j/ice/Component.java | 5 ++ src/org/ice4j/ice/HostCandidate.java | 21 +++++- src/org/ice4j/ice/LocalCandidate.java | 26 ++++++-- src/org/ice4j/ice/TcpHostCandidate.java | 65 +++++++++++++++++++ 5 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 src/org/ice4j/ice/TcpHostCandidate.java diff --git a/src/org/ice4j/ice/CandidatePair.java b/src/org/ice4j/ice/CandidatePair.java index 50d8752..27970e3 100644 --- a/src/org/ice4j/ice/CandidatePair.java +++ b/src/org/ice4j/ice/CandidatePair.java @@ -6,8 +6,10 @@ */ package org.ice4j.ice; +import java.net.*; import java.util.*; +import org.ice4j.socket.*; import org.ice4j.stack.*; /** @@ -21,6 +23,7 @@ import org.ice4j.stack.*; * * @author Emil Ivov * @author Lyubomir Marinov + * @author Boris Grozev */ public class CandidatePair implements Comparable @@ -38,6 +41,12 @@ public class CandidatePair */ private static final long MATH_POW_2_32 = 1L << 32; + /** + * A Comparator using the compareTo method of the + * CandidatePair. + */ + public static final PairComparator comparator = new PairComparator(); + /** * The local candidate of this pair. */ @@ -68,7 +77,7 @@ public class CandidatePair /** * Indicates whether this CandidatePair is on any of this agent's - * valid pair lists. + * valid pair lists. */ private boolean isValid = false; @@ -78,12 +87,6 @@ public class CandidatePair */ private boolean isNominated = false; - /** - * A Comparator using the compareTo method of the - * CandidatePair. - */ - public static final PairComparator comparator = new PairComparator(); - /** * Each candidate pair has a state that is assigned once the check list * for each media stream has been computed. The ICE RFC defines five @@ -607,9 +610,9 @@ public class CandidatePair } /** - * Returns the value of this pair's nominated flag to true. If a - * valid candidate pair has its nominated flag set, it means that it may be - * selected by ICE for sending and receiving media. + * Returns the value of this pair's nominated flag. If a valid candidate + * pair has its nominated flag set, it means that it may be selected by ICE + * for sending and receiving media. * * @return true if this pair has already been nominated for * selection and false otherwise. @@ -689,4 +692,66 @@ public class CandidatePair newValue); } } + + /** + * Returns the UDP DatagramSocket (if any) for this + * CandidatePair. + * @return the UDP DatagramSocket (if any) for this + * CandidatePair. + */ + public DatagramSocket getDatagramSocket() + { + IceSocketWrapper wrapper = getIceSocketWrapper(); + return wrapper == null ? null : wrapper.getUDPSocket(); + } + + /** + * Returns the TCP Socket (if any) for this CandidatePair. + * @return the TCP Socket (if any) for this CandidatePair. + */ + public Socket getSocket() + { + IceSocketWrapper wrapper = getIceSocketWrapper(); + return wrapper == null ? null : wrapper.getTCPSocket(); + } + + /** + * Returns the IceSocketWrapper for this CandidatePair. + * @return the IceSocketWrapper for this CandidatePair. + */ + public IceSocketWrapper getIceSocketWrapper() + { + LocalCandidate localCandidate = getLocalCandidate(); + if (localCandidate == null) + { + return null; + } + else if (localCandidate instanceof TcpHostCandidate) + { + /* + * TcpHostCandidates can have multiple sockets, and the one + * to be used by this specific CandidatePair has to have the + * same remote socket address as the pair's remote candidate. + */ + RemoteCandidate remoteCandidate = getRemoteCandidate(); + if (remoteCandidate == null) + return null; + + SocketAddress remoteSocketAddress + = remoteCandidate.getTransportAddress(); + for (IceSocketWrapper socket + : ((TcpHostCandidate) localCandidate).getIceSocketWrappers()) + { + if (socket.getTCPSocket() + .getRemoteSocketAddress().equals(remoteSocketAddress)) + return socket; + } + } + else + { + return localCandidate.getIceSocketWrapper(); + } + + return null; + } } diff --git a/src/org/ice4j/ice/Component.java b/src/org/ice4j/ice/Component.java index 36d3a7e..0d2d665 100644 --- a/src/org/ice4j/ice/Component.java +++ b/src/org/ice4j/ice/Component.java @@ -361,6 +361,10 @@ public class Component { if(localCnd.canReach(remoteCnd)) { + // A single LocalCandidate might be/become connected + // to more more than one remote address, and that's ok + // (that is, we need to form pairs with them all). + /* if(localCnd.getTransport() == Transport.TCP && localCnd.getIceSocketWrapper().getTCPSocket(). isConnected()) @@ -372,6 +376,7 @@ public class Component continue; } } + */ CandidatePair pair = new CandidatePair(localCnd, remoteCnd); diff --git a/src/org/ice4j/ice/HostCandidate.java b/src/org/ice4j/ice/HostCandidate.java index 4954ff3..1c4bd0e 100644 --- a/src/org/ice4j/ice/HostCandidate.java +++ b/src/org/ice4j/ice/HostCandidate.java @@ -46,14 +46,29 @@ public class HostCandidate extends LocalCandidate public HostCandidate(IceSocketWrapper socket, Component parentComponent) { - super(new TransportAddress(socket.getLocalAddress(), - socket.getLocalPort(), Transport.UDP), + this(socket, + parentComponent, + Transport.UDP); + } + + /** + * Creates a HostCandidate for the specified transport address. + * + * @param transportAddress the transport address for the new + * HostCandidate. + * @param parentComponent the Component that this candidate + * belongs to. + */ + HostCandidate(TransportAddress transportAddress, + Component parentComponent) + { + super(transportAddress, parentComponent, CandidateType.HOST_CANDIDATE, CandidateExtendedType.HOST_CANDIDATE, null); - this.socket = socket; + this.socket = null; setBase(this); } diff --git a/src/org/ice4j/ice/LocalCandidate.java b/src/org/ice4j/ice/LocalCandidate.java index e6ab344..a91a169 100644 --- a/src/org/ice4j/ice/LocalCandidate.java +++ b/src/org/ice4j/ice/LocalCandidate.java @@ -33,7 +33,7 @@ public abstract class LocalCandidate private CandidateExtendedType extendedType = null; /** - * Ufrag for the local candidat + * Ufrag for the local candidate. */ private String ufrag = null; @@ -78,10 +78,16 @@ public abstract class LocalCandidate * * @return the DatagramSocket associated with this * Candidate + * + * @deprecated This should be used by the library only. Users of ice4j + * should use {@link org.ice4j.ice.CandidatePair#getDatagramSocket()} + * on the appropriate CandidatePair instead. */ + @Deprecated public DatagramSocket getDatagramSocket() { - return getIceSocketWrapper().getUDPSocket(); + IceSocketWrapper wrapper = getIceSocketWrapper(); + return wrapper == null ? null : wrapper.getUDPSocket(); } /** @@ -90,10 +96,16 @@ public abstract class LocalCandidate * * @return the Socket associated with this * Candidate + * + * @deprecated This should be used by the library only. Users of ice4j + * should use {@link org.ice4j.ice.CandidatePair#getSocket()} on the + * appropriate CandidatePair instead. */ + @Deprecated public Socket getSocket() { - return getIceSocketWrapper().getTCPSocket(); + IceSocketWrapper wrapper = getIceSocketWrapper(); + return wrapper == null ? null : wrapper.getTCPSocket(); } /** @@ -122,7 +134,8 @@ public abstract class LocalCandidate { IceSocketWrapper hostSocket = getIceSocketWrapper(); - if (hostSocket.getTCPSocket() != null) + if (hostSocket != null + && hostSocket.getTCPSocket() != null) { Socket tcpSocket = hostSocket.getTCPSocket(); Socket tcpStunSocket = null; @@ -178,7 +191,8 @@ public abstract class LocalCandidate return stunSocket; } - else if (hostSocket.getUDPSocket() != null) + else if (hostSocket != null + && hostSocket.getUDPSocket() != null) { DatagramSocket udpSocket = hostSocket.getUDPSocket(); DatagramSocket udpStunSocket = null; @@ -281,7 +295,7 @@ public abstract class LocalCandidate getStunStack().removeSocket(getTransportAddress()); /* - * Allow this LocalCandiate implementation to not create a + * Allow this LocalCandidate implementation to not create a * socket if it still hasn't created one. */ socket.close(); diff --git a/src/org/ice4j/ice/TcpHostCandidate.java b/src/org/ice4j/ice/TcpHostCandidate.java new file mode 100644 index 0000000..653f4f7 --- /dev/null +++ b/src/org/ice4j/ice/TcpHostCandidate.java @@ -0,0 +1,65 @@ +/* + * 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 org.ice4j.*; +import org.ice4j.socket.*; + +import java.util.*; + +/** + * Extends {@link org.ice4j.ice.HostCandidate} allowing the instance to have + * a list of Sockets instead of just one socket. This is needed, + * because with TCP, connections from different remote addresses result in + * different Socket instances. + * + * @author Boris Grozev + */ +public class TcpHostCandidate + extends HostCandidate +{ + /** + * List of accepted sockets for this TcpHostCandidate. + */ + private final List sockets + = new LinkedList(); + + /** + * Initializes a new TcpHostCandidate. + * + * @param transportAddress the transport address of this + * TcpHostCandidate. + * @param parentComponent the Component that this candidate + * belongs to. + */ + public TcpHostCandidate(TransportAddress transportAddress, Component parentComponent) + { + super(transportAddress, + parentComponent); + } + + public List getIceSocketWrappers() + { + return sockets; + } + + public void addSocket(IceSocketWrapper socket) + { + sockets.add(socket); + } + + @Override + protected void free() + { + super.free(); + for (IceSocketWrapper socket : sockets) + { + socket.close(); + } + + } +}