master
剑器近 2020-11-03 15:30:46 +08:00
parent 6cd616a251
commit 6fa42d1706
9 changed files with 481 additions and 12 deletions

View File

@ -48,12 +48,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -1,8 +0,0 @@
package io.github.yezhihao.protostar;
public class Test {
public static void main(String[] args) {
System.out.println();
}
}

View File

@ -0,0 +1,46 @@
package io.github.yezhihao.protostar.convert;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.annotation.Field;
public class Attr1 {
private String name;
private int id;
public Attr1() {
}
public Attr1(String name, int id) {
this.name = name;
this.id = id;
}
@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Field(index = 1, type = DataType.WORD, desc = "ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Attr1{");
sb.append("name='").append(name).append('\'');
sb.append(", id=").append(id);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,66 @@
package io.github.yezhihao.protostar.convert;
import io.netty.buffer.ByteBuf;
import java.nio.charset.StandardCharsets;
public class Attr2 {
private int type;
private String content;
public Attr2() {
}
public Attr2(int type, String content) {
this.type = type;
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Attr2{");
sb.append("type=").append(type);
sb.append(", content='").append(content).append('\'');
sb.append('}');
return sb.toString();
}
public static class Schema implements io.github.yezhihao.protostar.Schema<Attr2> {
public static final Schema INSTANCE = new Schema();
private Schema() {
}
@Override
public Attr2 readFrom(ByteBuf input) {
Attr2 message = new Attr2();
message.type = input.readInt();
message.content = input.readCharSequence(input.readableBytes(), StandardCharsets.UTF_8).toString();
return message;
}
@Override
public void writeTo(ByteBuf output, Attr2 message) {
output.writeInt(message.type);
output.writeCharSequence(message.content, StandardCharsets.UTF_8);
}
}
}

View File

@ -0,0 +1,54 @@
package io.github.yezhihao.protostar.convert;
import io.github.yezhihao.protostar.IdStrategy;
import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.converter.MapConverter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AttributeConverter extends MapConverter<Integer, Object> {
private static final Logger log = LoggerFactory.getLogger(AttributeConverter.class);
private static final IdStrategy INSTANCE = AttributeType.INSTANCE;
@Override
public Object convert(Integer key, ByteBuf input) {
if (!input.isReadable())
return null;
Schema schema = INSTANCE.getSchema(key);
if (schema != null)
return INSTANCE.readFrom(key, input);
byte[] bytes = new byte[input.readableBytes()];
input.readBytes(bytes);
log.warn("未识别的附加信息ID[{}], HEX[{}]", key, ByteBufUtil.hexDump(bytes));
return bytes;
}
@Override
public void convert(Integer key, ByteBuf output, Object value) {
Schema schema = INSTANCE.getSchema(key);
if (schema != null) {
schema.writeTo(output, value);
} else {
log.warn("未注册的附加信息ID[{}], Value[{}]", key, value);
}
}
@Override
protected Integer readKey(ByteBuf input) {
return (int) input.readUnsignedByte();
}
@Override
protected void writeKey(ByteBuf output, Integer key) {
output.writeByte(key);
}
@Override
protected int valueSize() {
return 1;
}
}

View File

@ -0,0 +1,21 @@
package io.github.yezhihao.protostar.convert;
import io.github.yezhihao.protostar.IdStrategy;
import io.github.yezhihao.protostar.PrepareLoadStrategy;
import io.github.yezhihao.protostar.schema.IntSchema;
import io.github.yezhihao.protostar.schema.StringSchema;
public class AttributeType extends PrepareLoadStrategy {
public static final IdStrategy INSTANCE = new AttributeType();
@Override
protected void addSchemas(PrepareLoadStrategy schemaRegistry) {
schemaRegistry
.addSchema(1, IntSchema.Int32.INSTANCE)
.addSchema(2, StringSchema.Chars.getInstance((byte) 0, "GBK"))
.addSchema(3, Attr1.class)
.addSchema(4, Attr2.Schema.INSTANCE);
}
}

View File

@ -0,0 +1,114 @@
package io.github.yezhihao.protostar.convert;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.ProtostarUtil;
import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.annotation.Convert;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.annotation.Fs;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
Map<Integer, Schema<Foo>> multiVersionSchema = ProtostarUtil.getSchema(Foo.class);
Schema<Foo> schema_v0 = multiVersionSchema.get(0);
Schema<Foo> schema_v1 = multiVersionSchema.get(1);
ByteBuf buffer = Unpooled.buffer(32);
schema_v0.writeTo(buffer, foo());
String hex = ByteBufUtil.hexDump(buffer);
System.out.println(hex);
Foo foo = schema_v0.readFrom(buffer);
System.out.println(foo);
System.out.println("=========================version: 0");
buffer = Unpooled.buffer(32);
schema_v1.writeTo(buffer, foo());
hex = ByteBufUtil.hexDump(buffer);
System.out.println(hex);
foo = schema_v1.readFrom(buffer);
System.out.println(foo);
System.out.println("=========================version: 1");
}
public static Foo foo() {
Foo foo = new Foo();
foo.setName("张三");
foo.setId(128);
foo.setDateTime(LocalDateTime.of(2020, 7, 7, 19, 23, 59));
Map<Integer, Object> attrs = new TreeMap<>();
attrs.put(1, 123);
attrs.put(2, "李四");
attrs.put(3, new Attr1("test", 1));
attrs.put(4, new Attr2(2, "test2"));
foo.setAttributes(attrs);
return foo;
}
public static class Foo {
private String name;
private int id;
private LocalDateTime dateTime;
private Map<Integer, Object> attributes;
@Fs({@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称", version = 0),
@Field(index = 0, type = DataType.STRING, length = 10, desc = "名称", version = 1)})
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Fs({@Field(index = 1, type = DataType.WORD, desc = "ID", version = 0),
@Field(index = 1, type = DataType.DWORD, desc = "ID", version = 1)})
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Field(index = 3, type = DataType.BCD8421, desc = "日期", version = {0, 1})
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
@Convert(converter = AttributeConverter.class)
@Field(index = 4, type = DataType.MAP, desc = "属性", version = {0, 1})
public Map<Integer, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<Integer, Object> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Foo{");
sb.append("name='").append(name).append('\'');
sb.append(", id=").append(id);
sb.append(", dateTime=").append(dateTime);
sb.append(", attributes=").append(attributes);
sb.append('}');
return sb.toString();
}
}
}

View File

@ -0,0 +1,94 @@
package io.github.yezhihao.protostar.multiversion;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.ProtostarUtil;
import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.annotation.Fs;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import java.time.LocalDateTime;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, Schema<Foo>> multiVersionSchema = ProtostarUtil.getSchema(Foo.class);
Schema<Foo> schema_v0 = multiVersionSchema.get(0);
Schema<Foo> schema_v1 = multiVersionSchema.get(1);
ByteBuf buffer = Unpooled.buffer(32);
schema_v0.writeTo(buffer, foo());
String hex = ByteBufUtil.hexDump(buffer);
System.out.println(hex);
Foo foo = schema_v0.readFrom(buffer);
System.out.println(foo);
System.out.println("=========================version: 0");
buffer = Unpooled.buffer(32);
schema_v1.writeTo(buffer, foo());
hex = ByteBufUtil.hexDump(buffer);
System.out.println(hex);
foo = schema_v1.readFrom(buffer);
System.out.println(foo);
System.out.println("=========================version: 1");
}
public static Foo foo() {
Foo foo = new Foo();
foo.setName("张三");
foo.setId(128);
foo.setDateTime(LocalDateTime.of(2020, 7, 7, 19, 23, 59));
return foo;
}
public static class Foo {
private String name;
private int id;
private LocalDateTime dateTime;
@Fs({@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称", version = 0),
@Field(index = 0, type = DataType.STRING, length = 10, desc = "名称", version = 1)})
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Fs({@Field(index = 1, type = DataType.WORD, desc = "ID", version = 0),
@Field(index = 1, type = DataType.DWORD, desc = "ID", version = 1)})
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Field(index = 3, type = DataType.BCD8421, desc = "日期", version = {0, 1})
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Foo{");
sb.append("name='").append(name).append('\'');
sb.append(", id=").append(id);
sb.append(", dateTime=").append(dateTime);
sb.append('}');
return sb.toString();
}
}
}

View File

@ -0,0 +1,83 @@
package io.github.yezhihao.protostar.simple;
import io.github.yezhihao.protostar.DataType;
import io.github.yezhihao.protostar.FieldFactory;
import io.github.yezhihao.protostar.ProtostarUtil;
import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.annotation.Field;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import java.time.LocalDateTime;
import java.util.Map;
public class Test {
public static void main(String[] args) {
FieldFactory.EXPLAIN = true;
Map<Integer, Schema<Foo>> multiVersionSchema = ProtostarUtil.getSchema(Foo.class);
Schema<Foo> schema = multiVersionSchema.get(0);
ByteBuf buffer = Unpooled.buffer(32);
schema.writeTo(buffer, foo());
String hex = ByteBufUtil.hexDump(buffer);
System.out.println(hex);
Foo foo = schema.readFrom(buffer);
System.out.println(foo);
}
public static Foo foo() {
Foo foo = new Foo();
foo.setName("张三");
foo.setId(128);
foo.setDateTime(LocalDateTime.of(2020, 7, 7, 19, 23, 59));
return foo;
}
public static class Foo {
private String name;
private int id;
private LocalDateTime dateTime;
@Field(index = 0, type = DataType.STRING, lengthSize = 1, desc = "名称")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Field(index = 1, type = DataType.WORD, desc = "ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Field(index = 3, type = DataType.BCD8421, desc = "日期")
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Foo{");
sb.append("name='").append(name).append('\'');
sb.append(", id=").append(id);
sb.append(", dateTime=").append(dateTime);
sb.append('}');
return sb.toString();
}
}
}