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
@@ -10,13 +10,15 @@ import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import seng302.models.stream.StreamParser;
import seng302.models.stream.packets.BoatActionPacket;
import seng302.models.stream.packets.BoatActionType;
import seng302.server.ClientTransmitterThread;
import seng302.server.messages.BoatActionMessage;
import seng302.server.messages.BoatActionType;
public class Controller implements Initializable {
@FXML
private AnchorPane contentPane;
private ClientTransmitterThread clientTransmitterThread;
private void setContentPane(String jfxUrl) {
try {
@@ -37,29 +39,30 @@ public class Controller implements Initializable {
contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString());
setContentPane("/views/StartScreenView.fxml");
StreamParser.boatLocations.clear();
clientTransmitterThread = new ClientTransmitterThread("RaceVision Test Client Transmitter");
}
/** Handle the key-pressed event from the text field. */
public void keyPressed(KeyEvent e) {
BoatActionPacket boatActionPacket;
BoatActionMessage boatActionMessage;
switch (e.getCode()){
case SPACE: // align with vmg
boatActionPacket = new BoatActionPacket(BoatActionType.VMG);
boatActionPacket.sendPacket();
boatActionMessage = new BoatActionMessage(BoatActionType.VMG);
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
break;
case PAGE_UP: // upwind
boatActionPacket = new BoatActionPacket(BoatActionType.UPWIND);
boatActionPacket.sendPacket();
boatActionMessage = new BoatActionMessage(BoatActionType.UPWIND);
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
break;
case PAGE_DOWN: // downwind
boatActionPacket = new BoatActionPacket(BoatActionType.DOWNWIND);
boatActionPacket.sendPacket();
boatActionMessage = new BoatActionMessage(BoatActionType.DOWNWIND);
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
break;
case ENTER: // tack/gybe
boatActionPacket = new BoatActionPacket(BoatActionType.TACK_GYBE);
boatActionPacket.sendPacket();
boatActionMessage = new BoatActionMessage(BoatActionType.TACK_GYBE);
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
break;
//TODO Allow a zoom in and zoom out methods
case Z: // zoom in
@@ -75,8 +78,8 @@ public class Controller implements Initializable {
switch (e.getCode()) {
//TODO 12/07/17 Determine the sail state and send the appropriate packet (eg. if sails are in, send a sail out packet)
case SHIFT: // sails in/sails out
BoatActionPacket boatActionPacket = new BoatActionPacket(BoatActionType.SAILS_IN);
boatActionPacket.sendPacket();
BoatActionMessage boatActionMessage = new BoatActionMessage(BoatActionType.SAILS_IN);
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
break;
}
}
@@ -1,18 +0,0 @@
package seng302.models.stream.packets;
/**
* Created by kre39 on 12/07/17.
*/
public class BoatActionPacket {
BoatActionType actionType;
public BoatActionPacket(BoatActionType actionType) {
this.actionType = actionType;
}
// Sends the packet to the server
public void sendPacket(){
System.out.println(BoatActionType.getBoatPacketType(actionType));
}
}
@@ -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());
}
}
@@ -1,4 +1,4 @@
package seng302.models.stream.packets;
package seng302.server.messages;
/**
* Created by kre39 on 12/07/17.
@@ -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;