|
|
|
|
@ -3,7 +3,9 @@ package seng302.Networking;
|
|
|
|
|
import seng302.Model.BoatInRace;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by fwy13 on 19/04/17.
|
|
|
|
|
@ -139,39 +141,53 @@ public class RaceVisionByteEncoder {
|
|
|
|
|
result.put(event);
|
|
|
|
|
return result.array();
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
public byte[] chatterText(int messageType, String message){
|
|
|
|
|
int messageVersion = 0b1;
|
|
|
|
|
byte[] type = convert(messageType, 1);
|
|
|
|
|
byte[] length = convert(message.length(), 1);
|
|
|
|
|
byte[] messageBytes = message.getBytes(Charset.forName("UTF-8"));
|
|
|
|
|
byte[] text = convert(message, length[0]);
|
|
|
|
|
|
|
|
|
|
//byte[] text =
|
|
|
|
|
return;
|
|
|
|
|
}*/
|
|
|
|
|
ByteBuffer result = ByteBuffer.allocate(3 + text.length);
|
|
|
|
|
result.put(convert(messageVersion, 1));
|
|
|
|
|
result.put(type);
|
|
|
|
|
result.put(length);
|
|
|
|
|
result.put(text);
|
|
|
|
|
|
|
|
|
|
return result.array();
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
public byte[] boatLocation(){
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
public byte[] convert(String s, int size){
|
|
|
|
|
byte[] m = s.getBytes(Charset.forName("UTF-8"));
|
|
|
|
|
int length = m.length;
|
|
|
|
|
byte[] result;
|
|
|
|
|
if (length > 255){
|
|
|
|
|
length = 255;
|
|
|
|
|
} else if (size < 1){
|
|
|
|
|
result = new byte[0];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
byte[] result = Arrays.copyOfRange(m, 0, length + 1);
|
|
|
|
|
result = Arrays.copyOfRange(m, 0, length + 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public byte[] convert(int n, int size){
|
|
|
|
|
byte[] result;
|
|
|
|
|
if (size > 4){
|
|
|
|
|
result = new byte[4];
|
|
|
|
|
return result;
|
|
|
|
|
} else if (size < 1){
|
|
|
|
|
result = new byte[0];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
|
|
|
|
byteBuffer.putInt(n);
|
|
|
|
|
byte[] bytes = byteBuffer.array();
|
|
|
|
|
result = new byte[size];
|
|
|
|
|
for (int i = 4 - size ; i < 4; i++){
|
|
|
|
|
result[i-size] = bytes[i];
|
|
|
|
|
}
|
|
|
|
|
result = Arrays.copyOfRange(bytes, 4 - size, 4);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -180,14 +196,14 @@ public class RaceVisionByteEncoder {
|
|
|
|
|
if (size > 8){
|
|
|
|
|
result = new byte[8];
|
|
|
|
|
return result;
|
|
|
|
|
} else if (size < 1){
|
|
|
|
|
result = new byte[0];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
|
|
|
|
|
byteBuffer.putLong(n);
|
|
|
|
|
byte[] bytes = byteBuffer.array();
|
|
|
|
|
result = new byte[size];
|
|
|
|
|
for (int i = 8 - size ; i < 8; i++){
|
|
|
|
|
result[i-size] = bytes[i];
|
|
|
|
|
}
|
|
|
|
|
result = Arrays.copyOfRange(bytes, 8 - size, 8);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -197,14 +213,14 @@ public class RaceVisionByteEncoder {
|
|
|
|
|
if (size > 2){
|
|
|
|
|
result = new byte[2];
|
|
|
|
|
return result;
|
|
|
|
|
} else if (size < 1){
|
|
|
|
|
result = new byte[0];
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
|
|
|
|
|
byteBuffer.putLong(n);
|
|
|
|
|
byte[] bytes = byteBuffer.array();
|
|
|
|
|
result = new byte[size];
|
|
|
|
|
for (int i = 2 - size ; i < 2; i++){
|
|
|
|
|
result[i-size] = bytes[i];
|
|
|
|
|
}
|
|
|
|
|
result = Arrays.copyOfRange(bytes, 2 - size, 2);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|