Makes AwsCandidateHarvester reuses the harvest logic from super.

This commit is contained in:
damencho 2015-11-05 15:19:26 -06:00
parent 6a97941d68
commit 9512d303a2
3 changed files with 48 additions and 50 deletions

View File

@ -94,51 +94,13 @@ public class AwsCandidateHarvester
*/
public Collection<LocalCandidate> harvest(Component component)
{
if (mask == null || face == null)
{
if(!obtainEC2Addresses())
return null;
}
/*
* Report the LocalCandidates gathered by this CandidateHarvester so
* that the harvest is sure to be considered successful.
*/
Collection<LocalCandidate> candidates = new HashSet<LocalCandidate>();
for (Candidate<?> cand : component.getLocalCandidates())
{
if (!(cand instanceof HostCandidate)
|| !cand.getTransportAddress().getHostAddress()
.equals(face.getHostAddress()))
{
continue;
}
TransportAddress mappedAddress = new TransportAddress(
mask.getHostAddress(),
cand.getHostAddress().getPort(),
cand.getHostAddress().getTransport());
ServerReflexiveCandidate mappedCandidate
= new ServerReflexiveCandidate(
mappedAddress,
(HostCandidate)cand,
cand.getStunServerAddress(),
CandidateExtendedType.STATICALLY_MAPPED_CANDIDATE);
//try to add the candidate to the component and then
//only add it to the harvest not redundant
if( !candidates.contains(mappedCandidate)
&& component.addLocalCandidate(mappedCandidate))
{
candidates.add(mappedCandidate);
}
}
return candidates;
return super.harvest(component);
}
/**
@ -190,7 +152,7 @@ public class AwsCandidateHarvester
* Returns the discovered public (mask) address, or null.
* @return the discovered public (mask) address, or null.
*/
public static TransportAddress getMask()
public static TransportAddress getDiscoveredMask()
{
if (smellsLikeAnEC2())
{
@ -204,7 +166,7 @@ public class AwsCandidateHarvester
* Returns the discovered local (face) address, or null.
* @return the discovered local (face) address, or null.
*/
public static TransportAddress getFace()
public static TransportAddress getDiscoveredFace()
{
if (smellsLikeAnEC2())
{
@ -276,4 +238,22 @@ public class AwsCandidateHarvester
return retString;
}
/**
* Returns the public (mask) address, or null.
* @return the public (mask) address, or null.
*/
public TransportAddress getMask()
{
return this.mask;
}
/**
* Returns the local (face) address, or null.
* @return the local (face) address, or null.
*/
public TransportAddress getFace()
{
return this.face;
}
}

View File

@ -46,12 +46,12 @@ public class MappingCandidateHarvester
/**
* The addresses that we will use as a mask
*/
private TransportAddress mask;
private final TransportAddress mask;
/**
* The addresses that we will be masking
*/
private TransportAddress face;
private final TransportAddress face;
/**
* Creates a mapping harvester with the specified <tt>mask</tt>
@ -65,6 +65,7 @@ public class MappingCandidateHarvester
this.mask = mask;
this.face = face;
}
/**
* Maps all candidates to this harvester's mask and adds them to
* <tt>component</tt>.
@ -76,8 +77,7 @@ public class MappingCandidateHarvester
*/
public Collection<LocalCandidate> harvest(Component component)
{
if (mask == null || face == null)
if (getMask() == null || getFace() == null)
return null;
/*
@ -90,15 +90,15 @@ public class MappingCandidateHarvester
{
if (!(cand instanceof HostCandidate)
|| !cand.getTransportAddress().getHostAddress()
.equals(face.getHostAddress())
|| cand.getTransport() != face.getTransport())
.equals(getFace().getHostAddress())
|| cand.getTransport() != getFace().getTransport())
{
continue;
}
HostCandidate hostCandidate = (HostCandidate) cand;
TransportAddress mappedAddress = new TransportAddress(
mask.getHostAddress(),
getMask().getHostAddress(),
hostCandidate.getHostAddress().getPort(),
hostCandidate.getHostAddress().getTransport());
@ -122,4 +122,22 @@ public class MappingCandidateHarvester
return candidates;
}
/**
* Returns the public (mask) address, or null.
* @return the public (mask) address, or null.
*/
public TransportAddress getMask()
{
return this.mask;
}
/**
* Returns the local (face) address, or null.
* @return the local (face) address, or null.
*/
public TransportAddress getFace()
{
return this.face;
}
}

View File

@ -640,10 +640,10 @@ public class MultiplexingTcpHostHarvester
}
// Automatically add a mapping if we're on EC2
TransportAddress ec2Face = AwsCandidateHarvester.getFace();
TransportAddress ec2Face = AwsCandidateHarvester.getDiscoveredFace();
TransportAddress ec2Mask;
if (ec2Face != null &&
(ec2Mask = AwsCandidateHarvester.getMask()) != null)
(ec2Mask = AwsCandidateHarvester.getDiscoveredMask()) != null)
{
addMappedAddress(ec2Mask.getAddress(), ec2Face.getAddress());
logger.info("Adding an EC2 mapping for TCP: "