Replace deprecated MulticastSocket methods (#14611)
* Replace deprecated methods * Call leaveGroup method in shutdown Signed-off-by: Mark Hilbush <mark@hilbush.com>
This commit is contained in:
parent
d4a231e8a9
commit
1983bc36f1
@ -17,11 +17,14 @@ import static org.openhab.binding.benqprojector.internal.BenqProjectorBindingCon
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -44,6 +47,7 @@ public class MulticastListener {
|
||||
private final Logger logger = LoggerFactory.getLogger(MulticastListener.class);
|
||||
|
||||
private MulticastSocket socket;
|
||||
private InetSocketAddress inetSocketAddress;
|
||||
|
||||
// BenQ projector devices announce themselves on the AMX DDD multicast port
|
||||
private static final String AMX_MULTICAST_GROUP = "239.255.250.250";
|
||||
@ -57,20 +61,25 @@ public class MulticastListener {
|
||||
*/
|
||||
public MulticastListener(String ipv4Address) throws IOException, SocketException {
|
||||
InetAddress ifAddress = InetAddress.getByName(ipv4Address);
|
||||
NetworkInterface netIF = NetworkInterface.getByInetAddress(ifAddress);
|
||||
NetworkInterface networkInterface = getMulticastInterface(ipv4Address);
|
||||
logger.debug("Discovery job using address {} on network interface {}", ifAddress.getHostAddress(),
|
||||
netIF != null ? netIF.getName() : "UNKNOWN");
|
||||
networkInterface.getName());
|
||||
socket = new MulticastSocket(AMX_MULTICAST_PORT);
|
||||
socket.setInterface(ifAddress);
|
||||
socket.setNetworkInterface(networkInterface);
|
||||
socket.setSoTimeout(DEFAULT_SOCKET_TIMEOUT_SEC);
|
||||
InetAddress mcastAddress = InetAddress.getByName(AMX_MULTICAST_GROUP);
|
||||
socket.joinGroup(mcastAddress);
|
||||
inetSocketAddress = new InetSocketAddress(InetAddress.getByName(AMX_MULTICAST_GROUP), AMX_MULTICAST_PORT);
|
||||
socket.joinGroup(inetSocketAddress, null);
|
||||
logger.debug("Multicast listener joined multicast group {}:{}", AMX_MULTICAST_GROUP, AMX_MULTICAST_PORT);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
logger.debug("Multicast listener closing down multicast socket");
|
||||
socket.close();
|
||||
try {
|
||||
socket.leaveGroup(inetSocketAddress, null);
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
logger.debug("Exception shutting down multicast socket: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -129,4 +138,25 @@ public class MulticastListener {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private NetworkInterface getMulticastInterface(String interfaceIpAddress) throws SocketException {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
NetworkInterface networkInterface;
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
networkInterface = networkInterfaces.nextElement();
|
||||
if (networkInterface.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found interface address {} -> {}", interfaceAddress.toString(),
|
||||
interfaceAddress.getAddress().toString());
|
||||
}
|
||||
if (interfaceAddress.getAddress().toString().endsWith("/" + interfaceIpAddress)) {
|
||||
return networkInterface;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new SocketException("Unable to get network interface for " + interfaceIpAddress);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,11 +17,14 @@ import static org.openhab.binding.epsonprojector.internal.EpsonProjectorBindingC
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@ -44,6 +47,7 @@ public class MulticastListener {
|
||||
private final Logger logger = LoggerFactory.getLogger(MulticastListener.class);
|
||||
|
||||
private MulticastSocket socket;
|
||||
private InetSocketAddress inetSocketAddress;
|
||||
|
||||
// Epson projector devices announce themselves on the AMX DDD multicast port
|
||||
private static final String AMX_MULTICAST_GROUP = "239.255.250.250";
|
||||
@ -57,20 +61,25 @@ public class MulticastListener {
|
||||
*/
|
||||
public MulticastListener(String ipv4Address) throws IOException, SocketException {
|
||||
InetAddress ifAddress = InetAddress.getByName(ipv4Address);
|
||||
NetworkInterface netIF = NetworkInterface.getByInetAddress(ifAddress);
|
||||
NetworkInterface networkInterface = getMulticastInterface(ipv4Address);
|
||||
logger.debug("Discovery job using address {} on network interface {}", ifAddress.getHostAddress(),
|
||||
netIF != null ? netIF.getName() : "UNKNOWN");
|
||||
networkInterface.getName());
|
||||
socket = new MulticastSocket(AMX_MULTICAST_PORT);
|
||||
socket.setInterface(ifAddress);
|
||||
socket.setNetworkInterface(networkInterface);
|
||||
socket.setSoTimeout(DEFAULT_SOCKET_TIMEOUT_SEC);
|
||||
InetAddress mcastAddress = InetAddress.getByName(AMX_MULTICAST_GROUP);
|
||||
socket.joinGroup(mcastAddress);
|
||||
inetSocketAddress = new InetSocketAddress(InetAddress.getByName(AMX_MULTICAST_GROUP), AMX_MULTICAST_PORT);
|
||||
socket.joinGroup(inetSocketAddress, null);
|
||||
logger.debug("Multicast listener joined multicast group {}:{}", AMX_MULTICAST_GROUP, AMX_MULTICAST_PORT);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
logger.debug("Multicast listener closing down multicast socket");
|
||||
socket.close();
|
||||
try {
|
||||
socket.leaveGroup(inetSocketAddress, null);
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
logger.debug("Exception shutting down multicast socket: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -129,4 +138,25 @@ public class MulticastListener {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private NetworkInterface getMulticastInterface(String interfaceIpAddress) throws SocketException {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
NetworkInterface networkInterface;
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
networkInterface = networkInterfaces.nextElement();
|
||||
if (networkInterface.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found interface address {} -> {}", interfaceAddress.toString(),
|
||||
interfaceAddress.getAddress().toString());
|
||||
}
|
||||
if (interfaceAddress.getAddress().toString().endsWith("/" + interfaceIpAddress)) {
|
||||
return networkInterface;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new SocketException("Unable to get network interface for " + interfaceIpAddress);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,11 +17,15 @@ import static org.openhab.binding.globalcache.internal.GlobalCacheBindingConstan
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.slf4j.Logger;
|
||||
@ -37,6 +41,7 @@ public class MulticastListener {
|
||||
private final Logger logger = LoggerFactory.getLogger(MulticastListener.class);
|
||||
|
||||
private MulticastSocket socket;
|
||||
private InetSocketAddress inetSocketAddress;
|
||||
|
||||
private String serialNumber = "";
|
||||
private String vendor = "";
|
||||
@ -59,24 +64,29 @@ public class MulticastListener {
|
||||
public static final int DEFAULT_SOCKET_TIMEOUT = 3000;
|
||||
|
||||
/*
|
||||
* Constructor joins the multicast group, throws IOException on failure.
|
||||
* Constructor joins the multicast group
|
||||
*/
|
||||
public MulticastListener(String ipv4Address) throws IOException, SocketException {
|
||||
public MulticastListener(String ipv4Address) throws IOException, SocketException, UnknownHostException {
|
||||
InetAddress ifAddress = InetAddress.getByName(ipv4Address);
|
||||
NetworkInterface netIF = NetworkInterface.getByInetAddress(ifAddress);
|
||||
NetworkInterface networkInterface = getMulticastInterface(ipv4Address);
|
||||
logger.debug("Discovery job using address {} on network interface {}", ifAddress.getHostAddress(),
|
||||
netIF != null ? netIF.getName() : "UNKNOWN");
|
||||
networkInterface.getName());
|
||||
socket = new MulticastSocket(GC_MULTICAST_PORT);
|
||||
socket.setInterface(ifAddress);
|
||||
socket.setNetworkInterface(networkInterface);
|
||||
socket.setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
|
||||
InetAddress mcastAddress = InetAddress.getByName(GC_MULTICAST_GROUP);
|
||||
socket.joinGroup(mcastAddress);
|
||||
inetSocketAddress = new InetSocketAddress(InetAddress.getByName(GC_MULTICAST_GROUP), GC_MULTICAST_PORT);
|
||||
socket.joinGroup(inetSocketAddress, null);
|
||||
logger.debug("Multicast listener joined multicast group {}:{}", GC_MULTICAST_GROUP, GC_MULTICAST_PORT);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
logger.debug("Multicast listener closing down multicast socket");
|
||||
socket.close();
|
||||
try {
|
||||
socket.leaveGroup(inetSocketAddress, null);
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
logger.debug("Exception shutting down multicast socket: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -243,6 +253,27 @@ public class MulticastListener {
|
||||
macAddress = "";
|
||||
}
|
||||
|
||||
private NetworkInterface getMulticastInterface(String interfaceIpAddress) throws SocketException {
|
||||
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
NetworkInterface networkInterface;
|
||||
while (networkInterfaces.hasMoreElements()) {
|
||||
networkInterface = networkInterfaces.nextElement();
|
||||
if (networkInterface.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Found interface address {} -> {}", interfaceAddress.toString(),
|
||||
interfaceAddress.getAddress().toString());
|
||||
}
|
||||
if (interfaceAddress.getAddress().toString().endsWith("/" + interfaceIpAddress)) {
|
||||
return networkInterface;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new SocketException("Unable to get network interface for " + interfaceIpAddress);
|
||||
}
|
||||
|
||||
public String getSerialNumber() {
|
||||
return serialNumber;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user