Skip to content
Snippets Groups Projects
Commit 1464c490 authored by Elrabu's avatar Elrabu
Browse files

Update: Added Getter/Setter to the ConnectionHandler class and added a stop...

Update: Added Getter/Setter to the ConnectionHandler class and added a stop method for the Thread in the server class.
parent 0ebac858
No related branches found
No related tags found
2 merge requests!35Update: Added more Stuff to be transported over the server.,!2Merge of Multiplayer Feature into development branch
package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import de.hdm_stuttgart.battlearena.Model.Multiplayer.ConnectionHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -13,7 +12,7 @@ public class Client {
private PrintWriter out;
private BufferedReader in;
private static final Logger log = LogManager.getLogger(ConnectionHandler.class);
private static final Logger log = LogManager.getLogger(Client.class);
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
......
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.io.IOException;
public class ConnectionHandler {
private int playerX;
private int playerY;
private int pX;
private int pY;
private int enemyX;
private int enemyY;
private static final Logger log = LogManager.getLogger(ConnectionHandler.class);
private static int[] coordinates = {5,10};
public int getPlayerX() {
return playerX;
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
int[] cords = client.sendcords(coordinates);
System.out.println(cords);
//stop the connection:
client.stopConnection();
}
public int getpX() {
return pX;
}
public void setpX(int pX) {
this.pX = pX;
}
public int getpY() {
return pY;
}
public int getPlayerY() {
return playerY;
public void setpY(int pY) {
this.pY = pY;
}
public int getEnemyX() {
return enemyX;
}
public void setEnemyX(int enemyX) {
this.enemyX = enemyX;
}
public int getEnemyY() {
return enemyY;
}
public void setEnemyY(int enemyY) {
this.enemyY = enemyY;
}
}
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class GameServer {
private static final Logger log = LogManager.getLogger(GameServer.class);
private ServerSocket socket;
private int numPlayers;
private int maxPlayers;
private Socket p1Socket;
private Socket p2Socket;
private ReadFromClient p1ReadRunnable;
private ReadFromClient p2ReadRunnable;
private WriteToClient p1WriteRunnable;
private WriteToClient p2WriteRunnable;
private double p1x, p1y, p2x, p2y;
public GameServer() {
System.out.println("========= GAME SERVER =========");
numPlayers = 0;
maxPlayers = 2;
p1x = 100;
p1y = 400;
p2x = 490;
p2y = 400;
try {
socket = new ServerSocket(45371);
} catch (IOException ex) {
System.out.println("IOException from Game Server constructor!");
}
}
public void acceptConnections(){
try {
System.out.println("Waiting for conection...");
while (numPlayers < maxPlayers){
Socket s = socket.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());
numPlayers++;
out.writeInt(numPlayers);
System.out.println("Player #"+ numPlayers + "has connected.");
ReadFromClient rfc = new ReadFromClient(numPlayers, in);
WriteToClient wtc = new WriteToClient(numPlayers, out);
if(numPlayers == 1) {
p1Socket = s;
p1ReadRunnable = rfc;
p1WriteRunnable = wtc;
} else {
p2Socket = s;
p2ReadRunnable = rfc;
p2WriteRunnable = wtc;
p1WriteRunnable.sendStartMsg();
p2WriteRunnable.sendStartMsg();
Thread readThread1 = new Thread(p1ReadRunnable);
Thread readThread2 = new Thread(p2ReadRunnable);
readThread1.start();
readThread2.start();
Thread writeThread1 = new Thread(p1WriteRunnable);
Thread writeThread2 = new Thread(p2WriteRunnable);
writeThread1.start();
writeThread2.start();
}
}
System.out.println("No longer accepting connections");
}catch (IOException ex){
System.out.println("IOException from acceptConnect");
}
}
private class ReadFromClient implements Runnable {
private int playerID;
private DataInputStream dataIn;
public ReadFromClient( int pid, DataInputStream in) {
playerID = pid;
dataIn = in;
System.out.println("RFC" + playerID + "Runnable created");
}
public void run() {
try {
while (true) {
if(playerID == 1){
p1x = dataIn.readDouble();
p1y = dataIn.readDouble();
} else {
p2x = dataIn.readDouble();
p2y = dataIn.readDouble();
}
}
}catch (IOException ex){
System.out.println("IOException from RFC run()");
}
}
}
private class WriteToClient implements Runnable {
private int playerID;
private DataOutputStream dataOut;
public WriteToClient(int pid, DataOutputStream out) {
playerID = pid;
dataOut = out;
System.out.println("WTC" + playerID + "Runnable created");
}
public void run() {
try {
while(true) {
if(playerID == 1) {
dataOut.writeDouble(p2x);
dataOut.writeDouble(p2y);
dataOut.flush();
} else {
dataOut.writeDouble(p1x);
dataOut.writeDouble(p1y);
dataOut.flush();
}
try {
Thread.sleep(25);
} catch(InterruptedException ex){
System.out.println("InterruptedException from WTC run()");
}
}
} catch (IOException ex) {
System.out.println("IOException from WTC run()");
}
}
public void sendStartMsg() {
try {
dataOut.writeUTF("We now have 2 players. Go!");
} catch (IOException ex) {
System.out.println("IOException from sendStartMsg()");
}
}
}
public static void main(String[] args) {
GameServer gs = new GameServer();
gs.acceptConnections();
}
}
package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
package de.hdm_stuttgart.battlearena.Model.Multiplayer;
import de.hdm_stuttgart.battlearena.Model.Multiplayer.GameServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.net.*;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
......@@ -15,9 +13,13 @@ public class Server {
private static final Logger log = LogManager.getLogger(Server.class);
private ServerSocket serverSocket;
private static boolean isServerRunning = true;
private static int px;
private static int py;
private static int enemyx;
private static int enemyy;
private static int[] cords;
public static void main(String[] args) throws IOException {
......@@ -29,16 +31,9 @@ public class Server {
public void start(String lolcalhost, int port) throws IOException {
serverSocket = new ServerSocket(port);
log.info("server started!");
while (true)
new ServerHandler(serverSocket.accept()).start();
}
public void stop() throws IOException {
serverSocket.close();
new ServerHandler(serverSocket.accept()).start();
}
// EchoClientHandler class in the Server
private static class ServerHandler extends Thread {
private Socket clientSocket;
private PrintWriter out;
......@@ -71,6 +66,18 @@ public class Server {
log.info(cords);
//check if server was shut down:
if (!isServerRunning) {
executorService.shutdown();
try {
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
log.error("Error waiting for executor service termination: " + e.getMessage());
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
......@@ -78,4 +85,13 @@ public class Server {
}
}
public void stopServer() {
isServerRunning = false;
try {
serverSocket.close(); // Close the server socket to break out of accept() in the main thread
} catch (IOException e) {
log.error("Error closing server socket: " + e.getMessage());
}
}
}
package de.hdm_stuttgart.battlearena.Model.Multiplayer.TestServer;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.concurrent.TimeUnit;
public class ConnectionHandling {
private int px;
private int py;
private static int[] coordinates = {5,10};
public static void main(String[] args) throws IOException, InterruptedException {
Client client = new Client();
client.startConnection("localhost", 4444);
int[] cords = client.sendcords(coordinates);
System.out.println(cords);
}
public int getPx() {
return px;
}
public int getPy() {
return py;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment