-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/main/java/info/unterrainer/commons/udpobserver/Information.java
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
src/main/java/info/unterrainer/commons/udpobserver/UdpSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package info.unterrainer.commons.udpobserver; | ||
|
||
import java.io.IOException; | ||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.InetAddress; | ||
import java.net.InterfaceAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import info.unterrainer.commons.udpobserver.exceptions.UdpSendException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class UdpSender { | ||
|
||
private final int senderPort; | ||
private DatagramSocket sendSocket; | ||
private List<InetAddress> broadcastAddresses; | ||
|
||
protected List<InetAddress> getBroadcastAddresses() { | ||
if (broadcastAddresses != null) | ||
return broadcastAddresses; | ||
|
||
try { | ||
broadcastAddresses = getAllBroadcastAddresses(); | ||
} catch (SocketException e) { | ||
return List.of(); | ||
} | ||
return broadcastAddresses; | ||
} | ||
|
||
protected DatagramSocket getSendSocket() { | ||
if (sendSocket == null) { | ||
try { | ||
sendSocket = new DatagramSocket(senderPort, InetAddress.getLocalHost()); | ||
} catch (SocketException | UnknownHostException e) { | ||
log.error("Error retrieving socket.", e); | ||
throw new UdpSendException(e); | ||
} | ||
try { | ||
sendSocket.setBroadcast(true); | ||
} catch (SocketException e) { | ||
log.error("Error setting broadcast-mode.", e); | ||
throw new UdpSendException(e); | ||
} | ||
} | ||
return sendSocket; | ||
} | ||
|
||
public void broadcast(final int targetPort, final String content) { | ||
broadcast(targetPort, content.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
public void broadcast(final int targetPort, final byte[] bytes) { | ||
for (InetAddress address : getBroadcastAddresses()) | ||
try { | ||
send(address, targetPort, bytes); | ||
} catch (Exception e) { | ||
// NOOP | ||
} | ||
} | ||
|
||
public void send(final InetAddress targetAddress, final int targetPort, final String content) { | ||
send(targetAddress.getHostAddress(), targetPort, content); | ||
} | ||
|
||
public void send(final InetAddress targetAddress, final int targetPort, final byte[] bytes) { | ||
send(targetAddress.getHostAddress(), targetPort, bytes); | ||
} | ||
|
||
public void send(final String targetAddress, final int targetPort, final String content) { | ||
send(targetAddress, targetPort, content.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
public void send(final String targetAddress, final int targetPort, final byte[] bytes) { | ||
DatagramPacket packet; | ||
try { | ||
packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(targetAddress), targetPort); | ||
} catch (UnknownHostException e) { | ||
log.error("Error creating new packet.", e); | ||
throw new UdpSendException(e); | ||
} | ||
try { | ||
getSendSocket().send(packet); | ||
} catch (IOException e) { | ||
log.error("Error sending packet.", e); | ||
throw new UdpSendException(e); | ||
} | ||
} | ||
|
||
public List<InetAddress> getAllBroadcastAddresses() throws SocketException { | ||
List<InetAddress> broadcastList = new ArrayList<>(); | ||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); | ||
while (interfaces.hasMoreElements()) { | ||
NetworkInterface networkInterface = interfaces.nextElement(); | ||
|
||
if (networkInterface.isLoopback() || !networkInterface.isUp()) | ||
continue; | ||
|
||
networkInterface.getInterfaceAddresses() | ||
.stream() | ||
.map(InterfaceAddress::getBroadcast) | ||
.filter(Objects::nonNull) | ||
.forEach(broadcastList::add); | ||
} | ||
return broadcastList; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/info/unterrainer/commons/udpobserver/exceptions/UdpSendException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package info.unterrainer.commons.udpobserver.exceptions; | ||
|
||
public class UdpSendException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 2243163797766428122L; | ||
|
||
public UdpSendException(final Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/info/unterrainer/commons/udpobserver/scripts/TestObserverSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package info.unterrainer.commons.udpobserver.scripts; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.util.List; | ||
|
||
import info.unterrainer.commons.udpobserver.UdpSender; | ||
|
||
public class TestObserverSender { | ||
|
||
public static void main(final String[] args) throws InterruptedException, IOException { | ||
UdpSender sender = new UdpSender(8002); | ||
|
||
List<InetAddress> broadcastAddresses = sender.getAllBroadcastAddresses(); | ||
System.out.println("found broadcast addresses:"); | ||
for (InetAddress address : broadcastAddresses) | ||
System.out.println(" - " + address.getHostAddress()); | ||
|
||
System.out.println("sending UDP packets to port 8001..."); | ||
System.out.println("press <enter> to stop."); | ||
while (System.in.available() == 0) { | ||
sender.broadcast(8001, "TestPacket!"); | ||
|
||
System.out.println("Packet sent."); | ||
Thread.sleep(1000L); | ||
} | ||
System.out.println("done."); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/info/unterrainer/commons/udpobserver/scripts/TestObserverShelly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package info.unterrainer.commons.udpobserver.scripts; | ||
|
||
import java.io.IOException; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Collection; | ||
|
||
import info.unterrainer.commons.udpobserver.UdpDatagram; | ||
import info.unterrainer.commons.udpobserver.UdpObserver; | ||
|
||
public class TestObserverShelly { | ||
|
||
/** | ||
* STA:{"type":"ENOCEAN","adr":"fef2b30d","data":"eltako_button4","vendor":"eltako","state":{"BI":"pressed","BO":"released","AO":"released","AI":"released"}} | ||
*/ | ||
private static DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; | ||
|
||
public static void main(final String[] args) throws InterruptedException, IOException { | ||
UdpObserver observer = new UdpObserver(1901, 256, 1000); | ||
observer.start(); | ||
System.out.println("listening for UDP packets..."); | ||
System.out.println("press <enter> to stop."); | ||
while (System.in.available() == 0) { | ||
Thread.sleep(100L); | ||
outputList(observer.getReceivedDatagrams()); | ||
} | ||
observer.close(); | ||
System.out.println("done."); | ||
} | ||
|
||
private static void outputList(final Collection<UdpDatagram> list) { | ||
for (UdpDatagram data : list) | ||
System.out.println( | ||
String.format("[%s] %s", data.getMetaData().getTimestamp().format(formatter), data.getContent())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
java -cp "target/libs/*;target/classes" info.unterrainer.commons.udpobserver.scripts.TestObserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
java -cp "target/libs/*;target/classes" info.unterrainer.commons.udpobserver.scripts.TestObserver |