mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Key presses are transmitted to a host (but there is no host currently connected)
#pair[kre39,zyt10] #story[988]
This commit is contained in:
@@ -10,13 +10,15 @@ import javafx.scene.input.KeyEvent;
|
|||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.layout.Pane;
|
import javafx.scene.layout.Pane;
|
||||||
import seng302.models.stream.StreamParser;
|
import seng302.models.stream.StreamParser;
|
||||||
import seng302.models.stream.packets.BoatActionPacket;
|
import seng302.server.ClientTransmitterThread;
|
||||||
import seng302.models.stream.packets.BoatActionType;
|
import seng302.server.messages.BoatActionMessage;
|
||||||
|
import seng302.server.messages.BoatActionType;
|
||||||
|
|
||||||
public class Controller implements Initializable {
|
public class Controller implements Initializable {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private AnchorPane contentPane;
|
private AnchorPane contentPane;
|
||||||
|
private ClientTransmitterThread clientTransmitterThread;
|
||||||
|
|
||||||
private void setContentPane(String jfxUrl) {
|
private void setContentPane(String jfxUrl) {
|
||||||
try {
|
try {
|
||||||
@@ -37,29 +39,30 @@ public class Controller implements Initializable {
|
|||||||
contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString());
|
contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString());
|
||||||
setContentPane("/views/StartScreenView.fxml");
|
setContentPane("/views/StartScreenView.fxml");
|
||||||
StreamParser.boatLocations.clear();
|
StreamParser.boatLocations.clear();
|
||||||
|
clientTransmitterThread = new ClientTransmitterThread("RaceVision Test Client Transmitter");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handle the key-pressed event from the text field. */
|
/** Handle the key-pressed event from the text field. */
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
BoatActionPacket boatActionPacket;
|
BoatActionMessage boatActionMessage;
|
||||||
switch (e.getCode()){
|
switch (e.getCode()){
|
||||||
case SPACE: // align with vmg
|
case SPACE: // align with vmg
|
||||||
boatActionPacket = new BoatActionPacket(BoatActionType.VMG);
|
boatActionMessage = new BoatActionMessage(BoatActionType.VMG);
|
||||||
boatActionPacket.sendPacket();
|
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
|
||||||
break;
|
break;
|
||||||
case PAGE_UP: // upwind
|
case PAGE_UP: // upwind
|
||||||
boatActionPacket = new BoatActionPacket(BoatActionType.UPWIND);
|
boatActionMessage = new BoatActionMessage(BoatActionType.UPWIND);
|
||||||
boatActionPacket.sendPacket();
|
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
|
||||||
break;
|
break;
|
||||||
case PAGE_DOWN: // downwind
|
case PAGE_DOWN: // downwind
|
||||||
boatActionPacket = new BoatActionPacket(BoatActionType.DOWNWIND);
|
boatActionMessage = new BoatActionMessage(BoatActionType.DOWNWIND);
|
||||||
boatActionPacket.sendPacket();
|
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
|
||||||
break;
|
break;
|
||||||
case ENTER: // tack/gybe
|
case ENTER: // tack/gybe
|
||||||
boatActionPacket = new BoatActionPacket(BoatActionType.TACK_GYBE);
|
boatActionMessage = new BoatActionMessage(BoatActionType.TACK_GYBE);
|
||||||
boatActionPacket.sendPacket();
|
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
|
||||||
break;
|
break;
|
||||||
//TODO Allow a zoom in and zoom out methods
|
//TODO Allow a zoom in and zoom out methods
|
||||||
case Z: // zoom in
|
case Z: // zoom in
|
||||||
@@ -75,8 +78,8 @@ public class Controller implements Initializable {
|
|||||||
switch (e.getCode()) {
|
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)
|
//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
|
case SHIFT: // sails in/sails out
|
||||||
BoatActionPacket boatActionPacket = new BoatActionPacket(BoatActionType.SAILS_IN);
|
BoatActionMessage boatActionMessage = new BoatActionMessage(BoatActionType.SAILS_IN);
|
||||||
boatActionPacket.sendPacket();
|
clientTransmitterThread.sendBoatActionMessage(boatActionMessage);
|
||||||
break;
|
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{
|
void send(Message message) throws IOException{
|
||||||
if (client == null){
|
if (client == null){
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message.send(client);
|
message.send(client);
|
||||||
|
|
||||||
seqNum++;
|
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
-1
@@ -1,4 +1,4 @@
|
|||||||
package seng302.models.stream.packets;
|
package seng302.server.messages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by kre39 on 12/07/17.
|
* Created by kre39 on 12/07/17.
|
||||||
@@ -16,7 +16,8 @@ public enum MessageType {
|
|||||||
BOAT_LOCATION(37),
|
BOAT_LOCATION(37),
|
||||||
MARK_ROUNDING(38),
|
MARK_ROUNDING(38),
|
||||||
COURSE_WIND(44),
|
COURSE_WIND(44),
|
||||||
AVERAGE_WIND(47);
|
AVERAGE_WIND(47),
|
||||||
|
BOAT_ACTION(100);
|
||||||
|
|
||||||
private int code;
|
private int code;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user