Skip to content

Commit

Permalink
[#104] Uses ZscriptAddress more
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Sep 13, 2023
1 parent 311a394 commit 3f3e2af
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.zscript.javaclient.connection.ResponseParser.ResponseHeader;
import net.zscript.model.components.Zchars;

public class CommandResponseQueue implements CommandResponseSystem {
public class CommandResponseQueue implements DeviceCommunications {
private static final int MAX_SENT = 10;

private final ResponseAddressingSystem addrSystem = new ResponseAddressingSystem(this);
Expand All @@ -35,12 +35,12 @@ private interface CommandEntry {

public CommandResponseQueue(ZscriptConnection connection) {
this.connection = connection;
connection.onReceive(resp -> {
ResponseHeader header = ResponseParser.parseResponseHeader(resp);
if (header.getAddr().length == 0) {
callback(resp, header.getEcho(), header.getType());
connection.onReceive(responseBytes -> {
ResponseHeader header = ResponseParser.parseResponseHeader(responseBytes);
if (header.hasAddress()) {
addrSystem.response(header.getAddress(), responseBytes);
} else {
addrSystem.response(header.getAddr(), resp);
callback(responseBytes, header.getEcho(), header.getType());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.zscript.javaclient.commandbuilder.CommandSeqElement;

/**
* Defines an entity which can take a zscript message in low-level form (byte[]) or high level (
* Defines an entity which can take a zscript message in low-level form (byte[]), with or without an address, or high level form of a SyntaxTree node (CommandSeqElement)
*/
public interface CommandResponseSystem {
public interface DeviceCommunications {
void send(CommandSeqElement seq);

void send(ZscriptAddress addr, byte[] data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import java.util.function.Consumer;

public class ResponseAddressingSystem {
private final CommandResponseSystem parent;
private final DeviceCommunications parent;

private final Map<ZscriptAddress, Consumer<byte[]>> addressResp = new HashMap<>();
private final Map<ZscriptAddress, ZscriptConnection> addressConnection = new HashMap<>();

public ResponseAddressingSystem(CommandResponseSystem parent) {
public ResponseAddressingSystem(DeviceCommunications parent) {
this.parent = parent;
}

Expand All @@ -28,8 +28,7 @@ public void onReceive(Consumer<byte[]> handler) {
});
}

public void response(int[] addr, byte[] received) {
addressResp.get(ZscriptAddress.from(addr)).accept(received);
public void response(ZscriptAddress address, byte[] received) {
addressResp.get(address).accept(received);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@

public class ResponseParser {
static class ResponseHeader {
private final int[] addr;
private final int type;
private final int echo;
private final ZscriptAddress address;
private final int type;
private final int echo;

public ResponseHeader(int[] addr, int type, int echo) {
this.addr = addr;
public ResponseHeader(ZscriptAddress address, int type, int echo) {
this.address = address;
this.type = type;
this.echo = echo;
}

public int[] getAddr() {
return addr;
public ZscriptAddress getAddress() {
return address;
}

public boolean hasAddress() {
return address.size() > 0;
}

public int getType() {
Expand Down Expand Up @@ -90,12 +94,7 @@ public static ResponseHeader parseResponseHeader(byte[] resp) {
break;
}
}
int[] addrArr = new int[addr.size()];
int i = 0;
for (int val : addr) {
addrArr[i++] = val;
}
return new ResponseHeader(addrArr, respType, echo);
return new ResponseHeader(ZscriptAddress.from(addr), respType, echo);
}

private static byte convertMarkers(byte encoded) {
Expand Down Expand Up @@ -129,13 +128,10 @@ public static void parseFullResponse(final CommandSeqElement command, final byte
final List<ReadToken> tokenAfterMarkers = new ArrayList<>();
boolean prevWasMarker = false;

final OptIterator<ReadToken> itEndSeq = reader.iterator();
for (Optional<ReadToken> opt = itEndSeq.next(); opt.isPresent(); opt = itEndSeq.next()) {
if (opt.get().getKey() != Zchars.Z_RESPONSE_MARK && opt.get().getKey() != Zchars.Z_ECHO) {
tokenAfterMarkers.add(opt.get());
break;
}
}
reader.iterator().stream()
.filter(tok -> tok.getKey() != Zchars.Z_RESPONSE_MARK && tok.getKey() != Zchars.Z_ECHO)
.findFirst()
.ifPresent(tokenAfterMarkers::add);

final OptIterator<ReadToken> it = reader.iterator();
for (Optional<ReadToken> opt = it.next(); opt.isPresent(); opt = it.next()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@
import static java.util.stream.Collectors.joining;

import java.util.Arrays;
import java.util.List;

/**
* Simple encapsulation of a Zscript address.
*/
public class ZscriptAddress {
private final int[] address;
private final int[] addressParts;

public static ZscriptAddress from(int... address) {
return new ZscriptAddress(address);
public static ZscriptAddress from(int... addressParts) {
return new ZscriptAddress(addressParts.clone());
}

public static ZscriptAddress from(List<Integer> addressParts) {
return new ZscriptAddress(addressParts.stream().mapToInt(i -> i).toArray());
}

private ZscriptAddress(int[] addr) {
this.address = addr.clone();
this.addressParts = addr;
}

public int[] getAsInts() {
return address.clone();
return addressParts.clone();
}

public int size() {
return addressParts.length;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(address);
result = prime * result + Arrays.hashCode(addressParts);
return result;
}

Expand All @@ -43,7 +52,7 @@ public boolean equals(Object obj) {
return false;
}
ZscriptAddress other = (ZscriptAddress) obj;
if (!Arrays.equals(address, other.address)) {
if (!Arrays.equals(addressParts, other.addressParts)) {
return false;
}
return true;
Expand All @@ -56,7 +65,7 @@ public boolean equals(Object obj) {
*/
@Override
public String toString() {
return stream(address)
return stream(addressParts)
.mapToObj(a -> a == 0 ? "" : Integer.toHexString(a))
.collect(joining(".", "@", ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Main2 {
public static void main(String[] args) {
CommandResponseSystem zscriptOut = new CommandResponseQueue(new LocalZscriptConnection());
DeviceCommunications zscriptOut = new CommandResponseQueue(new LocalZscriptConnection());

zscriptOut.send(CoreModule.capabilities()
.versionType(PlatformFirmware)
Expand All @@ -19,10 +19,10 @@ public static void main(String[] args) {
.addResponseListener(r -> System.out.println(r.isPreviousActivationState()))
.build()));

ZscriptAddress address = ZscriptAddress.from(0x50, 0x0, 0x1);
ZscriptConnection addressConnection = zscriptOut.getResponseAddressingSystem().getAddressConnection(address);
ZscriptAddress address = ZscriptAddress.from(0x50, 0x0, 0x1);
ZscriptConnection addressConnection = zscriptOut.getResponseAddressingSystem().getAddressConnection(address);

CommandResponseSystem zscriptOutAddr = new CommandResponseQueue(addressConnection);
DeviceCommunications zscriptOutAddr = new CommandResponseQueue(addressConnection);

// zscriptOutAddr.send(CoreModule.capabilities()
// .versionType(PlatformFirmware)
Expand Down

0 comments on commit 3f3e2af

Please sign in to comment.