From 82544254c4940943e93da600184f372e4f50ab30 Mon Sep 17 00:00:00 2001 From: Boris Grozev Date: Tue, 29 Nov 2016 12:54:52 -0600 Subject: [PATCH] feat: Adds candidates' sockets to the component's merging socket. --- .../org/ice4j/ice/GoogleRelayedCandidate.java | 2 +- .../java/org/ice4j/ice/RelayedCandidate.java | 2 +- .../java/org/ice4j/ice/UPNPCandidate.java | 2 +- .../harvest/GoogleTurnCandidateHarvest.java | 8 +++++++ .../GoogleTurnSSLCandidateHarvester.java | 11 +++++----- .../ice/harvest/HostCandidateHarvester.java | 6 +++-- .../ice/harvest/StunCandidateHarvester.java | 11 ++++++---- .../ice/harvest/TurnCandidateHarvest.java | 2 ++ .../org/ice4j/ice/harvest/UPNPHarvester.java | 12 +++++----- .../socket/IceTcpServerSocketWrapper.java | 1 + .../ice4j/socket/MergingDatagramSocket.java | 22 ++++++++++++++++--- 11 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java b/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java index 9a97198..be5e199 100644 --- a/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java +++ b/src/main/java/org/ice4j/ice/GoogleRelayedCandidate.java @@ -194,7 +194,7 @@ public class GoogleRelayedCandidate * @see LocalCandidate#getCandidateIceSocketWrapper() */ @Override - protected synchronized IceSocketWrapper getCandidateIceSocketWrapper() + public synchronized IceSocketWrapper getCandidateIceSocketWrapper() { if (socket == null) { diff --git a/src/main/java/org/ice4j/ice/RelayedCandidate.java b/src/main/java/org/ice4j/ice/RelayedCandidate.java index 9adf723..8140034 100644 --- a/src/main/java/org/ice4j/ice/RelayedCandidate.java +++ b/src/main/java/org/ice4j/ice/RelayedCandidate.java @@ -129,7 +129,7 @@ public class RelayedCandidate * Candidate */ @Override - protected synchronized IceSocketWrapper getCandidateIceSocketWrapper() + public synchronized IceSocketWrapper getCandidateIceSocketWrapper() { if (socket == null) { diff --git a/src/main/java/org/ice4j/ice/UPNPCandidate.java b/src/main/java/org/ice4j/ice/UPNPCandidate.java index 95589e2..6525d18 100644 --- a/src/main/java/org/ice4j/ice/UPNPCandidate.java +++ b/src/main/java/org/ice4j/ice/UPNPCandidate.java @@ -92,7 +92,7 @@ public class UPNPCandidate * {@inheritDoc} */ @Override - protected IceSocketWrapper getCandidateIceSocketWrapper() + public IceSocketWrapper getCandidateIceSocketWrapper() { return getBase().getCandidateIceSocketWrapper(); } diff --git a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java index d79badc..215717e 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnCandidateHarvest.java @@ -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); } } diff --git a/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java index eb51111..18639f1 100644 --- a/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/GoogleTurnSSLCandidateHarvester.java @@ -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 InputStream and + * certificate). We explicitly need InputStream and * OutputStream because some Socket may redefine * getInputStream()/getOutputStream() and we need the original stream. * * @param inputStream InputStream of the socket - * @param outputStream OuputStream of the socket + * @param outputStream OutputStream of the socket * @return true if the SSL handshake is done * @throws IOException if something goes wrong */ diff --git a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java index 66e42d4..5a7b93a 100644 --- a/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -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)) { diff --git a/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java index 684f13f..3503ffa 100644 --- a/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -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) { diff --git a/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java b/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java index 926cc45..cb237c4 100644 --- a/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java +++ b/src/main/java/org/ice4j/ice/harvest/TurnCandidateHarvest.java @@ -258,6 +258,8 @@ public class TurnCandidateHarvest harvester.getStunStack().addSocket( relayedCandidate.getStunSocket(null)); + relayedCandidate.getParentComponent().getSocket().add( + relayedCandidate.getCandidateIceSocketWrapper()); addCandidate(relayedCandidate); } } diff --git a/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java b/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java index a838445..8ae0677 100644 --- a/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/UPNPHarvester.java @@ -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 UPNPCandidate instance which * represents the specified TransportAddress * @throws Exception if something goes wrong during candidate creation */ - public List createUPNPCandidate(IceSocketWrapper socket, - String externalIP, int port, Component cmp, GatewayDevice device) + private List createUPNPCandidate(IceSocketWrapper socket, + String externalIP, int port, Component component, GatewayDevice device) throws Exception { List 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); diff --git a/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java b/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java index d6a076b..3a38d7d 100644 --- a/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java +++ b/src/main/java/org/ice4j/socket/IceTcpServerSocketWrapper.java @@ -195,6 +195,7 @@ public class IceTcpServerSocketWrapper component.getParentStream().getParentAgent(). getStunStack().addSocket( new IceTcpSocketWrapper(multiplexingSocket)); + component.getSocket().add(multiplexingSocket); sockets.add(multiplexingSocket); } diff --git a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java index 2c0c30c..9075444 100644 --- a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java @@ -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)