Key presses are transmitted to a host (but there is no host currently connected)

#pair[kre39,zyt10] #story[988]
This commit is contained in:
Kusal Ekanayake
2017-07-13 15:39:48 +12:00
parent 5ce34bed92
commit 78557a4536
7 changed files with 111 additions and 35 deletions
@@ -0,0 +1,51 @@
package seng302.server;
import java.io.IOException;
import seng302.server.messages.BoatActionMessage;
/**
* Created by kre39 on 13/07/17.
*/
public class ClientTransmitterThread implements Runnable {
private StreamingServerSocket server;
private final int PORT_NUMBER = 4951;
private static final int LOG_LEVEL = 1;
public ClientTransmitterThread(String threadName){
Thread runner = new Thread(this, threadName);
runner.setDaemon(true);
runner.start();
}
static void serverLog(String message, int logLevel){
if(logLevel <= LOG_LEVEL){
System.out.println("[SERVER] " + message);
}
}
public void run() {
try{
// Needs to connect to the server: Currently no server is being connect so the boat action keys are not being sent
server = new StreamingServerSocket(PORT_NUMBER);
}
catch (IOException e){
serverLog("Failed to bind socket: " + e.getMessage(), 0);
}
// Wait for client to connect
server.start();
}
/**
* Send the post-start race course information
*/
public void sendBoatActionMessage(BoatActionMessage boatActionMessage) {
try {
server.send(boatActionMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -44,10 +44,9 @@ class StreamingServerSocket {
void send(Message message) throws IOException{
if (client == null){
return;
}
message.send(client);
seqNum++;
}
@@ -0,0 +1,40 @@
package seng302.server.messages;
import java.io.IOException;
import java.nio.channels.SocketChannel;
/**
* Created by kre39 on 12/07/17.
*/
public class BoatActionMessage extends Message{
private final MessageType MESSAGE_TYPE = MessageType.BOAT_ACTION;
private final int MESSAGE_VERSION = 1; //Always set to 1
private final int MESSAGE_SIZE = 1;
private BoatActionType actionType;
public BoatActionMessage(BoatActionType actionType) {
this.actionType = actionType;
}
@Override
public int getSize() {
return 0;
}
/**
* Send this message as a stream of bytes
* @param outputStream The output stream to send the message
*/
public void send(SocketChannel outputStream) throws IOException {
System.out.println("Sending boat action type: " + actionType.toString());
allocateBuffer();
writeHeaderToBuffer();
// Write message fields
putUnsignedByte((byte) MESSAGE_VERSION);
putInt((int) BoatActionType.getBoatPacketType(actionType), 1);
writeCRC();
rewind();
outputStream.write(getBuffer());
}
}
@@ -0,0 +1,32 @@
package seng302.server.messages;
/**
* Created by kre39 on 12/07/17.
*/
public enum BoatActionType {
VMG,
SAILS_IN,
SAILS_OUT,
TACK_GYBE,
UPWIND,
DOWNWIND;
public static Short getBoatPacketType(BoatActionType type){
switch (type){
case VMG:
return 1;
case SAILS_IN:
return 2;
case SAILS_OUT:
return 3;
case TACK_GYBE:
return 4;
case UPWIND:
return 5;
case DOWNWIND:
return 6;
}
return 0;
}
}
@@ -16,7 +16,8 @@ public enum MessageType {
BOAT_LOCATION(37),
MARK_ROUNDING(38),
COURSE_WIND(44),
AVERAGE_WIND(47);
AVERAGE_WIND(47),
BOAT_ACTION(100);
private int code;