updated encoder and decoder with test for decoder

-added binary message decoder test
-updated binary message encoder
-updated binary message decoder
-added enum for message type

#story[778, 782]
main
hba56 9 years ago
parent 655b5787a2
commit 8e79164b69

@ -20,9 +20,9 @@ public class BinaryMesageEncoder {
private int headerSourceID;
private short headerMessageLength;
public BinaryMesageEncoder(byte headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message){
public BinaryMesageEncoder(MessageType headerMessageType, long headerTimeStamp, int headerSourceID, short headerMessageLength, byte[] message){
//set the header
this.headerMessageType = headerMessageType;
this.headerMessageType = headerMessageType.getValue();
this.headerTimeStamp = headerTimeStamp;
this.headerSourceID = headerSourceID;
this.headerMessageLength = headerMessageLength;
@ -32,7 +32,7 @@ public class BinaryMesageEncoder {
tempHeaderByteBuffer.put(this.headerSync2);
tempHeaderByteBuffer.put(this.headerMessageType);
tempHeaderByteBuffer.put(longToSixBytes(this.headerTimeStamp));
tempHeaderByteBuffer.put(intToBytes(this.headerSourceID));
tempHeaderByteBuffer.putInt(this.headerSourceID);
tempHeaderByteBuffer.put(shortToBytes(this.headerMessageLength));
@ -53,15 +53,6 @@ public class BinaryMesageEncoder {
}
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);

@ -43,36 +43,30 @@ public class BinaryMessageDecoder {
this.crc = Arrays.copyOfRange(this.fullMessage, this.fullMessage.length - 4, fullMessage.length);
//run through the checks
if (this.message.length != bytesToInt(this.headerMessageLength)){
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 != 0x83){
}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 byteBuffer = ByteBuffer.allocate(2);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.put(bytesShort[0]);
byteBuffer.put(bytesShort[1]);
short shortVal = byteBuffer.getShort(0);
return shortVal;
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
short num = wrapped.getShort();
return num;
}
private int bytesToInt(byte[] bytesInt){
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
for(int i= 0; i<bytesInt.length;i++){
byteBuffer.put(bytesInt[i]);
}
int intVal = byteBuffer.getShort(0);
return intVal;
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
int num = wrapped.getInt();
return num;
}
private long bytesToLong(byte[] bytesLong){
@ -90,4 +84,24 @@ public class BinaryMessageDecoder {
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;
}
}

@ -0,0 +1,18 @@
package seng302.Networking;
/**
* Created by hba56 on 21/04/17.
*/
public enum MessageType {
HEARTBEAT(1), RACESTATUS(12), DISPLAYTEXTMESSAGE(20),
XMLMESSAGE(26), RACESTARTSTATUS(27), YACHTEVENTCODE(29), YACHTACTIONCODE(31),
CHATTERTEXT(36), BOATLOCATION(37), MARKROUNDING(38), COURSEWIND(44), AVGWIND(47);
private byte value;
private MessageType(int value) { this.value = (byte)value; }
public byte getValue() {
return value;
}
}

@ -1,9 +1,6 @@
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 seng302.Networking.*;
import java.io.BufferedReader;
import java.io.IOException;
@ -29,11 +26,32 @@ public class BinaryMessageDecoderTest {
byte[] encodedMessage = testEncoder.encode();
BinaryMesageEncoder testMessage = new BinaryMesageEncoder((byte)26, time, 1, (short)encodedMessage.length, encodedMessage);
BinaryMesageEncoder testMessage = new BinaryMesageEncoder(MessageType.XMLMESSAGE, time, 1, (short)encodedMessage.length, encodedMessage);
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(testMessage.getFullMessage());
testDecoder.decode();
//message length
Assert.assertEquals((short) encodedMessage.length,testDecoder.getMessageLength());
//time stamp
Assert.assertEquals(time,testDecoder.getTimeStamp());
//source ID
Assert.assertEquals((short) 1, testDecoder.getSourceID());
//message type
Assert.assertEquals(26, testDecoder.getMessageType());
XMLMessageDecoder decoderXML = new XMLMessageDecoder(testDecoder.getMessage());
decoderXML.decode();
//tests from XMLMessageDecoderTest to make sure the file is still good
Assert.assertEquals((byte)1, decoderXML.getMessageVersionNumber());
Assert.assertEquals((short)1, decoderXML.getAckNumber());
Assert.assertEquals(time, decoderXML.getTimeStamp());
Assert.assertEquals((byte)7, decoderXML.getXmlMsgSubType());
Assert.assertEquals((short)1, decoderXML.getSequenceNumber());
Assert.assertEquals((short)xmlString.length(), decoderXML.getXmlMsgLength());
Assert.assertEquals(xmlString.toString(), decoderXML.getXmlMessage());
}catch (IOException e){
System.out.println(e);
}

Loading…
Cancel
Save