From ce9e44e2f211268f2dea52e7638d6a5c547e1e65 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 1 Apr 2020 16:12:50 +0200 Subject: [PATCH] [Major] Moved Packet, PacketObserver, PacketState to strolch utils --- .../strolch/utils/communication/Packet.java | 126 ++++++++++++++++++ .../utils/communication/PacketObserver.java | 16 +++ .../utils/communication/PacketState.java | 10 ++ 3 files changed, 152 insertions(+) create mode 100644 li.strolch.utils/src/main/java/li/strolch/utils/communication/Packet.java create mode 100644 li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketObserver.java create mode 100644 li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketState.java diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/communication/Packet.java b/li.strolch.utils/src/main/java/li/strolch/utils/communication/Packet.java new file mode 100644 index 000000000..49fd76e90 --- /dev/null +++ b/li.strolch.utils/src/main/java/li/strolch/utils/communication/Packet.java @@ -0,0 +1,126 @@ +package li.strolch.utils.communication; + +import static li.strolch.utils.helper.StringHelper.toHexString; + +import com.google.gson.JsonObject; +import li.strolch.utils.helper.StringHelper; + +public class Packet { + + private final long timestamp; + private final String command; + private byte position; + private PacketState packetState; + private byte[] sent; + private byte[] received; + + public Packet(String command, byte position) { + this(System.currentTimeMillis(), command, position); + } + + public Packet(long timestamp, String command, byte position) { + this.timestamp = timestamp; + this.command = command; + this.position = position; + this.packetState = PacketState.New; + } + + public long getTimestamp() { + return this.timestamp; + } + + public String getCommand() { + return this.command; + } + + public byte getPosition() { + return this.position; + } + + public void setPosition(byte position) { + this.position = position; + } + + public PacketState getState() { + return this.packetState; + } + + public void setState(PacketState packetState) { + this.packetState = packetState; + } + + public Packet state(PacketState packetState) { + this.packetState = packetState; + return this; + } + + public byte[] getSent() { + return this.sent; + } + + public void setSent(byte[] sent) { + this.sent = sent; + } + + public Packet sent(byte[] sent) { + this.sent = sent; + return this; + } + + public byte[] getReceived() { + return this.received; + } + + public Packet received(byte[] received) { + this.received = received; + return this; + } + + @Override + public String toString() { + return "Packet{" + "timestamp=" + this.timestamp // + + ", command='" + this.command + '\'' // + + ", position=" + this.position // + + ", packetState=" + this.packetState // + + ", sent=" + (this.sent == null ? "[]" : toHexString(this.sent)) // + + ", received=" + (this.received == null ? "[]" : toHexString(this.received)) + '}'; + } + + public JsonObject toJson() { + JsonObject jsonObject = new JsonObject(); + + jsonObject.addProperty("time", this.timestamp); + jsonObject.addProperty("msgType", "Packet"); + jsonObject.addProperty("command", this.command); + jsonObject.addProperty("position", this.position); + jsonObject.addProperty("packetState", this.packetState.name()); + if (this.sent != null) + jsonObject.addProperty("sent", toHexString(this.sent)); + if (this.received != null) + jsonObject.addProperty("received", toHexString(this.received)); + + return jsonObject; + } + + public static Packet valueOf(JsonObject jsonObject) { + + long time = jsonObject.get("time").getAsLong(); + String command = jsonObject.get("command").getAsString(); + byte position = jsonObject.get("position").getAsByte(); + PacketState packetState = PacketState.valueOf(jsonObject.get("packetState").getAsString()); + + Packet packet = new Packet(time, command, position); + packet.setState(packetState); + + if (jsonObject.has("sent")) { + byte[] sent = StringHelper.fromHexString(jsonObject.get("sent").getAsString()); + packet.setSent(sent); + } + if (jsonObject.has("received")) { + byte[] received = StringHelper.fromHexString(jsonObject.get("received").getAsString()); + packet.received(received); + } + + return packet; + } +} diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketObserver.java b/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketObserver.java new file mode 100644 index 000000000..72c9daca9 --- /dev/null +++ b/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketObserver.java @@ -0,0 +1,16 @@ +package li.strolch.utils.communication; + +public interface PacketObserver { + + void notify(Packet packet); + + void notifySent(byte[] sent); + + void notifyReceived(byte[] received); + + void notifyInfo(String msg); + + void notifyError(String msg); + + void notifyError(String msg, Throwable e); +} diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketState.java b/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketState.java new file mode 100644 index 000000000..f6fc7f07a --- /dev/null +++ b/li.strolch.utils/src/main/java/li/strolch/utils/communication/PacketState.java @@ -0,0 +1,10 @@ +package li.strolch.utils.communication; + +public enum PacketState { + New, + Sent, + Failed, + NoResponse, + Retry, + Done +} \ No newline at end of file