diff --git a/src/main/java/org/ice4j/ice/harvest/AwsCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/AwsCandidateHarvester.java
index 73bf410..d2f748c 100644
--- a/src/main/java/org/ice4j/ice/harvest/AwsCandidateHarvester.java
+++ b/src/main/java/org/ice4j/ice/harvest/AwsCandidateHarvester.java
@@ -112,7 +112,7 @@ public class AwsCandidateHarvester
* @return true if we managed to obtain addresses or someone else
* had already achieved that before us, false otherwise.
*/
- private static synchronized boolean obtainEC2Addresses()
+ private synchronized boolean obtainEC2Addresses()
{
if(mask != null && face != null)
return true;
@@ -137,22 +137,23 @@ public class AwsCandidateHarvester
}
catch (Exception exc)
{
- //whatever happens, we just log and bail
+ //whatever happens, we just log and fail
logger.log(Level.INFO, "We failed to obtain EC2 instance addresses "
+ "for the following reason: ", exc);
logger.info("String for local IP: " + localIPStr);
logger.info("String for public IP: " + publicIPStr);
+ return false;
}
return true;
}
/**
- * Returns the discovered public (mask) address, or null.
- * @return the discovered public (mask) address, or null.
+ * Returns the public (mask) address, or null.
+ * @return the public (mask) address, or null.
*/
- public static TransportAddress getDiscoveredMask()
+ public TransportAddress getMask()
{
if (smellsLikeAnEC2())
{
@@ -163,10 +164,10 @@ public class AwsCandidateHarvester
}
/**
- * Returns the discovered local (face) address, or null.
- * @return the discovered local (face) address, or null.
+ * Returns the local (face) address, or null.
+ * @return the local (face) address, or null.
*/
- public static TransportAddress getDiscoveredFace()
+ public TransportAddress getFace()
{
if (smellsLikeAnEC2())
{
@@ -238,22 +239,4 @@ 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;
- }
}
diff --git a/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvester.java
index ed4b623..a68ed25 100644
--- a/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvester.java
+++ b/src/main/java/org/ice4j/ice/harvest/MappingCandidateHarvester.java
@@ -46,12 +46,12 @@ public class MappingCandidateHarvester
/**
* The addresses that we will use as a mask
*/
- private final TransportAddress mask;
+ protected TransportAddress mask;
/**
* The addresses that we will be masking
*/
- private final TransportAddress face;
+ protected TransportAddress face;
/**
* Creates a mapping harvester with the specified mask
diff --git a/src/main/java/org/ice4j/ice/harvest/StunMappingCandidateHarvester.java b/src/main/java/org/ice4j/ice/harvest/StunMappingCandidateHarvester.java
new file mode 100644
index 0000000..aa3359c
--- /dev/null
+++ b/src/main/java/org/ice4j/ice/harvest/StunMappingCandidateHarvester.java
@@ -0,0 +1,206 @@
+/*
+ * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
+ *
+ * Copyright @ 2015 Atlassian Pty Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.ice4j.ice.harvest;
+
+import org.ice4j.*;
+import org.ice4j.ice.*;
+
+import java.util.*;
+import java.util.logging.*;
+
+/**
+ * A MappingCandidateHarvester which use a list of pre-configured stun servers
+ * to discover its public ip address.
+ *
+ * @author Damian Minkov
+ */
+public class StunMappingCandidateHarvester
+ extends MappingCandidateHarvester
+{
+ /**
+ * The Logger used by the StunMappingCandidateHarvester
+ * class and its instances for logging output.
+ */
+ private static final Logger logger
+ = Logger.getLogger(StunMappingCandidateHarvester.class.getName());
+
+ /**
+ * The addresses that we will use as a mask
+ */
+ private static TransportAddress mask;
+
+ /**
+ * The addresses that we will be masking
+ */
+ private static TransportAddress face;
+
+ /**
+ * Whether we have already checked and found the mapping addresses.
+ */
+ private static boolean addressChecked = false;
+
+ /**
+ * The list of servers we will use to discover our public address.
+ */
+ private static String[] stunServers;
+
+ /**
+ * Creates a mapping harvester with the specified mask
+ * @param servers list of stun servers in the form address:port
+ */
+ public StunMappingCandidateHarvester(String[] servers)
+ {
+ super(null, null);
+
+ this.stunServers = servers;
+
+ // we have the list of addresses lets, try discovering
+ obtainAddresses();
+ }
+
+ /**
+ * Creates a mapping harvester with the specified mask
+ */
+ public StunMappingCandidateHarvester()
+ {
+ super(null, null);
+ }
+
+ /**
+ * Maps all candidates to this harvester's mask and adds them to
+ * component.
+ *
+ * @param component the {@link Component} that we'd like to map candidates
+ * to.
+ * @return the LocalCandidates gathered by this
+ * CandidateHarvester or null if no mask is specified.
+ */
+ public Collection 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 true if we managed to obtain addresses or someone else
+ * had already achieved that before us, false otherwise.
+ */
+ private synchronized boolean obtainAddresses()
+ {
+ if(mask != null && face != null)
+ return true;
+
+ try
+ {
+ for (String server : stunServers)
+ {
+ String[] addressAndPort = server.split(":");
+
+ StunCandidateHarvester stunHarv = new StunCandidateHarvester(
+ new TransportAddress(
+ addressAndPort[0],
+ Integer.valueOf(addressAndPort[1]),
+ Transport.UDP));
+
+ Agent agent = new Agent();
+ agent.setTrickling(false);
+ agent.addCandidateHarvester(stunHarv);
+
+ IceMediaStream stream = agent.createMediaStream("audio");
+ // local ports does not matter, just to be something
+ // out of the default used range
+ agent.createComponent(
+ stream, Transport.UDP, 32020, 32020, 32020 + 100);
+
+ IceMediaStream iceMediaStream = agent.getStreams().get(0);
+ TransportAddress addr = iceMediaStream
+ .getComponent(Component.RTP).getDefaultCandidate()
+ .getTransportAddress();
+ this.mask = addr;
+
+ Candidate> candidate =
+ iceMediaStream.getComponents().get(0).getDefaultCandidate();
+ this.face = candidate.getHostAddress();
+
+ agent.free();
+
+ if(this.mask != null && this.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;
+ }
+
+ /**
+ * Returns the public (mask) address, or null.
+ * @return the public (mask) address, or null.
+ */
+ public TransportAddress getMask()
+ {
+ if (!isAddressChecked())
+ {
+ obtainAddresses();
+ }
+ return mask;
+ }
+
+ /**
+ * Returns the local (face) address, or null.
+ * @return the local (face) address, or null.
+ */
+ public TransportAddress getFace()
+ {
+ if (!isAddressChecked())
+ {
+ obtainAddresses();
+ }
+ return face;
+ }
+}