- StartController sets up client-wide ControllerClient on socket to game server. - RaceController now calls ControllerClient.sendKey with each keypress - ConnectionAcceptor runs ControllerServer for each successfully connected client #story[1089]main
parent
9a76fa592c
commit
cc264f318e
@ -0,0 +1,46 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic service for sending key presses to game server
|
||||||
|
*/
|
||||||
|
public class ControllerClient {
|
||||||
|
/**
|
||||||
|
* Socket to server
|
||||||
|
*/
|
||||||
|
Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output stream wrapper for socket to server
|
||||||
|
*/
|
||||||
|
DataOutputStream outputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise controller client with live socket.
|
||||||
|
* @param socket to server
|
||||||
|
*/
|
||||||
|
public ControllerClient(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
try {
|
||||||
|
this.outputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a keypress to server
|
||||||
|
* @param key to send
|
||||||
|
* @throws IOException if socket write fails
|
||||||
|
*/
|
||||||
|
public void sendKey(ControlKey key) throws IOException {
|
||||||
|
// TODO - get and send action number currently corresponding to key (context dependent)
|
||||||
|
System.out.println(key.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
import visualiser.gameController.Keys.KeyFactory;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for dispatching key press data to race from client
|
||||||
|
*/
|
||||||
|
public class ControllerServer implements Runnable {
|
||||||
|
/**
|
||||||
|
* Socket to client
|
||||||
|
*/
|
||||||
|
private Socket socket;
|
||||||
|
/**
|
||||||
|
* Wrapper for input from client
|
||||||
|
*/
|
||||||
|
private DataInputStream inputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise server-side controller with live client socket
|
||||||
|
* @param socket to client
|
||||||
|
*/
|
||||||
|
public ControllerServer(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
try {
|
||||||
|
this.inputStream = new DataInputStream(this.socket.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for controller key input from client and loop.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while(true) {
|
||||||
|
byte[] key = new byte[1];
|
||||||
|
try {
|
||||||
|
inputStream.read(key);
|
||||||
|
// TODO - handle messages received
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue