Changeset 164 for trunk/sonorous

Show
Ignore:
Timestamp:
29/04/10 23:20:16 (4 months ago)
Author:
mbooth
Message:

Search on all available network interfaces.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/sonorous/uk.co.matbooth.upnp/src/uk/co/matbooth/upnp/discovery/SearchThread.java

    r163 r164  
    22 
    33import java.io.IOException; 
     4import java.net.Inet6Address; 
    45import java.net.InetAddress; 
     6import java.net.NetworkInterface; 
    57import java.net.SocketTimeoutException; 
    68import java.util.ArrayList; 
    79import java.util.Date; 
     10import java.util.Enumeration; 
    811import java.util.List; 
    912 
     
    1316 
    1417/** 
    15  * A thread that sends an SSDP search request from a socket bound to every network 
    16  * interface attached to the local host and listens for responses from UPnP devices 
    17  * announcing their presence on the network. 
     18 * A thread that sends an SSDP search request and listens for responses from UPnP devices 
     19 * announcing their presence on the network. A request will be sent to every network the 
     20 * host machine is connected to. 
    1821 */ 
    1922public class SearchThread extends DiscoveryThread { 
     
    3841 
    3942    /** 
    40      * TODO 
     43     * Creates the search thread. 
    4144     */ 
    4245    public SearchThread() { 
    43         // TODO create a socket for each network interface attached to this machine! 
    4446        try { 
    45             Socket sock = new Socket(InetAddress.getLocalHost().getHostAddress(), 0); 
    46             socks.add(sock); 
     47            // Create a socket for every address of every interface on the machine 
     48            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); 
     49            while (nis.hasMoreElements()) { 
     50                NetworkInterface ni = (NetworkInterface) nis.nextElement(); 
     51                Enumeration<InetAddress> addrs = ni.getInetAddresses(); 
     52                while (addrs.hasMoreElements()) { 
     53                    InetAddress addr = addrs.nextElement(); 
     54                    // TODO support IPv6 
     55                    if (addr.isLoopbackAddress() || addr instanceof Inet6Address) 
     56                        continue; 
     57                    Socket sock = new Socket(addr.getHostAddress(), 0); 
     58                    socks.add(sock); 
     59                } 
     60            } 
    4761        } catch (Exception e) { 
    4862            // If we can't create a socket for this network interface then just shrug it 
    4963            // off and don't bother 
     64            //TODO warn 
     65            e.printStackTrace(); 
    5066        } 
    5167    } 
     
    6985            int timeout = (MAX_TIME + ADDITIONAL_TIME) * SEARCH_ATTEMPTS * 1000; 
    7086            for (Socket sock : socks) { 
    71                 Thread t = new Thread(new SearchResponseThread(sock, timeout), "UPnP Discovery Search Response"); 
     87                Thread t = new Thread(new SearchResponseThread(sock, timeout), 
     88                        "UPnP Discovery Search Response"); 
    7289                t.setDaemon(true); 
    7390                t.start(); 
     
    121138                // TODO Auto-generated catch block 
    122139                e.printStackTrace(); 
     140            } finally { 
     141                sock.close(); 
    123142            } 
    124143        }