|
| 1 | +package info.unterrainer.commons.udpobserver; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.DatagramPacket; |
| 5 | +import java.net.DatagramSocket; |
| 6 | +import java.net.InetAddress; |
| 7 | +import java.net.InterfaceAddress; |
| 8 | +import java.net.NetworkInterface; |
| 9 | +import java.net.SocketException; |
| 10 | +import java.net.UnknownHostException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Enumeration; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +import info.unterrainer.commons.udpobserver.exceptions.UdpSendException; |
| 18 | +import lombok.RequiredArgsConstructor; |
| 19 | +import lombok.extern.slf4j.Slf4j; |
| 20 | + |
| 21 | +@Slf4j |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class UdpSender { |
| 24 | + |
| 25 | + private final int senderPort; |
| 26 | + private DatagramSocket sendSocket; |
| 27 | + private List<InetAddress> broadcastAddresses; |
| 28 | + |
| 29 | + protected List<InetAddress> getBroadcastAddresses() { |
| 30 | + if (broadcastAddresses != null) |
| 31 | + return broadcastAddresses; |
| 32 | + |
| 33 | + try { |
| 34 | + broadcastAddresses = getAllBroadcastAddresses(); |
| 35 | + } catch (SocketException e) { |
| 36 | + return List.of(); |
| 37 | + } |
| 38 | + return broadcastAddresses; |
| 39 | + } |
| 40 | + |
| 41 | + protected DatagramSocket getSendSocket() { |
| 42 | + if (sendSocket == null) { |
| 43 | + try { |
| 44 | + sendSocket = new DatagramSocket(senderPort, InetAddress.getLocalHost()); |
| 45 | + } catch (SocketException | UnknownHostException e) { |
| 46 | + log.error("Error retrieving socket.", e); |
| 47 | + throw new UdpSendException(e); |
| 48 | + } |
| 49 | + try { |
| 50 | + sendSocket.setBroadcast(true); |
| 51 | + } catch (SocketException e) { |
| 52 | + log.error("Error setting broadcast-mode.", e); |
| 53 | + throw new UdpSendException(e); |
| 54 | + } |
| 55 | + } |
| 56 | + return sendSocket; |
| 57 | + } |
| 58 | + |
| 59 | + public void broadcast(final int targetPort, final String content) { |
| 60 | + broadcast(targetPort, content.getBytes(StandardCharsets.UTF_8)); |
| 61 | + } |
| 62 | + |
| 63 | + public void broadcast(final int targetPort, final byte[] bytes) { |
| 64 | + for (InetAddress address : getBroadcastAddresses()) |
| 65 | + try { |
| 66 | + send(address, targetPort, bytes); |
| 67 | + } catch (Exception e) { |
| 68 | + // NOOP |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public void send(final InetAddress targetAddress, final int targetPort, final String content) { |
| 73 | + send(targetAddress.getHostAddress(), targetPort, content); |
| 74 | + } |
| 75 | + |
| 76 | + public void send(final InetAddress targetAddress, final int targetPort, final byte[] bytes) { |
| 77 | + send(targetAddress.getHostAddress(), targetPort, bytes); |
| 78 | + } |
| 79 | + |
| 80 | + public void send(final String targetAddress, final int targetPort, final String content) { |
| 81 | + send(targetAddress, targetPort, content.getBytes(StandardCharsets.UTF_8)); |
| 82 | + } |
| 83 | + |
| 84 | + public void send(final String targetAddress, final int targetPort, final byte[] bytes) { |
| 85 | + DatagramPacket packet; |
| 86 | + try { |
| 87 | + packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(targetAddress), targetPort); |
| 88 | + } catch (UnknownHostException e) { |
| 89 | + log.error("Error creating new packet.", e); |
| 90 | + throw new UdpSendException(e); |
| 91 | + } |
| 92 | + try { |
| 93 | + getSendSocket().send(packet); |
| 94 | + } catch (IOException e) { |
| 95 | + log.error("Error sending packet.", e); |
| 96 | + throw new UdpSendException(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public List<InetAddress> getAllBroadcastAddresses() throws SocketException { |
| 101 | + List<InetAddress> broadcastList = new ArrayList<>(); |
| 102 | + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 103 | + while (interfaces.hasMoreElements()) { |
| 104 | + NetworkInterface networkInterface = interfaces.nextElement(); |
| 105 | + |
| 106 | + if (networkInterface.isLoopback() || !networkInterface.isUp()) |
| 107 | + continue; |
| 108 | + |
| 109 | + networkInterface.getInterfaceAddresses() |
| 110 | + .stream() |
| 111 | + .map(InterfaceAddress::getBroadcast) |
| 112 | + .filter(Objects::nonNull) |
| 113 | + .forEach(broadcastList::add); |
| 114 | + } |
| 115 | + return broadcastList; |
| 116 | + } |
| 117 | +} |
0 commit comments