Search in sources :

Example 1 with DiscoveryTest

use of de.javawi.jstun.test.DiscoveryTest in project Smack by igniterealtime.

the class STUNResolver method initialize.

/**
     * Initialize the resolver.
     *
     * @throws XMPPException
     */
@Override
public void initialize() throws XMPPException {
    LOGGER.fine("Initialized");
    if (!isResolving() && !isResolved()) {
        // Get the best STUN server available
        if (currentServer.isNull()) {
            loadSTUNServers();
        }
        // We should have a valid STUN server by now...
        if (!currentServer.isNull()) {
            clearCandidates();
            resolverThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    // to the STUN server for our address.
                    try {
                        Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
                        String candAddress;
                        int candPort;
                        while (ifaces.hasMoreElements()) {
                            NetworkInterface iface = ifaces.nextElement();
                            Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
                            while (iaddresses.hasMoreElements()) {
                                InetAddress iaddress = iaddresses.nextElement();
                                if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
                                    // Reset the candidate
                                    candAddress = null;
                                    candPort = -1;
                                    DiscoveryTest test = new DiscoveryTest(iaddress, currentServer.getHostname(), currentServer.getPort());
                                    try {
                                        // Run the tests and get the
                                        // discovery
                                        // information, where all the
                                        // info is stored...
                                        DiscoveryInfo di = test.test();
                                        candAddress = di.getPublicIP() != null ? di.getPublicIP().getHostAddress() : null;
                                        // Get a valid port
                                        if (defaultPort == 0) {
                                            candPort = getFreePort();
                                        } else {
                                            candPort = defaultPort;
                                        }
                                        // add it to the list.
                                        if (candAddress != null && candPort >= 0) {
                                            TransportCandidate candidate = new TransportCandidate.Fixed(candAddress, candPort);
                                            candidate.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
                                            addCandidate(candidate);
                                            resolvedPublicIP = candidate.getIp();
                                            resolvedLocalIP = candidate.getLocalIp();
                                            return;
                                        }
                                    } catch (Exception e) {
                                        LOGGER.log(Level.SEVERE, "Exception", e);
                                    }
                                }
                            }
                        }
                    } catch (SocketException e) {
                        LOGGER.log(Level.SEVERE, "Exception", e);
                    } finally {
                        setInitialized();
                    }
                }
            }, "Waiting for all the transport candidates checks...");
            resolverThread.setName("STUN resolver");
            resolverThread.start();
        } else {
            throw new IllegalStateException("No valid STUN server found.");
        }
    }
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) SocketException(java.net.SocketException) XMPPException(org.jivesoftware.smack.XMPPException) DiscoveryInfo(de.javawi.jstun.test.DiscoveryInfo) DiscoveryTest(de.javawi.jstun.test.DiscoveryTest) InetAddress(java.net.InetAddress)

Aggregations

DiscoveryInfo (de.javawi.jstun.test.DiscoveryInfo)1 DiscoveryTest (de.javawi.jstun.test.DiscoveryTest)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1