Removed DataController from server
This commit is contained in:
@@ -1,19 +1,13 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Server {
|
||||
private SessionController sessionController;
|
||||
private DataController dataController;
|
||||
private Thread sessionThread;
|
||||
private HashMap<String, Thread> gameThreads;
|
||||
|
||||
public void start() {
|
||||
this.sessionController = new SessionController(this);
|
||||
this.dataController = new DataController(sessionController);
|
||||
SessionController sessionController = new SessionController();
|
||||
|
||||
this.gameThreads = new HashMap<>();
|
||||
this.sessionThread = new Thread(sessionController);
|
||||
@@ -35,12 +29,4 @@ public class Server {
|
||||
// gameThreads.get(game).start();
|
||||
// }
|
||||
}
|
||||
|
||||
public DataController getDataController() {
|
||||
return dataController;
|
||||
}
|
||||
|
||||
public void setDataController(DataController dataController) {
|
||||
this.dataController = dataController;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.controllers.DataController;
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataCallback;
|
||||
@@ -12,17 +10,15 @@ import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ServerClient implements Runnable, DataSource {
|
||||
private ObjectInputStream in;
|
||||
private ObjectOutputStream out;
|
||||
private final ObjectInputStream in;
|
||||
private final ObjectOutputStream out;
|
||||
private final String name;
|
||||
private final SessionController server;
|
||||
private final DataCallback callback;
|
||||
private boolean isConnected;
|
||||
|
||||
public ServerClient(String name, ObjectInputStream in, ObjectOutputStream out, SessionController server, DataController dataController) {
|
||||
public ServerClient(String name, ObjectInputStream in, ObjectOutputStream out, DataCallback callback) {
|
||||
this.name = name;
|
||||
this.server = server;
|
||||
this.callback = dataController;
|
||||
this.callback = callback;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
this.isConnected = true;
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
package netwerkprog.game.server;
|
||||
|
||||
import netwerkprog.game.server.Server;
|
||||
import netwerkprog.game.server.ServerClient;
|
||||
import netwerkprog.game.util.application.Controller;
|
||||
import netwerkprog.game.util.data.ConnectionData;
|
||||
import netwerkprog.game.util.data.Data;
|
||||
import netwerkprog.game.util.data.DataChangeCallback;
|
||||
import netwerkprog.game.util.data.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
@@ -19,15 +15,13 @@ 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 DataChangeCallback {
|
||||
private Server server;
|
||||
public class SessionController extends Controller implements DataCallback {
|
||||
private ServerSocket serverSocket;
|
||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||
private boolean listening;
|
||||
|
||||
public SessionController(Server server) {
|
||||
this.server = server;
|
||||
public SessionController() {
|
||||
this.listening = true;
|
||||
}
|
||||
|
||||
@@ -93,7 +87,7 @@ public class SessionController extends Controller implements DataChangeCallback
|
||||
}
|
||||
|
||||
System.out.println("[SERVER] got username " + username);
|
||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this, server.getDataController());
|
||||
ServerClient serverClient = new ServerClient(username, inputStream, outputStream, this);
|
||||
|
||||
Thread t = new Thread(serverClient);
|
||||
t.start();
|
||||
@@ -166,7 +160,7 @@ public class SessionController extends Controller implements DataChangeCallback
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataChange(Data data) {
|
||||
serverMessage(data);
|
||||
public void onDataReceived(Data data, DataSource source) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package netwerkprog.game.server.controllers;
|
||||
|
||||
import netwerkprog.game.util.data.*;
|
||||
import netwerkprog.game.util.game.GameCharacter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class DataController implements DataCallback {
|
||||
private final DataChangeCallback callback;
|
||||
private final HashSet<GameCharacter> gameCharacters;
|
||||
|
||||
public DataController(DataChangeCallback callback) {
|
||||
this.callback = callback;
|
||||
gameCharacters = new HashSet<>();
|
||||
}
|
||||
|
||||
public void addCharacter(GameCharacter gameCharacter) {
|
||||
this.gameCharacters.add(gameCharacter);
|
||||
callback.onDataChange(new CharacterData(gameCharacter.getName(), gameCharacter));
|
||||
}
|
||||
|
||||
public void addAllCharacters(GameCharacter... gameCharacters) {
|
||||
this.gameCharacters.addAll(Arrays.asList(gameCharacters));
|
||||
}
|
||||
|
||||
public void removeCharacter(String name) {
|
||||
this.gameCharacters.removeIf(character -> character.getName().equals(name));
|
||||
callback.onDataChange(new CharacterData(name, getCharacter(name))); //todo modify Character data to allow for removal.
|
||||
}
|
||||
|
||||
public void removeCharacter(GameCharacter gameCharacter) {
|
||||
this.gameCharacters.remove(gameCharacter);
|
||||
callback.onDataChange(new CharacterData(gameCharacter.getName(), gameCharacter)); //todo modify Character data to allow for removal.
|
||||
}
|
||||
|
||||
public void clearCharacters() {
|
||||
this.gameCharacters.clear();
|
||||
}
|
||||
|
||||
public HashSet<GameCharacter> getGameCharacters() {
|
||||
return gameCharacters;
|
||||
}
|
||||
|
||||
public GameCharacter getCharacter(String name) throws IllegalArgumentException {
|
||||
for (GameCharacter character : gameCharacters) {
|
||||
if (character.getName().equals(name)) {
|
||||
return character;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("The character does not exist.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataReceived(Data data, DataSource source) {
|
||||
System.out.println("[DATACONTROLLER] got data: " + data);
|
||||
source.writeData(data);
|
||||
switch (data.getType()) {
|
||||
case "Character" :
|
||||
if (data.getPayload() instanceof CharacterData) {
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package netwerkprog.game.util.data;
|
||||
|
||||
public interface DataChangeCallback {
|
||||
void onDataChange(Data data);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import netwerkprog.game.server.controllers.SessionController;
|
||||
import netwerkprog.game.server.SessionController;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RestartSessionControllerTest {
|
||||
|
||||
Reference in New Issue
Block a user