Merge pull request #45 from jitsi/mapping-harvesters

Updates mapping harvesters.
This commit is contained in:
damencho 2015-11-09 09:59:17 -06:00
commit 9bd973f0a8
2 changed files with 18 additions and 79 deletions

View File

@ -18,11 +18,9 @@
package org.ice4j.ice.harvest;
import org.ice4j.*;
import org.ice4j.ice.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;
/**
@ -74,6 +72,11 @@ public class AwsCandidateHarvester
*/
private static TransportAddress face;
/**
* Whether we have already checked and found the mapping addresses.
*/
private static boolean addressChecked = false;
/**
* Creates an AWS harvester. The actual addresses wil be retrieved later,
* during the first harvest.
@ -83,39 +86,17 @@ public class AwsCandidateHarvester
super(null, null);
}
/**
* Maps all candidates to this harvester's mask and adds them to
* <tt>component</tt>.
*
* @param component the {@link Component} that we'd like to map candidates
* to.
* @return the <tt>LocalCandidate</tt>s gathered by this
* <tt>CandidateHarvester</tt> or <tt>null</tt> if no mask is specified.
*/
public Collection<LocalCandidate> harvest(Component component)
{
if (mask == null || face == null)
{
if(!obtainEC2Addresses())
return null;
}
return super.harvest(component);
}
/**
* Sends HTTP GET queries to
* <tt>http://169.254.169.254/latest/meta-data/local-ipv4</tt> and
* <tt>http://169.254.169.254/latest/meta-data/public-ipv4</tt> to learn the
* private (face) and public (mask) addresses of this EC2 instance.
*
* @return <tt>true</tt> if we managed to obtain addresses or someone else
* had already achieved that before us, <tt>false</tt> otherwise.
*/
private synchronized boolean obtainEC2Addresses()
private static synchronized void obtainEC2Addresses()
{
if(mask != null && face != null)
return true;
if (addressChecked)
return;
addressChecked = true;
String localIPStr = null;
String publicIPStr = null;
@ -142,11 +123,7 @@ public class AwsCandidateHarvester
+ "for the following reason: ", exc);
logger.info("String for local IP: " + localIPStr);
logger.info("String for public IP: " + publicIPStr);
return false;
}
return true;
}
/**

View File

@ -20,7 +20,6 @@ package org.ice4j.ice.harvest;
import org.ice4j.*;
import org.ice4j.ice.*;
import java.util.*;
import java.util.logging.*;
/**
@ -81,38 +80,16 @@ public class StunMappingCandidateHarvester
super(null, null);
}
/**
* Maps all candidates to this harvester's mask and adds them to
* <tt>component</tt>.
*
* @param component the {@link Component} that we'd like to map candidates
* to.
* @return the <tt>LocalCandidate</tt>s gathered by this
* <tt>CandidateHarvester</tt> or <tt>null</tt> if no mask is specified.
*/
public Collection<LocalCandidate> harvest(Component component)
{
if (mask == null || face == null)
{
if(!obtainAddresses())
return null;
}
return super.harvest(component);
}
/**
* Uses the pre-configured list of stun servers to discover our public
* address. Uses the first successful one and ignore the rest.
* Learn the private (face) and public (mask) addresses of this instance.
*
* @return <tt>true</tt> if we managed to obtain addresses or someone else
* had already achieved that before us, <tt>false</tt> otherwise.
*/
private synchronized boolean obtainAddresses()
private static synchronized void obtainAddresses()
{
if(mask != null && face != null)
return true;
if (addressChecked)
return;
addressChecked = true;
try
{
@ -140,43 +117,28 @@ public class StunMappingCandidateHarvester
TransportAddress addr = iceMediaStream
.getComponent(Component.RTP).getDefaultCandidate()
.getTransportAddress();
StunMappingCandidateHarvester.mask = addr;
mask = addr;
Candidate<?> candidate =
iceMediaStream.getComponents().get(0).getDefaultCandidate();
StunMappingCandidateHarvester.face = candidate.getHostAddress();
face = candidate.getHostAddress();
agent.free();
if(StunMappingCandidateHarvester.mask != null
&& StunMappingCandidateHarvester.face != null)
if (mask != null && face != null)
break;
}
logger.info("Detected through stun local IP: " + face);
logger.info("Detected through stun public IP: " + mask);
addressChecked = true;
}
catch (Exception exc)
{
//whatever happens, we just log and fail
logger.log(Level.INFO, "We failed to obtain addresses "
+ "for the following reason: ", exc);
return false;
}
return true;
}
/**
* Whether we had already checked and succeeded finding address.
* @return whether we had already checked and succeeded finding address.
*/
private boolean isAddressChecked()
{
return addressChecked;
}
/**
@ -185,7 +147,7 @@ public class StunMappingCandidateHarvester
*/
public TransportAddress getMask()
{
if (!isAddressChecked())
if (mask == null)
{
obtainAddresses();
}
@ -198,7 +160,7 @@ public class StunMappingCandidateHarvester
*/
public TransportAddress getFace()
{
if (!isAddressChecked())
if (face == null)
{
obtainAddresses();
}