[SMACK-213] - Add new algorithm to discover usable localhost

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@8057 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Thiago Camargo 2007-04-18 17:26:58 +00:00 committed by thiago
parent 574cdab26f
commit 08ae96a721
4 changed files with 65 additions and 25 deletions

View File

@ -122,8 +122,6 @@ public class AudioChannel {
started = false;
}
started = true;
// Create an RTP session to transmit the output of the
// processor to the specified IP address and port no.
result = createTransmitter();
@ -132,6 +130,9 @@ public class AudioChannel {
processor = null;
started = false;
}
else {
started = true;
}
// Start the transmission
processor.start();
@ -203,15 +204,17 @@ public class AudioChannel {
// Wait for it to configure
boolean result = waitForState(processor, Processor.Configured);
if (!result)
if (!result){
return "Couldn't configure processor";
}
// Get the tracks from the processor
TrackControl[] tracks = processor.getTrackControls();
// Do we have atleast one track?
if (tracks == null || tracks.length < 1)
if (tracks == null || tracks.length < 1){
return "Couldn't find tracks in processor";
}
// Set the output content descriptor to RAW_RTP
// This will limit the supported formats reported from

View File

@ -24,18 +24,18 @@ import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession;
import java.util.Random;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.net.*;
/**
* Bridged Resolver use a RTPBridge Service to add a relayed candidate.
* A very reliable solution for NAT Traversal.
*
* <p/>
* The resolver verify is the XMPP Server that the client is connected offer this service.
* If the server supports, a candidate is requested from the service.
* The resolver adds this candidate
*/
public class BridgedResolver extends TransportResolver{
public class BridgedResolver extends TransportResolver {
XMPPConnection connection;
@ -67,21 +67,7 @@ public class BridgedResolver extends TransportResolver{
RTPBridge rtpBridge = RTPBridge.getRTPBridge(connection, String.valueOf(sid));
String localIp="127.0.0.1";
try {
localIp = InetAddress.getLocalHost().getHostAddress();
InetAddress iaddress = InetAddress.getLocalHost();
System.out.println(iaddress.isLoopbackAddress());
System.out.println(iaddress.isLinkLocalAddress());
System.out.println(iaddress.isSiteLocalAddress());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
String localIp = getLocalHost();
TransportCandidate localCandidate = new TransportCandidate.Fixed(
rtpBridge.getIp(), rtpBridge.getPortA());
@ -124,4 +110,38 @@ public class BridgedResolver extends TransportResolver{
// Nothing to do here
}
public static String getLocalHost() {
Enumeration ifaces = null;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
}
catch (SocketException e) {
e.printStackTrace();
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
Enumeration iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = (InetAddress) iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress() && !(iaddress instanceof Inet6Address)) {
return iaddress.getHostAddress();
}
}
}
try {
return InetAddress.getLocalHost().getHostAddress();
}
catch (Exception e) {
e.printStackTrace();
}
return "127.0.0.1";
}
}

View File

@ -138,7 +138,7 @@ public class ICEResolver extends TransportResolver {
network = iceNegociator.getPublicCandidate().getNetwork();
}
else {
localIp = InetAddress.getLocalHost().getHostAddress();
localIp = BridgedResolver.getLocalHost();
network = 0;
}

View File

@ -0,0 +1,17 @@
package org.jivesoftware.smackx.jingle.nat;
import junit.framework.TestCase;
import java.util.Enumeration;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.InetAddress;
import java.net.DatagramSocket;
public class LocalhostTest extends TestCase {
public void testGetLocalhost() {
System.out.println(BridgedResolver.getLocalHost());
}
}