feat: Adds candidates' sockets to the component's merging socket.

This commit is contained in:
Boris Grozev 2016-11-29 12:54:52 -06:00
parent c378329b8c
commit 82544254c4
11 changed files with 57 additions and 22 deletions

View File

@ -194,7 +194,7 @@ public class GoogleRelayedCandidate
* @see LocalCandidate#getCandidateIceSocketWrapper()
*/
@Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
public synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{
if (socket == null)
{

View File

@ -129,7 +129,7 @@ public class RelayedCandidate
* <tt>Candidate</tt>
*/
@Override
protected synchronized IceSocketWrapper getCandidateIceSocketWrapper()
public synchronized IceSocketWrapper getCandidateIceSocketWrapper()
{
if (socket == null)
{

View File

@ -92,7 +92,7 @@ public class UPNPCandidate
* {@inheritDoc}
*/
@Override
protected IceSocketWrapper getCandidateIceSocketWrapper()
public IceSocketWrapper getCandidateIceSocketWrapper()
{
return getBase().getCandidateIceSocketWrapper();
}

View File

@ -133,6 +133,14 @@ public class GoogleTurnCandidateHarvest
harvester.getStunStack().addSocket(
relayedCandidate.getStunSocket(null));
// Make the relayed candidate's socket available for reading
// by the component.
IceSocketWrapper candidateSocket
= relayedCandidate.getCandidateIceSocketWrapper();
Component component = relayedCandidate.getParentComponent();
component.getSocket().add(candidateSocket);
addCandidate(relayedCandidate);
}
}

View File

@ -121,7 +121,7 @@ public class GoogleTurnSSLCandidateHarvester
/**
* Returns the host candidate.
* For UDP it simply returns the candidate passed as paramter
* For UDP it simply returns the candidate passed as parameter
*
* However for TCP, we cannot return the same hostCandidate because in Java
* a "server" socket cannot connect to a destination with the same local
@ -146,11 +146,11 @@ public class GoogleTurnSSLCandidateHarvester
if(sslHandshake(inputStream, outputStream))
{
Component parentComponent = hostCand.getParentComponent();
MultiplexingSocket multiplexing = new MultiplexingSocket(sock);
cand
= new HostCandidate(
new IceTcpSocketWrapper(
new MultiplexingSocket(sock)),
new IceTcpSocketWrapper(multiplexing),
parentComponent,
Transport.TCP);
parentComponent
@ -158,6 +158,7 @@ public class GoogleTurnSSLCandidateHarvester
.getParentAgent()
.getStunStack()
.addSocket(cand.getStunSocket(null));
parentComponent.getSocket().add(multiplexing);
}
}
catch (Exception e)
@ -187,12 +188,12 @@ public class GoogleTurnSSLCandidateHarvester
/**
* Do the SSL handshake (send client certificate and wait for receive server
* certificate). We explicitely need <tt>InputStream</tt> and
* certificate). We explicitly need <tt>InputStream</tt> and
* <tt>OutputStream</tt> because some <tt>Socket</tt> may redefine
* getInputStream()/getOutputStream() and we need the original stream.
*
* @param inputStream <tt>InputStream</tt> of the socket
* @param outputStream <tt>OuputStream</tt> of the socket
* @param outputStream <tt>OutputStream</tt> of the socket
* @return true if the SSL handshake is done
* @throws IOException if something goes wrong
*/

View File

@ -418,6 +418,7 @@ public class HostCandidateHarvester
// checks. In case we have enabled STUN, we are going to use
// them as well while harvesting reflexive candidates.
createAndRegisterStunSocket(candidate);
component.getSocket().add(sock);
}
}
}
@ -569,8 +570,9 @@ public class HostCandidateHarvester
sock.setReuseAddress(true);
sock.bind(new InetSocketAddress(laddr, port));
IceSocketWrapper socket
= new IceTcpServerSocketWrapper(sock,
component);
= new IceTcpServerSocketWrapper(
new DelegatingServerSocket(sock),
component);
if(logger.isLoggable(Level.FINEST))
{

View File

@ -441,7 +441,7 @@ public class StunCandidateHarvester
*/
protected HostCandidate getHostCandidate(HostCandidate hostCand)
{
HostCandidate cand = null;
HostCandidate cand;
// create a new TCP HostCandidate
if(hostCand.getTransport() == Transport.TCP)
@ -450,12 +450,15 @@ public class StunCandidateHarvester
{
Socket sock = new Socket(stunServer.getAddress(),
stunServer.getPort());
cand = new HostCandidate(new IceTcpSocketWrapper(
new MultiplexingSocket(sock)),
hostCand.getParentComponent(), Transport.TCP);
MultiplexingSocket multiplexing = new MultiplexingSocket(sock);
cand
= new HostCandidate(
new IceTcpSocketWrapper(multiplexing),
hostCand.getParentComponent(), Transport.TCP);
hostCand.getParentComponent().getParentStream().
getParentAgent().getStunStack().addSocket(
cand.getStunSocket(null));
hostCand.getParentComponent().getSocket().add(multiplexing);
}
catch(Exception io)
{

View File

@ -258,6 +258,8 @@ public class TurnCandidateHarvest
harvester.getStunStack().addSocket(
relayedCandidate.getStunSocket(null));
relayedCandidate.getParentComponent().getSocket().add(
relayedCandidate.getCandidateIceSocketWrapper());
addCandidate(relayedCandidate);
}
}

View File

@ -203,25 +203,27 @@ public class UPNPHarvester
* @param socket local socket
* @param externalIP external IP address
* @param port local port
* @param cmp parent component
* @param component parent component
* @param device the UPnP gateway device
* @return a new <tt>UPNPCandidate</tt> instance which
* represents the specified <tt>TransportAddress</tt>
* @throws Exception if something goes wrong during candidate creation
*/
public List<LocalCandidate> createUPNPCandidate(IceSocketWrapper socket,
String externalIP, int port, Component cmp, GatewayDevice device)
private List<LocalCandidate> createUPNPCandidate(IceSocketWrapper socket,
String externalIP, int port, Component component, GatewayDevice device)
throws Exception
{
List<LocalCandidate> ret = new ArrayList<>();
TransportAddress addr
= new TransportAddress(externalIP, port, Transport.UDP);
HostCandidate base = new HostCandidate(socket, cmp);
HostCandidate base = new HostCandidate(socket, component);
UPNPCandidate candidate = new UPNPCandidate(addr, base, cmp, device);
UPNPCandidate candidate
= new UPNPCandidate(addr, base, component, device);
IceSocketWrapper stunSocket = candidate.getStunSocket(null);
candidate.getStunStack().addSocket(stunSocket);
component.getSocket().add(candidate.getCandidateIceSocketWrapper());
ret.add(candidate);
ret.add(base);

View File

@ -195,6 +195,7 @@ public class IceTcpServerSocketWrapper
component.getParentStream().getParentAgent().
getStunStack().addSocket(
new IceTcpSocketWrapper(multiplexingSocket));
component.getSocket().add(multiplexingSocket);
sockets.add(multiplexingSocket);
}

View File

@ -147,6 +147,21 @@ public class MergingDatagramSocket
doAdd(socket);
}
/**
* Adds the socket instance wrapped by {@code wrapper} to this merging
* socket. Note that this will start a thread reading from the added socket.
* @param wrapper the wrapper of the socket to add.
*/
public void add(IceSocketWrapper wrapper)
{
Object socket = wrapper.getUDPSocket();
if (socket == null)
{
socket = wrapper.getTCPSocket();
}
doAdd(socket);
}
/**
* Adds a {@link DatagramSocket} instance to this merging socket. Note
* that this will start a thread reading from the added socket.
@ -171,12 +186,13 @@ public class MergingDatagramSocket
*/
private void doAdd(Object socket)
{
Objects.requireNonNull(socket, "socket");
if (!(socket instanceof DelegatingSocket) &&
!(socket instanceof DatagramSocket))
{
logger.severe("Unsupported class: "
+ (socket == null ? "null" : socket.getClass()));
return;
throw new IllegalStateException("Socket type not supported: "
+ socket.getClass().getName());
}
synchronized (socketContainersSyncRoot)