$search
00001 00004 package javax.jmdns.impl; 00005 00006 import java.lang.reflect.Method; 00007 import java.net.InetAddress; 00008 import java.net.NetworkInterface; 00009 import java.net.SocketException; 00010 import java.util.Enumeration; 00011 import java.util.HashSet; 00012 import java.util.Set; 00013 import java.util.logging.Level; 00014 import java.util.logging.Logger; 00015 00016 import javax.jmdns.NetworkTopologyDiscovery; 00017 00023 public class NetworkTopologyDiscoveryImpl implements NetworkTopologyDiscovery { 00024 private final static Logger logger = Logger.getLogger(NetworkTopologyDiscoveryImpl.class.getName()); 00025 00026 private final Method _isUp; 00027 00028 private final Method _supportsMulticast; 00029 00033 public NetworkTopologyDiscoveryImpl() { 00034 super(); 00035 Method isUp; 00036 try { 00037 isUp = NetworkInterface.class.getMethod("isUp", (Class<?>[]) null); 00038 } catch (Exception exception) { 00039 // We do not want to throw anything if the method does not exist. 00040 isUp = null; 00041 } 00042 _isUp = isUp; 00043 Method supportsMulticast; 00044 try { 00045 supportsMulticast = NetworkInterface.class.getMethod("supportsMulticast", (Class<?>[]) null); 00046 } catch (Exception exception) { 00047 // We do not want to throw anything if the method does not exist. 00048 supportsMulticast = null; 00049 } 00050 _supportsMulticast = supportsMulticast; 00051 } 00052 00053 /* 00054 * (non-Javadoc) 00055 * @see javax.jmdns.JmmDNS.NetworkTopologyDiscovery#getInetAddresses() 00056 */ 00057 @Override 00058 public InetAddress[] getInetAddresses() { 00059 Set<InetAddress> result = new HashSet<InetAddress>(); 00060 try { 00061 00062 for (Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces(); nifs.hasMoreElements();) { 00063 NetworkInterface nif = nifs.nextElement(); 00064 for (Enumeration<InetAddress> iaenum = nif.getInetAddresses(); iaenum.hasMoreElements();) { 00065 InetAddress interfaceAddress = iaenum.nextElement(); 00066 if (logger.isLoggable(Level.FINEST)) { 00067 logger.finest("Found NetworkInterface/InetAddress: " + nif + " -- " + interfaceAddress); 00068 } 00069 if (this.useInetAddress(nif, interfaceAddress)) { 00070 result.add(interfaceAddress); 00071 } 00072 } 00073 } 00074 } catch (SocketException se) { 00075 logger.warning("Error while fetching network interfaces addresses: " + se); 00076 } 00077 return result.toArray(new InetAddress[result.size()]); 00078 } 00079 00080 /* 00081 * (non-Javadoc) 00082 * @see javax.jmdns.JmmDNS.NetworkTopologyDiscovery#useInetAddress(java.net.NetworkInterface, java.net.InetAddress) 00083 */ 00084 @Override 00085 public boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress) { 00086 try { 00087 if (_isUp != null) { 00088 try { 00089 if (!((Boolean) _isUp.invoke(networkInterface, (Object[]) null)).booleanValue()) { 00090 return false; 00091 } 00092 } catch (Exception exception) { 00093 // We should hide that exception. 00094 } 00095 } 00096 if (_supportsMulticast != null) { 00097 try { 00098 if (!((Boolean) _supportsMulticast.invoke(networkInterface, (Object[]) null)).booleanValue()) { 00099 return false; 00100 } 00101 } catch (Exception exception) { 00102 // We should hide that exception. 00103 } 00104 } 00105 if (interfaceAddress.isLoopbackAddress()) { 00106 return false; 00107 } 00108 return true; 00109 } catch (Exception exception) { 00110 return false; 00111 } 00112 } 00113 00114 /* 00115 * (non-Javadoc) 00116 * @see javax.jmdns.NetworkTopologyDiscovery#lockInetAddress(java.net.InetAddress) 00117 */ 00118 @Override 00119 public void lockInetAddress(InetAddress interfaceAddress) { 00120 // Default implementation does nothing. 00121 } 00122 00123 /* 00124 * (non-Javadoc) 00125 * @see javax.jmdns.NetworkTopologyDiscovery#unlockInetAddress(java.net.InetAddress) 00126 */ 00127 @Override 00128 public void unlockInetAddress(InetAddress interfaceAddress) { 00129 // Default implementation does nothing. 00130 } 00131 00132 }