mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Minor refactor, threads now start themselves
tags: #story[989] #refactor
This commit is contained in:
@@ -15,7 +15,10 @@ import seng302.server.messages.Message;
|
|||||||
/**
|
/**
|
||||||
* Created by kre39 on 13/07/17.
|
* Created by kre39 on 13/07/17.
|
||||||
*/
|
*/
|
||||||
public class ClientToServerThread extends Thread {
|
public class ClientToServerThread implements Runnable {
|
||||||
|
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private InputStream is;
|
private InputStream is;
|
||||||
private OutputStream os;
|
private OutputStream os;
|
||||||
@@ -33,6 +36,9 @@ public class ClientToServerThread extends Thread {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
thread = new Thread(this);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clientLog(String message, int logLevel){
|
static void clientLog(String message, int logLevel){
|
||||||
@@ -139,4 +145,8 @@ public class ClientToServerThread extends Thread {
|
|||||||
readByte();
|
readByte();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public Thread getThread() {
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -62,10 +62,9 @@ public class StartScreenController {
|
|||||||
try {
|
try {
|
||||||
String ipAddress = InetAddress.getLocalHost().getHostAddress();
|
String ipAddress = InetAddress.getLocalHost().getHostAddress();
|
||||||
new GameState(ipAddress);
|
new GameState(ipAddress);
|
||||||
new MainServerThread().start();
|
new MainServerThread();
|
||||||
// get the lobby controller so that we can pass the game server thread to it
|
// get the lobby controller so that we can pass the game server thread to it
|
||||||
setContentPane("/views/LobbyView.fxml");
|
setContentPane("/views/LobbyView.fxml");
|
||||||
|
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
System.err.println("COULD NOT FIND YOUR IP ADDRESS!");
|
System.err.println("COULD NOT FIND YOUR IP ADDRESS!");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -79,9 +78,9 @@ public class StartScreenController {
|
|||||||
// TODO: 10/07/17 wmu16 - Finish function
|
// TODO: 10/07/17 wmu16 - Finish function
|
||||||
String ipAddress = ipTextField.getText().trim().toLowerCase();
|
String ipAddress = ipTextField.getText().trim().toLowerCase();
|
||||||
try {
|
try {
|
||||||
|
// TODO: 22/07/17 wmu 16 - make this port number some static constant somewhere perhaps a config file?
|
||||||
ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, 4950);
|
ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, 4950);
|
||||||
controller.setClientToServerThread(clientToServerThread);
|
controller.setClientToServerThread(clientToServerThread);
|
||||||
clientToServerThread.start();
|
|
||||||
setContentPane("/views/LobbyView.fxml");
|
setContentPane("/views/LobbyView.fxml");
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -15,12 +15,14 @@ import java.util.concurrent.PriorityBlockingQueue;
|
|||||||
* A class describing the overall server, which creates and collects server threads for each client
|
* A class describing the overall server, which creates and collects server threads for each client
|
||||||
* Created by wmu16 on 13/07/17.
|
* Created by wmu16 on 13/07/17.
|
||||||
*/
|
*/
|
||||||
public class MainServerThread extends Thread implements PacketBufferDelegate, ClientConnectionDelegate{
|
public class MainServerThread implements Runnable, PacketBufferDelegate, ClientConnectionDelegate{
|
||||||
|
|
||||||
private static final int PORT = 4950;
|
private static final int PORT = 4950;
|
||||||
private static final Integer MAX_NUM_PLAYERS = 3;
|
private static final Integer MAX_NUM_PLAYERS = 3;
|
||||||
private static final int LOG_LEVEL = 1;
|
private static final int LOG_LEVEL = 1;
|
||||||
|
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
private ServerSocket serverSocket = null;
|
private ServerSocket serverSocket = null;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
|
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
|
||||||
@@ -36,6 +38,9 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
|
|||||||
}
|
}
|
||||||
|
|
||||||
packetBuffer = new PriorityBlockingQueue<>();
|
packetBuffer = new PriorityBlockingQueue<>();
|
||||||
|
|
||||||
|
thread = new Thread(this);
|
||||||
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -50,7 +55,7 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
|
|||||||
serverListenThread.start();
|
serverListenThread.start();
|
||||||
|
|
||||||
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
|
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
|
||||||
while (!isInterrupted()) {
|
while (!thread.isInterrupted()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000 / 60); //60 times per second we should calculate the game state
|
Thread.sleep(1000 / 60); //60 times per second we should calculate the game state
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -122,7 +127,7 @@ public class MainServerThread extends Thread implements PacketBufferDelegate, Cl
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void clientConnected(ServerToClientThread serverToClientThread) {
|
public void clientConnected(ServerToClientThread serverToClientThread) {
|
||||||
serverLog("Player Connected From " + serverToClientThread.getName(), 0);
|
serverLog("Player Connected From " + serverToClientThread.getThread().getName(), 0);
|
||||||
serverToClientThreads.add(serverToClientThread);
|
serverToClientThreads.add(serverToClientThread);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ public class ServerListenThread extends Thread{
|
|||||||
Socket thisClient = serverSocket.accept();
|
Socket thisClient = serverSocket.accept();
|
||||||
if (thisClient != null){
|
if (thisClient != null){
|
||||||
ServerToClientThread thisConnection = new ServerToClientThread(thisClient);
|
ServerToClientThread thisConnection = new ServerToClientThread(thisClient);
|
||||||
thisConnection.start();
|
|
||||||
delegate.clientConnected(thisConnection);
|
delegate.clientConnected(thisConnection);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -20,11 +20,13 @@ import seng302.utilities.GeoPoint;
|
|||||||
* All server threads created and owned by the server thread handler which can trigger client updates on its threads
|
* All server threads created and owned by the server thread handler which can trigger client updates on its threads
|
||||||
* Created by wmu16 on 13/07/17.
|
* Created by wmu16 on 13/07/17.
|
||||||
*/
|
*/
|
||||||
public class ServerToClientThread extends Thread {
|
public class ServerToClientThread implements Runnable {
|
||||||
|
|
||||||
private static final Integer LOG_LEVEL = 1;
|
private static final Integer LOG_LEVEL = 1;
|
||||||
private static final Integer MAX_ID_ATTEMPTS = 10;
|
private static final Integer MAX_ID_ATTEMPTS = 10;
|
||||||
|
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
private InputStream is;
|
private InputStream is;
|
||||||
private OutputStream os;
|
private OutputStream os;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
@@ -50,6 +52,9 @@ public class ServerToClientThread extends Thread {
|
|||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
sourceId = rand.nextInt(100000);
|
sourceId = rand.nextInt(100000);
|
||||||
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
|
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0));
|
||||||
|
|
||||||
|
thread = new Thread(this);
|
||||||
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -185,4 +190,9 @@ public class ServerToClientThread extends Thread {
|
|||||||
readByte();
|
readByte();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Thread getThread() {
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user