Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
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.connection.ConnectionData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
@@ -11,7 +9,7 @@ import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client extends Controller {
|
||||
public class Client implements Runnable {
|
||||
private final int port;
|
||||
private final String hostname;
|
||||
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;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Server {
|
||||
private Thread sessionThread;
|
||||
private HashMap<String, Thread> gameThreads;
|
||||
|
||||
public void start() {
|
||||
SessionController sessionController = new SessionController();
|
||||
|
||||
this.gameThreads = new HashMap<>();
|
||||
this.sessionThread = new Thread(sessionController);
|
||||
|
||||
run();
|
||||
}
|
||||
|
||||
private void run() {
|
||||
setTestGames();
|
||||
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;
|
||||
|
||||
public class ServerClient implements Runnable, DataSource {
|
||||
private final String name;
|
||||
private final SessionController server;
|
||||
private final DataCallback callback;
|
||||
private final ObjectInputStream in;
|
||||
private final ObjectOutputStream out;
|
||||
private final String name;
|
||||
private final DataCallback callback;
|
||||
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.server = server;
|
||||
this.callback = callback;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.isConnected = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes data to the connected client.
|
||||
* @param data The data object to write.
|
||||
*/
|
||||
public void writeData(Data data) {
|
||||
try {
|
||||
System.out.println("[SERVERCLIENT] writing data " + data);
|
||||
this.out.writeObject(data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -43,19 +48,14 @@ public class ServerClient implements Runnable, DataSource {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Disconnect")) {
|
||||
this.isConnected = false;
|
||||
//todo properly remove thread.
|
||||
this.server.disconnect(this);
|
||||
}
|
||||
} else {
|
||||
// callback.onDataReceived((Data) this.in.readObject());
|
||||
System.out.println("[SERVERCLIENT] got data: " + data + ", sending callback");
|
||||
callback.onDataReceived(data, this);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("[SERVERCLIENT] caught exception - " + e.getMessage());
|
||||
System.out.println("[SERVERCLIENT] terminating failing connection...");
|
||||
this.isConnected = false;
|
||||
System.out.println("[SERVERCLIENT] done!");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.connection.ConnectionData;
|
||||
import netwerkprog.game.util.data.connection.NameData;
|
||||
@@ -11,19 +10,17 @@ import java.io.ObjectOutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
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.
|
||||
*/
|
||||
public class SessionController extends Controller implements DataCallback {
|
||||
public class SessionController implements DataCallback, Runnable {
|
||||
private ServerSocket serverSocket;
|
||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||
private final ArrayList<ServerClient> clients;
|
||||
private boolean listening;
|
||||
|
||||
public SessionController() {
|
||||
this.clients = new ArrayList<>();
|
||||
this.listening = true;
|
||||
}
|
||||
|
||||
@@ -58,98 +55,35 @@ public class SessionController extends Controller implements DataCallback {
|
||||
*/
|
||||
public void registerClient(Socket socket) {
|
||||
try {
|
||||
System.out.println("[SERVER] got new client on " + socket.getInetAddress().getHostAddress());
|
||||
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
|
||||
|
||||
String username = "";
|
||||
String username;
|
||||
boolean registering = true;
|
||||
|
||||
while (registering) {
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Please give a username"));
|
||||
Object object = inputStream.readObject();
|
||||
|
||||
if (object instanceof Data) {
|
||||
Data data = (Data) object;
|
||||
if (data instanceof ConnectionData) {
|
||||
ConnectionData connectionData = (ConnectionData) data.getPayload();
|
||||
if (connectionData.getAction().equals("Connect")) {
|
||||
username = connectionData.getMessage();
|
||||
outputStream.writeObject(new ConnectionData("Connect", "Confirm"));
|
||||
registering = false;
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
} else {
|
||||
//todo error messaging.
|
||||
}
|
||||
}
|
||||
|
||||
username = "player" + (this.clients.size() + 1);
|
||||
System.out.println("[SERVER] set username " + username);
|
||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this);
|
||||
|
||||
ServerClient serverClient = new ServerClient(username, this, this, inputStream, outputStream);
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
|
||||
serverClient.writeData(new NameData(username));
|
||||
this.clientThreads.put(username,t);
|
||||
this.clients.add(serverClient);
|
||||
} catch (IOException | ClassNotFoundException ex) {
|
||||
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.
|
||||
*/
|
||||
@@ -160,7 +94,14 @@ public class SessionController extends Controller implements DataCallback {
|
||||
} catch (IOException e) {
|
||||
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
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.application;
|
||||
|
||||
public abstract class Controller implements Runnable {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user