Set up Boat Action Message encoding

- Fixed command number for sails out

#story[1089]
main
Connor Taylor-Brown 9 years ago
parent e670ded66d
commit 0352e3310d

@ -94,6 +94,15 @@ public class BinaryMessageEncoder {
this.fullMessage = tempFullMessageByteBuffer.array(); this.fullMessage = tempFullMessageByteBuffer.array();
} }
/**
* Construct a binary message from message type and message body.
* @param headerMessageType of message
* @param messageBody of message
*/
public BinaryMessageEncoder(MessageType headerMessageType, byte[] messageBody) {
this(headerMessageType, System.currentTimeMillis(), 69, (short)messageBody.length, messageBody);
}
/** /**
* Returns the full encoded message. This includes the header, body, and CRC. * Returns the full encoded message. This includes the header, body, and CRC.
* @return Full encoded message. * @return Full encoded message.

@ -19,6 +19,7 @@ public enum MessageType {
MARKROUNDING(38), MARKROUNDING(38),
COURSEWIND(44), COURSEWIND(44),
AVGWIND(47), AVGWIND(47),
BOATACTION(100),
NOTAMESSAGE(0); NOTAMESSAGE(0);
///Primitive value of the enum. ///Primitive value of the enum.

@ -1,10 +1,13 @@
package visualiser.gameController; package visualiser.gameController;
import network.BinaryMessageEncoder;
import network.Messages.Enums.MessageType;
import visualiser.gameController.Keys.ControlKey; import visualiser.gameController.Keys.ControlKey;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /**
@ -27,6 +30,7 @@ public class ControllerClient {
*/ */
public ControllerClient(Socket socket) { public ControllerClient(Socket socket) {
this.socket = socket; this.socket = socket;
try { try {
this.outputStream = new DataOutputStream(socket.getOutputStream()); this.outputStream = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) { } catch (IOException e) {
@ -40,7 +44,14 @@ public class ControllerClient {
* @throws IOException if socket write fails * @throws IOException if socket write fails
*/ */
public void sendKey(ControlKey key) throws IOException { public void sendKey(ControlKey key) throws IOException {
// TODO - get and send action number currently corresponding to key (context dependent) int protocolCode = key.getProtocolCode();
System.out.println(key.toString() + ": " + key.getProtocolCode()); if(protocolCode > -1) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(protocolCode);
byte[] message = new byte[]{buffer.get(3)};
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, message);
//outputStream.write(binaryMessage.getFullMessage());
System.out.println("Binary message constructed");
}
} }
} }

@ -39,7 +39,7 @@ public class ControllerServer implements Runnable {
@Override @Override
public void run() { public void run() {
while(true) { while(true) {
byte[] key = new byte[1]; byte[] key = new byte[16];
try { try {
inputStream.read(key); inputStream.read(key);
// TODO - handle messages received // TODO - handle messages received

@ -19,7 +19,7 @@ public class SailsToggleKey extends ControlKey {
*/ */
@Override @Override
public void onAction() { public void onAction() {
protocolCode = protocolCode == 2? 1 : 2; protocolCode = protocolCode == 2? 3 : 2;
} }
@Override @Override

Loading…
Cancel
Save