Merge branch 'networkingTest' of https://eng-git.canterbury.ac.nz/seng302-2017/team-7 into networkingTest
commit
82acf40dd6
@ -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,93 @@
|
||||
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 != 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
|
||||
}
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package seng302;
|
||||
package seng302.Networking;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
@ -1,4 +1,4 @@
|
||||
package seng302;
|
||||
package seng302.Networking;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
@ -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