Mercurial > stress-tester
changeset 686:3abf8dd75b09
OptionsReaderWriter
author | Devel 1 |
---|---|
date | Fri, 24 Nov 2017 15:10:25 +0100 |
parents | 25d4e7b3722e |
children | ca40b6492c9f |
files | stress-tester/src/main/java/com/passus/st/reader/nc/option/Option.java stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionCoder.java stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOption.java stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java |
diffstat | 6 files changed, 282 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/Option.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,33 @@ +package com.passus.st.reader.nc.option; + +/** + * + * @author mikolaj.podbielski + */ +public class Option { + + private final String name; + private Object value; + + public Option(String name) { + this.name = name; + } + + public Option(String name, Object value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionCoder.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,73 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; +import com.passus.data.ByteString; +import com.passus.data.DataHelper; +import com.passus.st.reader.nc.NcDataHelper; + +/** + * + * @author mikolaj.podbielski + * @param <T> + */ +public interface OptionCoder<T> { + + public static DataHelper HELPER = DataHelper.BIG_ENDIAN; + public static NcDataHelper NC_HELPER = NcDataHelper.getInstance(); + + public void encode(T value, ByteBuff buff); + + public Object decode(ByteBuff buff); + + public static class IntCoder implements OptionCoder<Integer> { + + @Override + public void encode(Integer value, ByteBuff buff) { + HELPER.writeInt4(buff, value); + } + + @Override + public Object decode(ByteBuff buff) { + return HELPER.readInt4(buff); + } + } + + public static class IntVlcCoder implements OptionCoder<Integer> { + + @Override + public void encode(Integer value, ByteBuff buff) { + NC_HELPER.writeIntVLC(value, buff); + } + + @Override + public Object decode(ByteBuff buff) { + return NC_HELPER.readIntVLC(buff); + } + } + + public static class ByteStringNTCoder implements OptionCoder<ByteString> { + + @Override + public void encode(ByteString value, ByteBuff buff) { + NC_HELPER.writeByteStringNullTerminated(value, buff); + } + + @Override + public Object decode(ByteBuff buff) { + return NC_HELPER.readByteStringNullTerminated(buff); + } + } + + public static class StringNTCoder implements OptionCoder<String> { + + @Override + public void encode(String value, ByteBuff buff) { + NC_HELPER.writeStringNullTerminated(value, buff); + } + + @Override + public Object decode(ByteBuff buff) { + return NC_HELPER.readStringNullTerminated(buff); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,60 @@ +package com.passus.st.reader.nc.option; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author mikolaj.podbielski + */ +public class Options { + + private static final String[] OPTION_NAMES = new String[256]; + + private static final Map<String, Byte> OPTION_CODES = new HashMap<>(); + + private static final Map<String, OptionCoder> OPTION_CODERS = new HashMap<>(); + + static void addOption(String name, byte code) { + if (code == 0) { + throw new IllegalArgumentException("Option code must be in range 1-255"); + } + if (OPTION_NAMES[code] != null) { + throw new IllegalArgumentException("Option code used already: " + code); + } + if (OPTION_CODES.containsKey(name)) { + throw new IllegalArgumentException("Option name used already: " + name); + } + + OPTION_NAMES[code] = name; + OPTION_CODES.put(name, code); + } + + static void addOption(String name, byte code, OptionCoder coder) { + addOption(name, code); + OPTION_CODERS.put(name, coder); + } + + public static String getOptionName(byte code) { + return OPTION_NAMES[code]; + } + + public static byte getOptionCode(String name) { + Byte code = OPTION_CODES.get(name); + return code == null ? 0 : code; + } + + public static OptionCoder getOptionCoder(String name) { + return OPTION_CODERS.get(name); + } + + public static final String OPTION_VARS = "vars"; + public static final String OPTION_X = "opt_x"; + public static final String OPTION_Y = "opt_y"; + + static { + addOption(OPTION_VARS, (byte) 10, new VarOptionCoder()); + addOption(OPTION_X, (byte) 101); + addOption(OPTION_Y, (byte) 102); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,52 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; +import com.passus.st.reader.nc.NcDataHelper; +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author mikolaj.podbielski + */ +public class OptionsReaderWriter { + + public static NcDataHelper NC_HELPER = NcDataHelper.getInstance(); + + public void writeOptions(List<Option> options, ByteBuff buff) { + NC_HELPER.writeIntVLC(options.size(), buff); + + for (Option option : options) { + String name = option.getName(); + byte code = Options.getOptionCode(name); + OptionCoder coder = Options.getOptionCoder(name); + + buff.append(code); + if (code == 0) { + NC_HELPER.writeStringNullTerminated(name, buff); + } + coder.encode(option.getValue(), buff); + } + } + + public List<Option> readOptions(ByteBuff buff) { + int numOptions = NC_HELPER.readIntVLC(buff); + List<Option> options = new ArrayList<>(numOptions); + + for (int i = 0; i < numOptions; i++) { + byte code = buff.read(); + String name; + if (code == 0) { + name = NC_HELPER.readStringNullTerminated(buff); + } else { + name = Options.getOptionName(code); + } + OptionCoder coder = Options.getOptionCoder(name); + Object value = coder.decode(buff); + + options.add(new Option(name, value)); + } + + return options; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOption.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,37 @@ +package com.passus.st.reader.nc.option; + +/** + * + * @author mikolaj.podbielski + */ +public class VarOption { + + private String name; + private long startOffset; + private long endOffset; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getStartOffset() { + return startOffset; + } + + public void setStartOffset(long startOffset) { + this.startOffset = startOffset; + } + + public long getEndOffset() { + return endOffset; + } + + public void setEndOffset(long endOffset) { + this.endOffset = endOffset; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java Fri Nov 24 15:10:25 2017 +0100 @@ -0,0 +1,27 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; + +/** + * + * @author mikolaj.podbielski + */ +public class VarOptionCoder implements OptionCoder<VarOption> { + + @Override + public void encode(VarOption value, ByteBuff buff) { + NC_HELPER.writeStringNullTerminated(value.getName(), buff); + NC_HELPER.writeLongVLC(value.getStartOffset(), buff); + NC_HELPER.writeLongVLC(value.getEndOffset(), buff); + } + + @Override + public Object decode(ByteBuff buff) { + VarOption value = new VarOption(); + value.setName(NC_HELPER.readStringNullTerminated(buff)); + value.setStartOffset(NC_HELPER.readLongVLC(buff)); + value.setEndOffset(NC_HELPER.readLongVLC(buff)); + return value; + } + +}