You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.2 KiB
108 lines
3.2 KiB
package seng302.Networking;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.util.Arrays;
|
|
|
|
/**
|
|
* Created by hba56 on 21/04/17.
|
|
*/
|
|
public class BinaryMessageDecoder {
|
|
private byte[] fullMessage;
|
|
private byte[] header;
|
|
private byte[] message;
|
|
private byte[] crc;
|
|
|
|
private byte headerSync1;
|
|
private byte headerSync2;
|
|
private byte headerMessageType;
|
|
private byte[] headerTimeStamp;
|
|
private byte[] headerSourceID;
|
|
private byte[] headerMessageLength;
|
|
|
|
|
|
public BinaryMessageDecoder(byte[] fullMessage) {
|
|
this.fullMessage = fullMessage;
|
|
}
|
|
|
|
public void decode() throws IndexOutOfBoundsException {
|
|
//get the header
|
|
this.header = Arrays.copyOfRange(this.fullMessage, 0, 15);
|
|
|
|
this.headerSync1 = this.header[0];
|
|
this.headerSync2 = this.header[1];
|
|
this.headerMessageType = this.header[2];
|
|
this.headerTimeStamp = Arrays.copyOfRange(this.header, 3, 9);
|
|
this.headerSourceID = Arrays.copyOfRange(this.header, 9, 13);
|
|
this.headerMessageLength = Arrays.copyOfRange(this.header, 13, 15);
|
|
|
|
//get message
|
|
this.message = Arrays.copyOfRange(this.fullMessage, 15, this.fullMessage.length - 4);
|
|
|
|
//get crc
|
|
this.crc = Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - 4, fullMessage.length);
|
|
|
|
//run through the checks
|
|
if (this.message.length != bytesToShort(this.headerMessageLength)) {
|
|
System.err.println("message length in header does not equal the message length");
|
|
System.err.println("message length in header: " + bytesToInt(this.headerMessageLength));
|
|
System.err.println("message length: " + this.message.length);
|
|
} else if (this.headerSync1 != 0x47) {
|
|
System.err.println("Sync byte 1 is wrong");
|
|
} else if (this.headerSync2 != (byte) 0x83) {
|
|
System.err.println("Sync byte 2 is wrong");
|
|
} else if (false) {
|
|
//todo check crc
|
|
}
|
|
}
|
|
|
|
|
|
private short bytesToShort(byte[] bytesShort) {
|
|
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
|
short num = wrapped.getShort();
|
|
return num;
|
|
}
|
|
|
|
private int bytesToInt(byte[] bytesInt) {
|
|
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
|
int num = wrapped.getInt();
|
|
return num;
|
|
}
|
|
|
|
private long bytesToLong(byte[] bytesLong) {
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
|
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
|
byteBuffer.put((byte) 0);
|
|
byteBuffer.put((byte) 0);
|
|
byteBuffer.put(bytesLong[0]);
|
|
byteBuffer.put(bytesLong[1]);
|
|
byteBuffer.put(bytesLong[2]);
|
|
byteBuffer.put(bytesLong[3]);
|
|
byteBuffer.put(bytesLong[4]);
|
|
byteBuffer.put(bytesLong[5]);
|
|
long longVal = byteBuffer.getLong(0);
|
|
return longVal;
|
|
}
|
|
|
|
public long getTimeStamp() {
|
|
return bytesToLong(this.headerTimeStamp);
|
|
}
|
|
|
|
public int getSourceID() {
|
|
return bytesToInt(this.headerSourceID);
|
|
}
|
|
|
|
public short getMessageLength() {
|
|
return bytesToShort(this.headerMessageLength);
|
|
}
|
|
|
|
public int getMessageType() {
|
|
return (int) this.headerMessageType;
|
|
}
|
|
|
|
public byte[] getMessage() {
|
|
return message;
|
|
}
|
|
}
|
|
|