Skip to content

Commit

Permalink
[#72] Zcode filenames to Zscript
Browse files Browse the repository at this point in the history
  • Loading branch information
susanw1 committed Aug 1, 2023
1 parent 7fc0c65 commit cbe61cc
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import java.util.Queue;
import java.util.function.Consumer;

import net.zscript.javaclient.responseParser.ZcodeResponseParser.ResponseHeader;
import net.zscript.javaclient.responseParser.ResponseParser.ResponseHeader;
import net.zscript.javaclient.zcodeApi.CommandSeqElement;
import net.zscript.javaclient.zcodeApi.ZcodeCommandBuilder;
import net.zscript.javaclient.zcodeApi.ZscriptCommandBuilder;

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

private final ZcodeResponseAddressingSystem addrSystem = new ZcodeResponseAddressingSystem(this);
private final ResponseAddressingSystem addrSystem = new ResponseAddressingSystem(this);

private ZcodeConnection connection;
private ZscriptConnection connection;

private interface CommandEntry {
byte[] compile();
Expand All @@ -37,7 +37,7 @@ public CommandSeqElEntry(final CommandSeqElement cmdSeq, final int echo) {
@Override
public byte[] compile() {
// TODO: decide on how locking will work...
byte[] echoF = ZcodeCommandBuilder.writeField((byte) '_', echo);
byte[] echoF = ZscriptCommandBuilder.writeField((byte) '_', echo);
byte[] startData = cmdSeq.compile(false);

ByteArrayOutputStream str = new ByteArrayOutputStream(startData.length + echoF.length + 1);
Expand All @@ -52,7 +52,7 @@ public byte[] compile() {
}

public void callback(final byte[] received) {
ZcodeResponseParser.parseFullResponse(cmdSeq, received);
ResponseParser.parseFullResponse(cmdSeq, received);
}

public int getEcho() {
Expand All @@ -67,9 +67,9 @@ public boolean canBePipelined() {

private class AddrCommandSeqElEntry implements CommandEntry {
private final byte[] cmdSeq;
private final ZcodeAddress addr;
private final ZscriptAddress addr;

public AddrCommandSeqElEntry(final byte[] cmdSeq, final ZcodeAddress addr) {
public AddrCommandSeqElEntry(final byte[] cmdSeq, final ZscriptAddress addr) {
this.cmdSeq = cmdSeq;
this.addr = addr;
}
Expand All @@ -81,7 +81,7 @@ public byte[] compile() {
try {
boolean isFirst = true;
for (int i : addr.getAddr()) {
str.write(ZcodeCommandBuilder.writeField((byte) (isFirst ? '@' : '.'), i));
str.write(ZscriptCommandBuilder.writeField((byte) (isFirst ? '@' : '.'), i));
isFirst = false;
}
str.write(cmdSeq);
Expand Down Expand Up @@ -127,10 +127,10 @@ public boolean canBePipelined() {
private int currentEcho = 0;
private boolean canPipeline = true;

public ZcodeCommandResponseQueue(ZcodeConnection connection) {
public CommandResponseQueue(ZscriptConnection connection) {
this.connection = connection;
connection.onReceive(resp -> {
ResponseHeader header = ZcodeResponseParser.parseResponseHeader(resp);
ResponseHeader header = ResponseParser.parseResponseHeader(resp);
if (header.getAddr().length == 0) {
callback(resp, header.getEcho(), header.getType());
} else {
Expand All @@ -140,7 +140,7 @@ public ZcodeCommandResponseQueue(ZcodeConnection connection) {
}

@Override
public void send(final ZcodeAddress addr, final byte[] data) {
public void send(final ZscriptAddress addr, final byte[] data) {
if (sent.size() < MAX_SENT && canPipeline) {
AddrCommandSeqElEntry el = new AddrCommandSeqElEntry(data, addr);
sent.add(el);
Expand Down Expand Up @@ -211,8 +211,8 @@ private void callback(final byte[] response, int echo, int respType) {
}

@Override
public ZcodeResponseAddressingSystem getResponseAddressingSystem() {
return new ZcodeResponseAddressingSystem(this);
public ResponseAddressingSystem getResponseAddressingSystem() {
return new ResponseAddressingSystem(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import net.zscript.javaclient.zcodeApi.CommandSeqElement;

public interface ZcodeCommandResponseSystem {
public interface CommandResponseSystem {
void send(CommandSeqElement seq);

void send(ZcodeAddress addr, byte[] data);
void send(ZscriptAddress addr, byte[] data);

void send(byte[] zcode, Consumer<byte[]> callback);

ZcodeResponseAddressingSystem getResponseAddressingSystem();
ResponseAddressingSystem getResponseAddressingSystem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
import java.util.Map;
import java.util.function.Consumer;

public class ZcodeResponseAddressingSystem {
private final ZcodeCommandResponseSystem parent;
private final Map<ZcodeAddress, Consumer<byte[]>> addressResp = new HashMap<>();
private final Map<ZcodeAddress, ZcodeConnection> addressConnection = new HashMap<>();
public class ResponseAddressingSystem {
private final CommandResponseSystem parent;
private final Map<ZscriptAddress, Consumer<byte[]>> addressResp = new HashMap<>();
private final Map<ZscriptAddress, ZscriptConnection> addressConnection = new HashMap<>();

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

public ZcodeConnection getAddressConnection(ZcodeAddress addr) {
return addressConnection.computeIfAbsent(addr, a -> new ZcodeConnection() {
public ZscriptConnection getAddressConnection(ZscriptAddress addr) {
return addressConnection.computeIfAbsent(addr, a -> new ZscriptConnection() {
@Override
public void send(byte[] data) {
parent.send(addr, data);
Expand All @@ -28,7 +28,7 @@ public void onReceive(Consumer<byte[]> handler) {
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
import java.util.Set;

import net.zscript.javaclient.zcodeApi.CommandSeqElement;
import net.zscript.javaclient.zcodeApi.ZcodeCommand;
import net.zscript.javaclient.zcodeApi.ZcodeUnparsedCommandResponse;
import net.zscript.javaclient.zcodeApi.ZcodeCommand.ZcodeSequencePath;
import net.zscript.javaclient.zcodeApi.ZscriptCommand;
import net.zscript.javaclient.zcodeApi.ZscriptUnparsedCommandResponse;
import net.zscript.javaclient.zcodeApi.ZscriptCommand.ZcodeSequencePath;
import net.zscript.javareceiver.tokenizer.OptIterator;
import net.zscript.javareceiver.tokenizer.TokenBuffer;
import net.zscript.javareceiver.tokenizer.TokenExtendingBuffer;
import net.zscript.javareceiver.tokenizer.Tokenizer;
import net.zscript.javareceiver.tokenizer.TokenBuffer.TokenReader;
import net.zscript.javareceiver.tokenizer.TokenBuffer.TokenReader.ReadToken;

public class ZcodeResponseParser {
public class ResponseParser {
static class ResponseHeader {
private final int[] addr;
private final int type;
Expand Down Expand Up @@ -146,10 +146,10 @@ public static void parseFullResponse(final CommandSeqElement command, final byte
}

private static void matchMarkers(final CommandSeqElement command, final List<Byte> markers, final List<ReadToken> tokenAfterMarkers) {
ZcodeCommand current = null;
ZcodeSequencePath successPath = ZcodeCommand.findFirstCommand(command);
ZcodeSequencePath failPath = ZcodeCommand.findFirstCommand(command);
Set<ZcodeCommand> sentResponses = new HashSet<>();
ZscriptCommand current = null;
ZcodeSequencePath successPath = ZscriptCommand.findFirstCommand(command);
ZcodeSequencePath failPath = ZscriptCommand.findFirstCommand(command);
Set<ZscriptCommand> sentResponses = new HashSet<>();

int offset = 0;

Expand Down Expand Up @@ -196,11 +196,11 @@ private static void matchMarkers(final CommandSeqElement command, final List<Byt
}
successPath = current.findSuccessPath();
failPath = current.findFailPath();
current.response(new ZcodeUnparsedCommandResponse(tokenAfterMarkers.get(offset)));
current.response(new ZscriptUnparsedCommandResponse(tokenAfterMarkers.get(offset)));
sentResponses.add(current);
}
for (ZcodeSequencePath path = ZcodeCommand.findFirstCommand(command); path != null; path = path.getNext().findNext()) {
ZcodeCommand cmd = path.getNext();
for (ZcodeSequencePath path = ZscriptCommand.findFirstCommand(command); path != null; path = path.getNext().findNext()) {
ZscriptCommand cmd = path.getNext();
if (!sentResponses.contains(cmd)) {
cmd.notExecuted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import java.util.Arrays;

public class ZcodeAddress {
public class ZscriptAddress {
private final int[] addr;

public static ZcodeAddress from(int addr0) {
return new ZcodeAddress(new int[] { addr0 });
public static ZscriptAddress from(int addr0) {
return new ZscriptAddress(new int[] { addr0 });
}

public static ZcodeAddress from(int addr0, int addr1) {
return new ZcodeAddress(new int[] { addr0, addr1 });
public static ZscriptAddress from(int addr0, int addr1) {
return new ZscriptAddress(new int[] { addr0, addr1 });
}

public static ZcodeAddress from(int addr0, int addr1, int addr2) {
return new ZcodeAddress(new int[] { addr0, addr1, addr2 });
public static ZscriptAddress from(int addr0, int addr1, int addr2) {
return new ZscriptAddress(new int[] { addr0, addr1, addr2 });
}

public ZcodeAddress(int[] addr) {
public ZscriptAddress(int[] addr) {
this.addr = addr;
}

Expand All @@ -41,7 +41,7 @@ public boolean equals(Object obj) {
return false;
if (getClass() != obj.getClass())
return false;
ZcodeAddress other = (ZcodeAddress) obj;
ZscriptAddress other = (ZscriptAddress) obj;
if (!Arrays.equals(addr, other.addr))
return false;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.function.Consumer;

public interface ZcodeConnection {
public interface ZscriptConnection {
void send(byte[] data);

void onReceive(Consumer<byte[]> handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.nio.charset.StandardCharsets;

public class ZcodeAbortCommand extends ZcodeCommand {
public class AbortCommand extends ZscriptCommand {

@Override
public CommandSeqElement thenFail() {
Expand Down Expand Up @@ -65,7 +65,7 @@ public boolean isCommand() {
}

@Override
public void response(ZcodeUnparsedCommandResponse resp) {
public void response(ZscriptUnparsedCommandResponse resp) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import java.util.Iterator;
import java.util.List;

public class ZcodeAndSeqElement extends CommandSeqElement {
public class AndSeqElement extends CommandSeqElement {
private final List<CommandSeqElement> elements;

ZcodeAndSeqElement(List<CommandSeqElement> elements) {
AndSeqElement(List<CommandSeqElement> elements) {
this.elements = elements;
for (CommandSeqElement el : elements) {
el.setParent(this);
}
}

ZcodeAndSeqElement(CommandSeqElement el1, CommandSeqElement el2) {
AndSeqElement(CommandSeqElement el1, CommandSeqElement el2) {
this.elements = new ArrayList<>();
elements.add(el1);
elements.add(el2);
Expand All @@ -27,12 +27,12 @@ public class ZcodeAndSeqElement extends CommandSeqElement {
@Override
public CommandSeqElement andThen(CommandSeqElement next) {
List<CommandSeqElement> newEls = new ArrayList<>(elements);
if (next.getClass() == ZcodeAndSeqElement.class) {
newEls.addAll(((ZcodeAndSeqElement) next).elements);
if (next.getClass() == AndSeqElement.class) {
newEls.addAll(((AndSeqElement) next).elements);
} else {
newEls.add(next);
}
return new ZcodeAndSeqElement(newEls);
return new AndSeqElement(newEls);
}

@Override
Expand All @@ -54,25 +54,25 @@ public boolean isCommand() {
public CommandSeqElement reEvaluate() {
List<CommandSeqElement> els = new ArrayList<>();
for (CommandSeqElement element : elements) {
if (element.getClass() == ZcodeFailureCommand.class) {
if (element.getClass() == FailureCommand.class) {
els.add(element);
break;
} else if (element.getClass() == ZcodeAbortCommand.class) {
} else if (element.getClass() == AbortCommand.class) {
els.add(element);
break;
} else if (element.getClass() == ZcodeAndSeqElement.class) {
els.addAll(((ZcodeAndSeqElement) element).elements);
} else if (element.getClass() == ZcodeBlankCommand.class) {
} else if (element.getClass() == AndSeqElement.class) {
els.addAll(((AndSeqElement) element).elements);
} else if (element.getClass() == BlankCommand.class) {
} else {
els.add(element.reEvaluate());
}
}
if (els.size() == 0) {
return new ZcodeBlankCommand();
return new BlankCommand();
} else if (els.size() == 1) {
return els.get(0);
}
return new ZcodeAndSeqElement(els);
return new AndSeqElement(els);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package net.zscript.javaclient.zcodeApi;

public class ZcodeBlankCommand extends ZcodeCommand {
public class BlankCommand extends ZscriptCommand {

@Override
public CommandSeqElement thenFail() {
return new ZcodeFailureCommand();
return new FailureCommand();
}

@Override
public CommandSeqElement thenAbort() {
return new ZcodeAbortCommand();
return new AbortCommand();
}

@Override
Expand Down Expand Up @@ -63,7 +63,7 @@ public byte[] compile(boolean includeParens) {
}

@Override
public void response(ZcodeUnparsedCommandResponse resp) {
public void response(ZscriptUnparsedCommandResponse resp) {
}

@Override
Expand Down
Loading

0 comments on commit cbe61cc

Please sign in to comment.