Skip to content

Commit

Permalink
enhance unit test and logging (#3374)
Browse files Browse the repository at this point in the history
* enhance unit test and logging

* enhance logging message

* fix unit test

* make code clean
  • Loading branch information
beiwei30 authored Jan 29, 2019
1 parent 58e35b2 commit d7e95b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,24 @@ public static String toURL(String protocol, String host, int port, String path)
}

public static void joinMulticastGroup (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException {
setInterface(multicastSocket, multicastAddress);
setInterface(multicastSocket, multicastAddress instanceof Inet6Address);
multicastSocket.setLoopbackMode(false);
multicastSocket.joinGroup(multicastAddress);
}

public static void setInterface (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException{
public static void setInterface (MulticastSocket multicastSocket, boolean preferIpv6) throws IOException{
boolean interfaceSet = false;
boolean ipV6 = multicastAddress instanceof Inet6Address;
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface i = (NetworkInterface) interfaces.nextElement();
Enumeration addresses = i.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = (InetAddress) addresses.nextElement();
if (ipV6 && address instanceof Inet6Address) {
if (preferIpv6 && address instanceof Inet6Address) {
multicastSocket.setInterface(address);
interfaceSet = true;
break;
} else if (!ipV6 && address instanceof Inet4Address) {
} else if (!preferIpv6 && address instanceof Inet4Address) {
multicastSocket.setInterface(address);
interfaceSet = true;
break;
Expand All @@ -376,4 +375,4 @@ public static void setInterface (MulticastSocket multicastSocket, InetAddress mu
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
Expand Down Expand Up @@ -81,9 +82,8 @@ public MulticastRegistry(URL url) {
}
try {
multicastAddress = InetAddress.getByName(url.getHost());
if (!multicastAddress.isMulticastAddress()) {
throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
}
checkMulticastAddress(multicastAddress);

multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : url.getPort();
multicastSocket = new MulticastSocket(multicastPort);
NetUtils.joinMulticastGroup(multicastSocket, multicastAddress);
Expand Down Expand Up @@ -132,6 +132,19 @@ public void run() {
}
}

private void checkMulticastAddress(InetAddress multicastAddress) {
if (!multicastAddress.isMulticastAddress()) {
String message = "Invalid multicast address " + multicastAddress;
if (!(multicastAddress instanceof Inet4Address)) {
throw new IllegalArgumentException(message + ", " +
"ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
} else {
throw new IllegalArgumentException(message + ", " + "ipv6 multicast address must start with ff, " +
"for example: ff01::1");
}
}
}

/**
* Remove the expired providers, only when "clean" parameter is true.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,34 +227,35 @@ public void testMulticastAddress() {
MulticastSocket multicastSocket = null;
try {
// ipv4 multicast address
try {
multicastAddress = InetAddress.getByName("224.55.66.77");
multicastSocket = new MulticastSocket(2345);
multicastSocket.setLoopbackMode(false);
NetUtils.setInterface(multicastSocket, multicastAddress);
multicastSocket.joinGroup(multicastAddress);
} finally {
if (multicastSocket != null) {
multicastSocket.close();
}
multicastAddress = InetAddress.getByName("224.55.66.77");
multicastSocket = new MulticastSocket(2345);
multicastSocket.setLoopbackMode(false);
NetUtils.setInterface(multicastSocket, false);
multicastSocket.joinGroup(multicastAddress);
} catch (Exception e) {
Assertions.fail(e);
} finally {
if (multicastSocket != null) {
multicastSocket.close();
}
}

// multicast ipv6 address,
/*try {
multicastAddress = InetAddress.getByName("ff01::1");
multicastSocket = new MulticastSocket();
multicastSocket.setLoopbackMode(false);
NetUtils.setInterface(multicastSocket, multicastAddress);
multicastSocket.joinGroup(multicastAddress);
} finally {
if (multicastSocket != null) {
multicastSocket.close();
}
}*/
// multicast ipv6 address,
try {
multicastAddress = InetAddress.getByName("ff01::1");

} catch (Exception e) {
Assertions.fail(e);
multicastSocket = new MulticastSocket();
multicastSocket.setLoopbackMode(false);
NetUtils.setInterface(multicastSocket, true);
multicastSocket.joinGroup(multicastAddress);
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (multicastSocket != null) {
multicastSocket.close();
}
}

}

}

0 comments on commit d7e95b4

Please sign in to comment.