-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
View models, terminated string, prefixed string, database packet defi…
…nition loader, transient modifier on fields that get encoded in JSON
- Loading branch information
Showing
11 changed files
with
326 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
public enum PacketType { | ||
NEGOTIATION, | ||
SERVICE | ||
GAME | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/one/slope/slip/io/packet/field/PrefixedStringPacketField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package one.slope.slip.io.packet.field; | ||
|
||
import one.slope.slip.io.SuperBuffer; | ||
import one.slope.slip.io.DataType; | ||
|
||
public class PrefixedStringPacketField extends FieldCodec<String> { | ||
private DataType prefix; | ||
|
||
public PrefixedStringPacketField(DataType prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
@Override | ||
public String read(SuperBuffer buffer) { | ||
byte[] bytes = buffer.get(prefix.width()); | ||
return new String(bytes); | ||
} | ||
|
||
@Override | ||
public void write(SuperBuffer buffer, String value) { | ||
byte[] bytes = value.getBytes(); | ||
buffer.put(bytes.length, this.prefix); | ||
buffer.put(bytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/one/slope/slip/service/DatabasePacketDefinitionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package one.slope.slip.service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.dieselpoint.norm.Database; | ||
|
||
import one.slope.slip.io.DataType; | ||
import one.slope.slip.io.packet.PacketDefinition; | ||
import one.slope.slip.io.packet.PacketDefinitionProvider; | ||
import one.slope.slip.io.packet.field.BooleanFieldCodec; | ||
import one.slope.slip.io.packet.field.FieldCodec; | ||
import one.slope.slip.io.packet.field.NumericFieldCodec; | ||
import one.slope.slip.io.packet.field.PacketField; | ||
import one.slope.slip.io.packet.field.PrefixedStringPacketField; | ||
import one.slope.slip.io.packet.field.TerminatedStringPacketField; | ||
|
||
public class DatabasePacketDefinitionProvider implements PacketDefinitionProvider { | ||
private final Database database; | ||
|
||
private PacketDefinition[] definitions; | ||
|
||
public DatabasePacketDefinitionProvider(Database database) { | ||
this.database = database; | ||
} | ||
|
||
public boolean load(String revision) { | ||
List<RevisionPacketRead> packets = database.where("revision = ?", revision).results(RevisionPacketRead.class); | ||
List<PacketDefinition> temp = new ArrayList<>(); | ||
|
||
packets.forEach((packet) -> { | ||
List<RevisionPacketFieldRead> fields = database.where("revision_id = ? AND event_id = ?", packet.revision_id, packet.event_id).results(RevisionPacketFieldRead.class); | ||
List<PacketField<?>> tempFields = new ArrayList<>(); | ||
|
||
fields.forEach((field) -> { | ||
FieldCodec<?> codec = null; | ||
|
||
switch(field.revision_field_type) { | ||
case BOOLEAN: | ||
codec = new BooleanFieldCodec(); | ||
break; | ||
case BYTE: | ||
case SHORT: | ||
case TRIPLE: | ||
case INTEGER: | ||
case LONG: | ||
codec = new NumericFieldCodec<Long>(field.revision_field_type, field.endian, field.transformation, field.range); | ||
break; | ||
case LB_STRING: | ||
case LS_STRING: | ||
codec = new PrefixedStringPacketField(field.revision_field_type.length()); | ||
break; | ||
case LT_STRING: | ||
case NT_STRING: | ||
codec = new TerminatedStringPacketField(field.revision_field_type.terminator()); | ||
break; | ||
} | ||
|
||
PacketField<?> pfield = new PacketField(field.field_name, field.revision_field_rank, codec); | ||
tempFields.add(pfield); | ||
}); | ||
|
||
PacketDefinition def = new PacketDefinition(packet.event_type_name, packet.opcode, packet.event_name, tempFields.toArray(new PacketField[0]), (int)packet.length); | ||
temp.add(def); | ||
}); | ||
|
||
this.definitions = temp.toArray(new PacketDefinition[0]); | ||
return this.definitions != null && this.definitions.length > 0; | ||
} | ||
|
||
@Override | ||
public PacketDefinition[] get() { | ||
return this.definitions; | ||
} | ||
|
||
public Database database() { | ||
return this.database; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/one/slope/slip/service/RevisionPacketFieldRead.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package one.slope.slip.service; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.EnumType; | ||
import javax.persistence.Enumerated; | ||
import javax.persistence.Table; | ||
|
||
import com.dieselpoint.norm.Database; | ||
|
||
import one.slope.slip.io.DataEndian; | ||
import one.slope.slip.io.DataRange; | ||
import one.slope.slip.io.DataTransformation; | ||
import one.slope.slip.io.DataType; | ||
import one.slope.slip.io.packet.PacketType; | ||
|
||
@Table(name = "revision_packet_field_read") | ||
public class RevisionPacketFieldRead { | ||
@Column(name = "event_id", insertable = false, updatable = false) | ||
public int event_id; | ||
|
||
@Column(name = "packet_id", insertable = false, updatable = false) | ||
public int packet_id; | ||
|
||
@Column(name = "revision_id", insertable = false, updatable = false) | ||
public int revision_id; | ||
|
||
@Column(name = "field_id", insertable = false, updatable = false) | ||
public int field_id; | ||
|
||
@Column(name = "revision_packet_id", insertable = false, updatable = false) | ||
public int revision_packet_id; | ||
|
||
@Column(name = "revision_field_id", insertable = false, updatable = false) | ||
public int revision_field_id; | ||
|
||
@Column(name = "event_type_id", insertable = false, updatable = false) | ||
public int event_type_id; | ||
|
||
@Column(name = "field_type_id", insertable = false, updatable = false) | ||
public int field_type_id; | ||
|
||
@Column(name = "revision_field_type_id", insertable = false, updatable = false) | ||
public int revision_field_type_id; | ||
|
||
@Column(name = "opcode", insertable = false, updatable = false) | ||
public int opcode; | ||
|
||
@Column(name = "length", insertable = false, updatable = false) | ||
public long length; | ||
|
||
@Column(name = "event_name", insertable = false, updatable = false) | ||
public String event_name; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "event_type_name", insertable = false, updatable = false) | ||
public PacketType event_type_name; | ||
|
||
@Column(name = "revision", insertable = false, updatable = false) | ||
public String revision; | ||
|
||
@Column(name = "field_name", insertable = false, updatable = false) | ||
public String field_name; | ||
|
||
@Column(name = "revision_field_rank", insertable = false, updatable = false) | ||
public int revision_field_rank; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "field_type", insertable = false, updatable = false) | ||
public DataType field_type; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "revision_field_type", insertable = false, updatable = false) | ||
public DataType revision_field_type; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "ranged", insertable = false, updatable = false) | ||
public DataRange range; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "endian", insertable = false, updatable = false) | ||
public DataEndian endian; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name = "transformation", insertable = false, updatable = false) | ||
public DataTransformation transformation; | ||
|
||
public RevisionPacketFieldRead() { | ||
|
||
} | ||
|
||
public Event event(Database db) { | ||
return db.where("id = ?", this.event_id).results(Event.class).get(0); | ||
} | ||
|
||
public FieldType fieldType(Database db) { | ||
return db.where("id = ?", this.field_type_id).results(FieldType.class).get(0); | ||
} | ||
|
||
public RevisionField revisionField(Database db) { | ||
return db.where("id = ?", this.revision_field_id).results(RevisionField.class).get(0); | ||
} | ||
|
||
public FieldType revisionFieldType(Database db) { | ||
return db.where("id = ?", this.revision_field_type_id).results(FieldType.class).get(0); | ||
} | ||
|
||
public Packet packet(Database db) { | ||
return db.where("id = ?", this.packet_id).results(Packet.class).get(0); | ||
} | ||
|
||
public Field field(Database db) { | ||
return db.where("id = ?", this.field_id).results(Field.class).get(0); | ||
} | ||
|
||
public Revision revision(Database db) { | ||
return db.where("id = ?", this.revision_id).results(Revision.class).get(0); | ||
} | ||
|
||
public RevisionPacket revisionPacket(Database db) { | ||
return db.where("id = ?", this.revision_packet_id).results(RevisionPacket.class).get(0); | ||
} | ||
|
||
public EventType type(Database db) { | ||
return db.where("id = ?", this.event_type_id).results(EventType.class).get(0); | ||
} | ||
} |
Oops, something went wrong.