refactor: Refactors the Component's socket API.

This commit is contained in:
Boris Grozev 2016-12-01 12:01:10 -06:00
parent dae6d32875
commit b4a5e2eb83
11 changed files with 48 additions and 36 deletions

View File

@ -124,16 +124,27 @@ public class Component
private final Logger logger;
/**
* The {@link MergingDatagramSocket} instance which will serve as the
* single socket instance for this {@link Component}, merging received
* packets from all of its candidates.
* The single {@link ComponentSocket} instance for this {@link Component},
* which will merge the multiple sockets from the candidate/pairs.
*/
private final MergingDatagramSocket mergingDatagramSocket;
private final ComponentSocket componentSocket;
/**
* A wrapper around {@link #mergingDatagramSocket}.
* The public view of {@link #componentSocket}, wrapped in a
* {@link MultiplexingDatagramSocket} for the convenience of users of the
* library.
* This is the instance which should be used by applications for
* reading/writing application data.
*/
private final IceUdpSocketWrapper mergingDatagramSocketWrapper;
private final MultiplexingDatagramSocket socket;
/**
* A wrapper around {@link #socket}, kept only to help preserve the old
* API (see {@link LocalCandidate#getIceSocketWrapper()} and
* {@link CandidatePair#getIceSocketWrapper()})
*/
@Deprecated
private final IceSocketWrapper socketWrapper;
/**
* Creates a new <tt>Component</tt> with the specified <tt>componentID</tt>
@ -152,11 +163,9 @@ public class Component
try
{
mergingDatagramSocket = new ComponentSocket(this);
mergingDatagramSocketWrapper
= new IceUdpSocketWrapper(
new MultiplexingDatagramSocket(
mergingDatagramSocket));
componentSocket = new ComponentSocket(this);
socket = new MultiplexingDatagramSocket(componentSocket);
socketWrapper = new IceUdpSocketWrapper(socket);
}
catch (SocketException se)
{
@ -807,7 +816,7 @@ public class Component
}
}
getSocket().close();
getComponentSocket().close();
}
/**
@ -941,24 +950,30 @@ public class Component
}
/**
* @return the single socket for this {@link Component} which should be
* used for reading and writing data.
* @return the internal merging socket for this component. This is for
* ice4j use only.
* For reading/writing application data, use {@link #getSocket()}.
*/
public MergingDatagramSocket getSocket()
public ComponentSocket getComponentSocket()
{
return mergingDatagramSocket;
return componentSocket;
}
/**
* @return the socket for this {@link Component}, which should be used for
* reading/writing application data.
*/
public MultiplexingDatagramSocket getSocket()
{
return socket;
}
/**
* @return an {@link IceSocketWrapper} instance wrapping the socket for this
* candidate (see {@link #getSocket()}).
* @deprecated Use {@link #getSocket()} directly. This is only introduced
* to ease the transition of applications which are already written to use
* an {@link IceSocketWrapper} instance.
* candidate (see {@link #getComponentSocket()}).
*/
@Deprecated
public IceSocketWrapper getSocketWrapper()
{
return mergingDatagramSocketWrapper;
return socketWrapper;
}
}

View File

@ -29,7 +29,7 @@ import java.util.logging.*;
* Extends {@link MergingDatagramSocket} with functionality specific to
* an ICE {@link Component}.
*/
class ComponentSocket
public class ComponentSocket
extends MergingDatagramSocket
implements PropertyChangeListener
{
@ -155,6 +155,7 @@ class ComponentSocket
}
finally
{
component.getParentStream().removePairStateChangeListener(this);
component = null;
}
}

View File

@ -139,7 +139,7 @@ public class GoogleTurnCandidateHarvest
= relayedCandidate.getCandidateIceSocketWrapper();
Component component = relayedCandidate.getParentComponent();
component.getSocket().add(candidateSocket);
component.getComponentSocket().add(candidateSocket);
addCandidate(relayedCandidate);
}

View File

@ -158,7 +158,7 @@ public class GoogleTurnSSLCandidateHarvester
.getParentAgent()
.getStunStack()
.addSocket(cand.getStunSocket(null));
parentComponent.getSocket().add(multiplexing);
parentComponent.getComponentSocket().add(multiplexing);
}
}
catch (Exception e)

View File

@ -418,7 +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);
component.getComponentSocket().add(sock);
}
}
}

View File

@ -18,18 +18,14 @@
package org.ice4j.ice.harvest;
import org.ice4j.*;
import org.ice4j.attribute.*;
import org.ice4j.ice.*;
import org.ice4j.message.*;
import org.ice4j.socket.*;
import org.ice4j.stack.*;
import org.ice4j.util.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import java.util.logging.Logger; // Disambiguation
/**
@ -366,7 +362,7 @@ public class SinglePortUdpHarvester
new TransportAddress(remoteAddress, Transport.UDP));
// TODO: maybe move this code to the candidates.
component.getSocket().add(multiplexing);
component.getComponentSocket().add(multiplexing);
// XXX is this necessary?
synchronized (candidateSockets)

View File

@ -458,7 +458,7 @@ public class StunCandidateHarvester
hostCand.getParentComponent().getParentStream().
getParentAgent().getStunStack().addSocket(
cand.getStunSocket(null));
hostCand.getParentComponent().getSocket().add(multiplexing);
hostCand.getParentComponent().getComponentSocket().add(multiplexing);
}
catch(Exception io)
{

View File

@ -520,7 +520,7 @@ public class TcpHarvester
candidate.addSocket(candidateSocket);
// TODO: Maybe move this code to the candidate.
component.getSocket().add(multiplexing);
component.getComponentSocket().add(multiplexing);
// the socket is not our responsibility anymore. It is up to
// the candidate/component to close/free it.

View File

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

View File

@ -223,7 +223,7 @@ public class UPNPHarvester
= new UPNPCandidate(addr, base, component, device);
IceSocketWrapper stunSocket = candidate.getStunSocket(null);
candidate.getStunStack().addSocket(stunSocket);
component.getSocket().add(candidate.getCandidateIceSocketWrapper());
component.getComponentSocket().add(candidate.getCandidateIceSocketWrapper());
ret.add(candidate);
ret.add(base);

View File

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