下面列出了java.net.NetworkInterface#supportsMulticast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static List<NetworkInterface> findNetworkInterfaces() throws SocketException {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
LOG.trace("findNetworkInterfaces checking interface: {}", ni);
if (ni.supportsMulticast() && ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia.getAddress() instanceof java.net.Inet4Address &&
!ia.getAddress().isLoopbackAddress() &&
!DEFAULT_EXCLUSIONS.contains(ni.getName())) {
// Add at the start, make usage order consistent with the
// existing ActiveMQ releases discovery will be used with.
interfaces.add(0, ni);
break;
}
}
}
}
LOG.trace("findNetworkInterfaces returning: {}", interfaces);
return interfaces;
}
private static NetworkInterface[] getNetworkInterfaces() {
final HashSet<NetworkInterface> list = new HashSet<>();
try {
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
final NetworkInterface next = interfaces.nextElement();
if (next.supportsMulticast() && next.isUp()) {
list.add(next);
}
}
} catch (final SocketException e) {
//Ignore
}
return list.toArray(new NetworkInterface[list.size()]);
}
/** format network interface flags */
private static String formatFlags(NetworkInterface nic) throws SocketException {
StringBuilder flags = new StringBuilder();
if (nic.isUp()) {
flags.append("UP ");
}
if (nic.supportsMulticast()) {
flags.append("MULTICAST ");
}
if (nic.isLoopback()) {
flags.append("LOOPBACK ");
}
if (nic.isPointToPoint()) {
flags.append("POINTOPOINT ");
}
if (nic.isVirtual()) {
flags.append("VIRTUAL ");
}
flags.append("mtu:" + nic.getMTU());
flags.append(" index:" + nic.getIndex());
return flags.toString();
}
/** format network interface flags */
private static String formatFlags(NetworkInterface nic) throws SocketException {
StringBuilder flags = new StringBuilder();
if (nic.isUp()) {
flags.append("UP ");
}
if (nic.supportsMulticast()) {
flags.append("MULTICAST ");
}
if (nic.isLoopback()) {
flags.append("LOOPBACK ");
}
if (nic.isPointToPoint()) {
flags.append("POINTOPOINT ");
}
if (nic.isVirtual()) {
flags.append("VIRTUAL ");
}
flags.append("mtu:").append(nic.getMTU());
flags.append(" index:").append(nic.getIndex());
return flags.toString();
}
@SuppressWarnings("JdkObsolete")
private boolean isMulticastEnabledIPv4Interface(NetworkInterface iface) {
try {
if (!iface.supportsMulticast() || !iface.isUp()) {
return false;
}
}
catch (SocketException se) {
return false;
}
// Suppressed "JdkObsolete", usage of Enumeration is deliberate
for (Enumeration<InetAddress> i = iface.getInetAddresses(); i.hasMoreElements(); ) {
InetAddress address = i.nextElement();
if (address.getClass() == Inet4Address.class) {
return true;
}
}
return false;
}
private static NetworkInterface[] getNetworkInterfaces() {
final HashSet<NetworkInterface> list = new HashSet<NetworkInterface>();
try {
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
final NetworkInterface next = interfaces.nextElement();
if (next.supportsMulticast() && next.isUp()) {
list.add(next);
}
}
} catch (SocketException e) {
//Ignore
}
return list.toArray(new NetworkInterface[list.size()]);
}
/**
* Create a new broadcaster
*
* @param address - multicast group address
* @param srcAddress - address of interface we should use to broadcast.
* @param port - udp port to use
* @param ttl - packet ttl
* @throws IOException
*/
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
throws IOException, JdpException {
this.addr = address;
this.port = port;
ProtocolFamily family = (address instanceof Inet6Address)
? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
channel = DatagramChannel.open(family);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
// with srcAddress equal to null, this constructor do exactly the same as
// if srcAddress is not passed
if (srcAddress != null) {
// User requests particular interface to bind to
NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
if (interf == null) {
throw new JdpException("Unable to get network interface for " + srcAddress.toString());
}
if (!interf.isUp()) {
throw new JdpException(interf.getName() + " is not up.");
}
if (!interf.supportsMulticast()) {
throw new JdpException(interf.getName() + " does not support multicast.");
}
try {
channel.bind(new InetSocketAddress(srcAddress, 0));
} catch (UnsupportedAddressTypeException ex) {
throw new JdpException("Unable to bind to source address");
}
channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
}
}
protected void startReceivers(NetworkInterface ni, String udpMulticast, boolean udpBroadcast) throws Exception {
if (ni == null || ni.isLoopback()) {
return;
}
List<InterfaceAddress> list = ni.getInterfaceAddresses();
if (list == null || list.isEmpty()) {
return;
}
if (udpMulticast != null && ni.supportsMulticast()) {
// Create multicast receiver
receivers.add(new UDPMulticastReceiver(nodeID, udpMulticast, transporter, ni));
}
if (udpBroadcast) {
for (InterfaceAddress ia : list) {
if (ia == null) {
continue;
}
InetAddress address = ia.getBroadcast();
if (address == null || address.isLoopbackAddress()) {
continue;
}
String udpAddress = address.getHostAddress();
if (udpAddress == null || udpAddress.isEmpty() || udpAddress.startsWith("127.")) {
continue;
}
// Create broadcast receiver
receivers.add(new UDPBroadcastReceiver(nodeID, udpAddress, transporter));
}
}
}
/**
* {@inheritDoc}
*
* @return <code>address</code> if <code>networkInterface</code>
* {@link NetworkInterface#supportsMulticast() supports multicast}.
*/
@Override
protected InetAddress isAcceptable(NetworkInterface networkInterface, InetAddress address) throws SocketException {
if( networkInterface.supportsMulticast() )
return address;
return null;
}
/**
* Creates an array of sockets with TTL and network interface set.
*
* @param mcastTTL multicast TTL.
* @return an array of multicast sockets to broadcast on.
* @throws IOException if I/O error occurred while creating a multicast socket.
* @noinspection SocketOpenedButNotSafelyClosed, ConstantConditions
*/
private static MulticastSocket[] createSockets(final int mcastTTL) throws IOException {
Exception lastException = null; // Records last error in case we could not create any sockets
final List<MulticastSocket> socketList = new ArrayList<MulticastSocket>(11);
final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
try {
final NetworkInterface netIf = enumeration.nextElement();
if (netIf.supportsMulticast()) {
final MulticastSocket socket = new MulticastSocket(); // NOPMD
socket.setTimeToLive(mcastTTL);
socket.setNetworkInterface(netIf);
socket.setSendBufferSize(SEND_BUFFER_SIZE);
socketList.add(socket);
}
} catch (final Exception e) {
lastException = e;
ExceptionUtils.ignoreException(e, "continue to connect to those we can");
}
}
if (socketList.isEmpty()) {
throw new IOException("Could not create at least one multicast socket. Last error: " + lastException);
}
return socketList.toArray(new MulticastSocket[socketList.size()]);
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
/**
* Validation of every discovered network interface.
* <p>
* Override this method to customize which network interfaces are used.
* </p>
* <p>
* The given implementation ignores interfaces which are
* </p>
* <ul>
* <li>loopback (yes, we do not bind to lo0)</li>
* <li>down</li>
* <li>have no bound IP addresses</li>
* <li>named "vmnet*" (OS X VMWare does not properly stop interfaces when it quits)</li>
* <li>named "vnic*" (OS X Parallels interfaces should be ignored as well)</li>
* <li>named "*virtual*" (VirtualBox interfaces, for example</li>
* <li>named "ppp*"</li>
* </ul>
*
* @param iface The interface to validate.
* @return True if the given interface matches all validation criteria.
* @throws Exception If any validation test failed with an un-recoverable error.
*/
protected boolean isUsableNetworkInterface(NetworkInterface iface) throws Exception {
if (!iface.isUp()) {
log.finer("Skipping network interface (down): " + iface.getDisplayName());
return false;
}
if (getInetAddresses(iface).size() == 0) {
log.finer("Skipping network interface without bound IP addresses: " + iface.getDisplayName());
return false;
}
if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("vmnet") ||
(iface.getDisplayName() != null && iface.getDisplayName().toLowerCase(Locale.ENGLISH).contains("vmnet"))) {
log.finer("Skipping network interface (VMWare): " + iface.getDisplayName());
return false;
}
if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("vnic")) {
log.finer("Skipping network interface (Parallels): " + iface.getDisplayName());
return false;
}
if (iface.getName().toLowerCase(Locale.ENGLISH).contains("virtual")) {
log.finer("Skipping network interface (named '*virtual*'): " + iface.getDisplayName());
return false;
}
if (iface.getName().toLowerCase(Locale.ENGLISH).startsWith("ppp")) {
log.finer("Skipping network interface (PPP): " + iface.getDisplayName());
return false;
}
if (iface.isLoopback()) {
log.finer("Skipping network interface (ignoring loopback): " + iface.getDisplayName());
return false;
}
if (useInterfaces.size() > 0 && !useInterfaces.contains(iface.getName())) {
log.finer("Skipping unwanted network interface (-D" + SYSTEM_PROPERTY_NET_IFACES + "): " + iface.getName());
return false;
}
if (!iface.supportsMulticast())
log.warning("Network interface may not be multicast capable: " + iface.getDisplayName());
return true;
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}
private static boolean isNetworkInterfaceTestable(NetworkInterface netIf) throws Exception {
System.out.println("checking netif == " + netIf.getName());
return (netIf.isUp() && netIf.supportsMulticast() && isIpAddrAvailable(netIf));
}