Merge branch 'refs/heads/master' of ssh://akwizgran@briar.git.sourceforge.net/gitroot/briar/prototype

This commit is contained in:
akwizgran
2011-11-08 13:15:05 +00:00
3 changed files with 11 additions and 45 deletions

View File

@@ -53,7 +53,7 @@ abstract class AbstractListener implements DiscoveryListener {
protected void findNestedClassIds(Object o, Collection<String> ids) { protected void findNestedClassIds(Object o, Collection<String> ids) {
o = getDataElementValue(o); o = getDataElementValue(o);
if(o instanceof Enumeration) { if(o instanceof Enumeration<?>) {
for(Object o1 : Collections.list((Enumeration<?>) o)) { for(Object o1 : Collections.list((Enumeration<?>) o)) {
findNestedClassIds(o1, ids); findNestedClassIds(o1, ids);
} }

View File

@@ -5,14 +5,10 @@ import java.net.DatagramPacket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
@@ -45,7 +41,7 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
// Bind a multicast socket for receiving packets // Bind a multicast socket for receiving packets
MulticastSocket ms = null; MulticastSocket ms = null;
try { try {
InetAddress iface = chooseMulticastInterface(); InetAddress iface = chooseInterface(true);
ms = new MulticastSocket(mcast.getPort()); ms = new MulticastSocket(mcast.getPort());
ms.setInterface(iface); ms.setInterface(iface);
ms.joinGroup(mcast.getAddress()); ms.joinGroup(mcast.getAddress());
@@ -135,38 +131,6 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
return b; return b;
} }
private InetAddress chooseMulticastInterface() throws IOException {
// Try to find a LAN interface that supports multicast
List<NetworkInterface> ifaces =
Collections.list(NetworkInterface.getNetworkInterfaces());
for(NetworkInterface iface : ifaces) {
if(iface.supportsMulticast()) {
Enumeration<InetAddress> addrs = iface.getInetAddresses();
for(InetAddress addr : Collections.list(addrs)) {
if(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Preferring " + addr.getHostAddress());
return addr;
}
}
}
}
// Settle for a WAN interface that supports multicast
for(NetworkInterface iface : ifaces) {
if(iface.supportsMulticast()) {
Enumeration<InetAddress> addrs = iface.getInetAddresses();
for(InetAddress addr : Collections.list(addrs)) {
if(!addr.isLoopbackAddress()) {
if(LOG.isLoggable(Level.INFO))
LOG.info("Accepting " + addr.getHostAddress());
return addr;
}
}
}
}
throw new IOException("No suitable interfaces for multicast");
}
private int parsePacket(byte[] b, int off, int len) { private int parsePacket(byte[] b, int off, int len) {
if(len != 2) return 0; if(len != 2) return 0;
return ByteUtils.readUint16(b, off); return ByteUtils.readUint16(b, off);
@@ -179,7 +143,7 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
// Bind a TCP socket for receiving connections // Bind a TCP socket for receiving connections
ServerSocket ss = null; ServerSocket ss = null;
try { try {
InetAddress iface = chooseTcpInterface(true); InetAddress iface = chooseInterface(true);
ss = new ServerSocket(); ss = new ServerSocket();
ss.bind(new InetSocketAddress(iface, 0)); ss.bind(new InetSocketAddress(iface, 0));
} catch(IOException e) { } catch(IOException e) {
@@ -197,7 +161,7 @@ public class LanSocketPlugin extends SimpleSocketPlugin {
// Bind a multicast socket for sending packets // Bind a multicast socket for sending packets
MulticastSocket ms = null; MulticastSocket ms = null;
try { try {
InetAddress iface = chooseMulticastInterface(); InetAddress iface = chooseInterface(true);
ms = new MulticastSocket(); ms = new MulticastSocket();
ms.setInterface(iface); ms.setInterface(iface);
} catch(IOException e) { } catch(IOException e) {

View File

@@ -65,7 +65,7 @@ class SimpleSocketPlugin extends SocketPlugin {
SocketAddress addr = createSocketAddress(callback.getLocalProperties()); SocketAddress addr = createSocketAddress(callback.getLocalProperties());
if(addr == null) { if(addr == null) {
try { try {
return new InetSocketAddress(chooseTcpInterface(false), 0); return new InetSocketAddress(chooseInterface(false), 0);
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage()); if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.getMessage());
} }
@@ -73,7 +73,7 @@ class SimpleSocketPlugin extends SocketPlugin {
return addr; return addr;
} }
protected InetAddress chooseTcpInterface(boolean lan) throws IOException { protected InetAddress chooseInterface(boolean lan) throws IOException {
List<NetworkInterface> ifaces = List<NetworkInterface> ifaces =
Collections.list(NetworkInterface.getNetworkInterfaces()); Collections.list(NetworkInterface.getNetworkInterfaces());
// Try to find an interface of the preferred type (LAN or WAN) // Try to find an interface of the preferred type (LAN or WAN)
@@ -84,7 +84,8 @@ class SimpleSocketPlugin extends SocketPlugin {
boolean site = addr.isSiteLocalAddress(); boolean site = addr.isSiteLocalAddress();
if(lan == (link || site)) { if(lan == (link || site)) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Preferring " + addr.getHostAddress()); LOG.info("Choosing interface " +
addr.getHostAddress());
return addr; return addr;
} }
} }
@@ -95,12 +96,13 @@ class SimpleSocketPlugin extends SocketPlugin {
for(InetAddress addr : Collections.list(iface.getInetAddresses())) { for(InetAddress addr : Collections.list(iface.getInetAddresses())) {
if(!addr.isLoopbackAddress()) { if(!addr.isLoopbackAddress()) {
if(LOG.isLoggable(Level.INFO)) if(LOG.isLoggable(Level.INFO))
LOG.info("Accepting " + addr.getHostAddress()); LOG.info("Accepting interface " +
addr.getHostAddress());
return addr; return addr;
} }
} }
} }
throw new IOException("No suitable interfaces for TCP"); throw new IOException("No suitable interfaces");
} }
@Override @Override