Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
guFalcon committed Sep 9, 2021
2 parents 2151ad8 + b0ea680 commit d39ea72
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/

.settings/

src/main/java/info/unterrainer/commons/udpobserver/Information.java
1 change: 1 addition & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.methodParameters=generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
org.eclipse.jdt.core.compiler.compliance=13
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>info.unterrainer.commons.udp-observer</groupId>
<artifactId>udp-observer</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
<name>UdpObserver</name>
<packaging>jar</packaging>

Expand Down

This file was deleted.

117 changes: 117 additions & 0 deletions src/main/java/info/unterrainer/commons/udpobserver/UdpSender.java
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;
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@

public class TestObserver {

/**
* 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);
UdpObserver observer = new UdpObserver(8001, 256, 1000);
observer.start();
System.out.println("listening for UDP packets...");
System.out.println("press <enter> to stop.");
Expand Down
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.");
}
}
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()));
}
}
1 change: 1 addition & 0 deletions start_test_observer.bat
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
1 change: 1 addition & 0 deletions start_test_observer.sh
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

0 comments on commit d39ea72

Please sign in to comment.