-added binary message encoder -added test for the decoder -bug with the sync number 2 #story[782]main
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;
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue