Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
package netwerkprog.game.client.game.connections;
|
package netwerkprog.game.client.game.connections;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
|
||||||
import netwerkprog.game.util.application.Controller;
|
|
||||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
|
||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
|
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
@@ -11,7 +9,7 @@ import java.io.ObjectOutputStream;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Client extends Controller {
|
public class Client implements Runnable {
|
||||||
private final int port;
|
private final int port;
|
||||||
private final String hostname;
|
private final String hostname;
|
||||||
private boolean isConnected;
|
private boolean isConnected;
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package netwerkprog.game.client.game.logic;
|
|
||||||
|
|
||||||
import netwerkprog.game.util.application.Controller;
|
|
||||||
|
|
||||||
public class Logic extends Controller {
|
|
||||||
|
|
||||||
public Logic() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +1,15 @@
|
|||||||
package netwerkprog.game.server;
|
package netwerkprog.game.server;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
private Thread sessionThread;
|
private Thread sessionThread;
|
||||||
private HashMap<String, Thread> gameThreads;
|
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
SessionController sessionController = new SessionController();
|
SessionController sessionController = new SessionController();
|
||||||
|
|
||||||
this.gameThreads = new HashMap<>();
|
|
||||||
this.sessionThread = new Thread(sessionController);
|
this.sessionThread = new Thread(sessionController);
|
||||||
|
|
||||||
run();
|
run();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void run() {
|
private void run() {
|
||||||
setTestGames();
|
|
||||||
this.sessionThread.start();
|
this.sessionThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setTestGames() {
|
|
||||||
// for (int i = 0; i < 10; i++) {
|
|
||||||
// gameThreads.put("game " + i, new Thread(new GameController(i)));
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// for (String game : gameThreads.keySet()) {
|
|
||||||
// gameThreads.get(game).start();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,23 +8,28 @@ import java.io.ObjectInputStream;
|
|||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
public class ServerClient implements Runnable, DataSource {
|
public class ServerClient implements Runnable, DataSource {
|
||||||
|
private final String name;
|
||||||
|
private final SessionController server;
|
||||||
|
private final DataCallback callback;
|
||||||
private final ObjectInputStream in;
|
private final ObjectInputStream in;
|
||||||
private final ObjectOutputStream out;
|
private final ObjectOutputStream out;
|
||||||
private final String name;
|
|
||||||
private final DataCallback callback;
|
|
||||||
private boolean isConnected;
|
private boolean isConnected;
|
||||||
|
|
||||||
public ServerClient(String name, ObjectInputStream in, ObjectOutputStream out, DataCallback callback) {
|
public ServerClient(String name, SessionController server, DataCallback callback, ObjectInputStream in, ObjectOutputStream out) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.server = server;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.isConnected = true;
|
this.isConnected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes data to the connected client.
|
||||||
|
* @param data The data object to write.
|
||||||
|
*/
|
||||||
public void writeData(Data data) {
|
public void writeData(Data data) {
|
||||||
try {
|
try {
|
||||||
System.out.println("[SERVERCLIENT] writing data " + data);
|
|
||||||
this.out.writeObject(data);
|
this.out.writeObject(data);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -43,19 +48,14 @@ public class ServerClient implements Runnable, DataSource {
|
|||||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||||
if (connectionData.getAction().equals("Disconnect")) {
|
if (connectionData.getAction().equals("Disconnect")) {
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
//todo properly remove thread.
|
this.server.disconnect(this);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// callback.onDataReceived((Data) this.in.readObject());
|
|
||||||
System.out.println("[SERVERCLIENT] got data: " + data + ", sending callback");
|
|
||||||
callback.onDataReceived(data, this);
|
callback.onDataReceived(data, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
|
||||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
|
||||||
this.isConnected = false;
|
this.isConnected = false;
|
||||||
System.out.println("[SERVERCLIENT] done!");
|
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package netwerkprog.game.server;
|
package netwerkprog.game.server;
|
||||||
|
|
||||||
import netwerkprog.game.util.application.Controller;
|
|
||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||||
import netwerkprog.game.util.data.connection.NameData;
|
import netwerkprog.game.util.data.connection.NameData;
|
||||||
@@ -11,19 +10,17 @@ import java.io.ObjectOutputStream;
|
|||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The sessionController manages any connections from new clients and assigns individual threads to said clients.
|
* The sessionController manages any connections from new clients and assigns individual threads to said clients.
|
||||||
*/
|
*/
|
||||||
public class SessionController extends Controller implements DataCallback {
|
public class SessionController implements DataCallback, Runnable {
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
private final ArrayList<ServerClient> clients;
|
||||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
|
||||||
private boolean listening;
|
private boolean listening;
|
||||||
|
|
||||||
public SessionController() {
|
public SessionController() {
|
||||||
|
this.clients = new ArrayList<>();
|
||||||
this.listening = true;
|
this.listening = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,98 +55,35 @@ public class SessionController extends Controller implements DataCallback {
|
|||||||
*/
|
*/
|
||||||
public void registerClient(Socket socket) {
|
public void registerClient(Socket socket) {
|
||||||
try {
|
try {
|
||||||
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
|
|
||||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||||
|
String username;
|
||||||
String username = "";
|
|
||||||
boolean registering = true;
|
boolean registering = true;
|
||||||
|
|
||||||
while (registering) {
|
while (registering) {
|
||||||
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
||||||
Object object = inputStream.readObject();
|
Object object = inputStream.readObject();
|
||||||
|
|
||||||
if (object instanceof Data) {
|
if (object instanceof Data) {
|
||||||
Data data = (Data) object;
|
Data data = (Data) object;
|
||||||
if (data instanceof ConnectionData) {
|
if (data instanceof ConnectionData) {
|
||||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||||
if (connectionData.getAction().equals("Connect")) {
|
if (connectionData.getAction().equals("Connect")) {
|
||||||
username = connectionData.getMessage();
|
|
||||||
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
||||||
registering = false;
|
registering = false;
|
||||||
} else {
|
|
||||||
//todo error messaging.
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
//todo error messaging.
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
//todo error messaging.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
username = "player" + (this.clients.size() + 1);
|
username = "player" + (this.clients.size() + 1);
|
||||||
System.out.println("[SERVER] set username " + username);
|
ServerClient serverClient = new ServerClient(username, this, this, inputStream, outputStream);
|
||||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this);
|
|
||||||
|
|
||||||
Thread t = new Thread(serverClient);
|
Thread t = new Thread(serverClient);
|
||||||
t.start();
|
t.start();
|
||||||
|
|
||||||
serverClient.writeData(new NameData(username));
|
serverClient.writeData(new NameData(username));
|
||||||
this.clientThreads.put(username,t);
|
|
||||||
this.clients.add(serverClient);
|
this.clients.add(serverClient);
|
||||||
} catch (IOException | ClassNotFoundException ex) {
|
} catch (IOException | ClassNotFoundException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a server message to all connected clients.
|
|
||||||
* @param data message.
|
|
||||||
*/
|
|
||||||
public void serverMessage(Data data) {
|
|
||||||
for (ServerClient serverClient : clients) {
|
|
||||||
serverClient.writeData(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message to a specific user.
|
|
||||||
* @param name user.
|
|
||||||
* @param data message.
|
|
||||||
*/
|
|
||||||
public void personalMessage(String name, Data data) {
|
|
||||||
for (ServerClient serverClient : clients) {
|
|
||||||
if (serverClient.getName().equals(name)) {
|
|
||||||
serverClient.writeData(data);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Removes a client from the server.
|
|
||||||
* @param serverClient The client to remove.
|
|
||||||
*/
|
|
||||||
public void removeClient(ServerClient serverClient) {
|
|
||||||
this.clients.remove(serverClient);
|
|
||||||
try {
|
|
||||||
this.clientThreads.get(serverClient.getName()).join();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
this.clientThreads.remove(serverClient.getName());
|
|
||||||
//this.serverMessage(serverClient.getName() + " left!");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets a list of all connected users.
|
|
||||||
* @return Set of all connected users.
|
|
||||||
*/
|
|
||||||
public Set<String> getUsernames() {
|
|
||||||
return this.clientThreads.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shuts down the sessionController.
|
* Shuts down the sessionController.
|
||||||
*/
|
*/
|
||||||
@@ -160,7 +94,14 @@ public class SessionController extends Controller implements DataCallback {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
System.out.println("[SERVER] networking shutdown ");
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnects the client from the server list.
|
||||||
|
* @param client The Client to disconnect.
|
||||||
|
*/
|
||||||
|
public void disconnect(ServerClient client) {
|
||||||
|
this.clients.remove(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package netwerkprog.game.util.application;
|
|
||||||
|
|
||||||
public abstract class Controller implements Runnable {
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user