binary message encoder as well test for the decoder

-added binary message encoder
-added test for the decoder
-bug with the sync number 2

#story[782]
main
hba56 9 years ago
parent f56bb747a7
commit 655b5787a2

@ -0,0 +1,86 @@
package seng302.Networking;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
/**
* Created by hba56 on 21/04/17.
*/
public class BinaryMesageEncoder {
private byte[] fullMessage;
private byte[] header;
private byte[] message;
private byte[] crc;
private byte headerSync1 = (byte)0x47;
private byte headerSync2 = (byte)0x83;
private byte headerMessageType;
private long headerTimeStamp;
private int headerSourceID;
private short headerMessageLength;
public BinaryMesageEncoder(byte headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message){
//set the header
this.headerMessageType = headerMessageType;
this.headerTimeStamp = headerTimeStamp;
this.headerSourceID = headerSourceID;
this.headerMessageLength = headerMessageLength;
ByteBuffer tempHeaderByteBuffer = ByteBuffer.allocate(15);
tempHeaderByteBuffer.put(this.headerSync1);
tempHeaderByteBuffer.put(this.headerSync2);
tempHeaderByteBuffer.put(this.headerMessageType);
tempHeaderByteBuffer.put(longToSixBytes(this.headerTimeStamp));
tempHeaderByteBuffer.put(intToBytes(this.headerSourceID));
tempHeaderByteBuffer.put(shortToBytes(this.headerMessageLength));
this.header = tempHeaderByteBuffer.array();
//set the message
this.message = message;
//set full message
ByteBuffer tempMessageByteBuffer = ByteBuffer.allocate(19+this.headerMessageLength);
tempMessageByteBuffer.put(this.header);
tempMessageByteBuffer.put(this.message);
//todo fix crc, currently just some int
tempMessageByteBuffer.put(intToBytes(1));
this.fullMessage = tempMessageByteBuffer.array();
}
private short bytesToShort(byte[] bytesShort){
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.put(bytesShort[0]);
byteBuffer.put(bytesShort[1]);
short shortVal = byteBuffer.getShort(0);
return shortVal;
}
private byte[] longToSixBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return Arrays.copyOfRange(buffer.array(), 2, 8);
}
private byte[] shortToBytes(short x) {
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES);
buffer.putShort(x);
return buffer.array();
}
private byte[] intToBytes(int x) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.putInt(x);
return buffer.array();
}
public byte[] getFullMessage() {
return fullMessage;
}
}

@ -27,25 +27,30 @@ public class BinaryMessageDecoder {
public void decode() throws IndexOutOfBoundsException{
//get the header
this.header = Arrays.copyOfRange(this.fullMessage, 0, 16);
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, 16);
this.headerMessageLength = Arrays.copyOfRange(this.header, 13, 15);
//get message
this.message = Arrays.copyOfRange(this.fullMessage, 16, this.fullMessage.length - 4);
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 != bytesToInt(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 != 0x83){
System.err.println("Sync byte 2 is wrong");
}else if(false){
//todo check crc
}
@ -53,22 +58,21 @@ public class BinaryMessageDecoder {
private short bytesToShort(byte[] bytesShort){
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.put(bytesShort[0]);
byteBuffer.put(bytesShort[1]);
short shortVal = byteBuffer.getShort(0);
return shortVal;
}
private short bytesToInt(byte[] bytesInt){
private int bytesToInt(byte[] bytesInt){
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.put(bytesInt[0]);
byteBuffer.put(bytesInt[1]);
byteBuffer.put(bytesInt[2]);
byteBuffer.put(bytesInt[3]);
short shortVal = byteBuffer.getShort(0);
return shortVal;
byteBuffer.order(ByteOrder.BIG_ENDIAN);
for(int i= 0; i<bytesInt.length;i++){
byteBuffer.put(bytesInt[i]);
}
int intVal = byteBuffer.getShort(0);
return intVal;
}
private long bytesToLong(byte[] bytesLong){

@ -0,0 +1,41 @@
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.BinaryMesageEncoder;
import seng302.Networking.BinaryMessageDecoder;
import seng302.Networking.XMLMessageDecoder;
import seng302.Networking.XMLMessageEncoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by hba56 on 21/04/17.
*/
public class BinaryMessageDecoderTest {
@Test
public void decodeTest(){
try{
StringBuilder xmlString;
BufferedReader br = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream(("raceXML/Regatta.xml"))));
String line;
xmlString = new StringBuilder();
while((line=br.readLine())!= null){
xmlString.append(line.trim());
}
long time = System.currentTimeMillis();
XMLMessageEncoder testEncoder = new XMLMessageEncoder((byte)1, (short)1, time, (byte)7, (short)1, (short)xmlString.length(), xmlString.toString());
byte[] encodedMessage = testEncoder.encode();
BinaryMesageEncoder testMessage = new BinaryMesageEncoder((byte)26, time, 1, (short)encodedMessage.length, encodedMessage);
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
testDecoder.decode();
}catch (IOException e){
System.out.println(e);
}
}
}

@ -39,7 +39,6 @@ public class XMLMessageDecoderTest {
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
}catch (IOException e){
System.out.println(e);
}

Loading…
Cancel
Save