1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

[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 = false;
} }
started = true;
// Create an RTP session to transmit the output of the // Create an RTP session to transmit the output of the
// processor to the specified IP address and port no. // processor to the specified IP address and port no.
result = createTransmitter(); result = createTransmitter();
@ -132,6 +130,9 @@ public class AudioChannel {
processor = null; processor = null;
started = false; started = false;
} }
else {
started = true;
}
// Start the transmission // Start the transmission
processor.start(); processor.start();
@ -203,15 +204,17 @@ public class AudioChannel {
// Wait for it to configure // Wait for it to configure
boolean result = waitForState(processor, Processor.Configured); boolean result = waitForState(processor, Processor.Configured);
if (!result) if (!result){
return "Couldn't configure processor"; return "Couldn't configure processor";
}
// Get the tracks from the processor // Get the tracks from the processor
TrackControl[] tracks = processor.getTrackControls(); TrackControl[] tracks = processor.getTrackControls();
// Do we have atleast one track? // 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"; return "Couldn't find tracks in processor";
}
// Set the output content descriptor to RAW_RTP // Set the output content descriptor to RAW_RTP
// This will limit the supported formats reported from // 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 org.jivesoftware.smackx.jingle.JingleSession;
import java.util.Random; import java.util.Random;
import java.net.InetAddress; import java.util.Enumeration;
import java.net.UnknownHostException; import java.net.*;
/** /**
* Bridged Resolver use a RTPBridge Service to add a relayed candidate. * Bridged Resolver use a RTPBridge Service to add a relayed candidate.
* A very reliable solution for NAT Traversal. * A very reliable solution for NAT Traversal.
* * <p/>
* The resolver verify is the XMPP Server that the client is connected offer this service. * 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. * If the server supports, a candidate is requested from the service.
* The resolver adds this candidate * The resolver adds this candidate
*/ */
public class BridgedResolver extends TransportResolver{ public class BridgedResolver extends TransportResolver {
XMPPConnection connection; XMPPConnection connection;
@ -67,21 +67,7 @@ public class BridgedResolver extends TransportResolver{
RTPBridge rtpBridge = RTPBridge.getRTPBridge(connection, String.valueOf(sid)); RTPBridge rtpBridge = RTPBridge.getRTPBridge(connection, String.valueOf(sid));
String localIp = getLocalHost();
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();
}
TransportCandidate localCandidate = new TransportCandidate.Fixed( TransportCandidate localCandidate = new TransportCandidate.Fixed(
rtpBridge.getIp(), rtpBridge.getPortA()); rtpBridge.getIp(), rtpBridge.getPortA());
@ -124,4 +110,38 @@ public class BridgedResolver extends TransportResolver{
// Nothing to do here // 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(); network = iceNegociator.getPublicCandidate().getNetwork();
} }
else { else {
localIp = InetAddress.getLocalHost().getHostAddress(); localIp = BridgedResolver.getLocalHost();
network = 0; 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());
}
}