Adds methods to access the sockets of a CandidatePair.

This commit is contained in:
boris 2014-08-29 08:27:20 +00:00
parent 5108d99220
commit c0e7cd56d6
5 changed files with 183 additions and 19 deletions

View File

@ -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<CandidatePair>
@ -38,6 +41,12 @@ public class CandidatePair
*/
private static final long MATH_POW_2_32 = 1L << 32;
/**
* A <tt>Comparator</tt> using the <tt>compareTo</tt> method of the
* <tt>CandidatePair</tt>.
*/
public static final PairComparator comparator = new PairComparator();
/**
* The local candidate of this pair.
*/
@ -68,7 +77,7 @@ public class CandidatePair
/**
* Indicates whether this <tt>CandidatePair</tt> 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 <tt>Comparator</tt> using the <tt>compareTo</tt> method of the
* <tt>CandidatePair</tt>.
*/
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 <tt>true</tt>. 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 <tt>true</tt> if this pair has already been nominated for
* selection and <tt>false</tt> otherwise.
@ -689,4 +692,66 @@ public class CandidatePair
newValue);
}
}
/**
* Returns the UDP <tt>DatagramSocket</tt> (if any) for this
* <tt>CandidatePair</tt>.
* @return the UDP <tt>DatagramSocket</tt> (if any) for this
* <tt>CandidatePair</tt>.
*/
public DatagramSocket getDatagramSocket()
{
IceSocketWrapper wrapper = getIceSocketWrapper();
return wrapper == null ? null : wrapper.getUDPSocket();
}
/**
* Returns the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>.
* @return the TCP <tt>Socket</tt> (if any) for this <tt>CandidatePair</tt>.
*/
public Socket getSocket()
{
IceSocketWrapper wrapper = getIceSocketWrapper();
return wrapper == null ? null : wrapper.getTCPSocket();
}
/**
* Returns the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>.
* @return the <tt>IceSocketWrapper</tt> for this <tt>CandidatePair</tt>.
*/
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;
}
}

View File

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

View File

@ -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
* <tt>HostCandidate</tt>.
* @param parentComponent the <tt>Component</tt> 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);
}

View File

@ -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 <tt>DatagramSocket</tt> associated with this
* <tt>Candidate</tt>
*
* @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getDatagramSocket()}
* on the appropriate <tt>CandidatePair</tt> 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 <tt>Socket</tt> associated with this
* <tt>Candidate</tt>
*
* @deprecated This should be used by the library only. Users of ice4j
* should use {@link org.ice4j.ice.CandidatePair#getSocket()} on the
* appropriate <tt>CandidatePair</tt> 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();

View File

@ -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 <tt>Socket</tt>s instead of just one socket. This is needed,
* because with TCP, connections from different remote addresses result in
* different <tt>Socket</tt> instances.
*
* @author Boris Grozev
*/
public class TcpHostCandidate
extends HostCandidate
{
/**
* List of <tt>accept</tt>ed sockets for this <tt>TcpHostCandidate</tt>.
*/
private final List<IceSocketWrapper> sockets
= new LinkedList<IceSocketWrapper>();
/**
* Initializes a new <tt>TcpHostCandidate</tt>.
*
* @param transportAddress the transport address of this
* <tt>TcpHostCandidate</tt>.
* @param parentComponent the <tt>Component</tt> that this candidate
* belongs to.
*/
public TcpHostCandidate(TransportAddress transportAddress, Component parentComponent)
{
super(transportAddress,
parentComponent);
}
public List<IceSocketWrapper> getIceSocketWrappers()
{
return sockets;
}
public void addSocket(IceSocketWrapper socket)
{
sockets.add(socket);
}
@Override
protected void free()
{
super.free();
for (IceSocketWrapper socket : sockets)
{
socket.close();
}
}
}