Removed DataController from server
This commit is contained in:
@@ -1,19 +1,13 @@
|
|||||||
package netwerkprog.game.server;
|
package netwerkprog.game.server;
|
||||||
|
|
||||||
import netwerkprog.game.server.controllers.DataController;
|
|
||||||
import netwerkprog.game.server.controllers.SessionController;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
private SessionController sessionController;
|
|
||||||
private DataController dataController;
|
|
||||||
private Thread sessionThread;
|
private Thread sessionThread;
|
||||||
private HashMap<String, Thread> gameThreads;
|
private HashMap<String, Thread> gameThreads;
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
this.sessionController = new SessionController(this);
|
SessionController sessionController = new SessionController();
|
||||||
this.dataController = new DataController(sessionController);
|
|
||||||
|
|
||||||
this.gameThreads = new HashMap<>();
|
this.gameThreads = new HashMap<>();
|
||||||
this.sessionThread = new Thread(sessionController);
|
this.sessionThread = new Thread(sessionController);
|
||||||
@@ -35,12 +29,4 @@ public class Server {
|
|||||||
// gameThreads.get(game).start();
|
// 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;
|
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.ConnectionData;
|
||||||
import netwerkprog.game.util.data.Data;
|
import netwerkprog.game.util.data.Data;
|
||||||
import netwerkprog.game.util.data.DataCallback;
|
import netwerkprog.game.util.data.DataCallback;
|
||||||
@@ -12,17 +10,15 @@ 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 ObjectInputStream in;
|
private final ObjectInputStream in;
|
||||||
private ObjectOutputStream out;
|
private final ObjectOutputStream out;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final SessionController server;
|
|
||||||
private final DataCallback callback;
|
private final DataCallback callback;
|
||||||
private boolean isConnected;
|
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.name = name;
|
||||||
this.server = server;
|
this.callback = callback;
|
||||||
this.callback = dataController;
|
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.isConnected = true;
|
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.application.Controller;
|
||||||
import netwerkprog.game.util.data.ConnectionData;
|
import netwerkprog.game.util.data.*;
|
||||||
import netwerkprog.game.util.data.Data;
|
|
||||||
import netwerkprog.game.util.data.DataChangeCallback;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
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.
|
* The sessionController manages any connections from new clients and assigns individual threads to said clients.
|
||||||
*/
|
*/
|
||||||
public class SessionController extends Controller implements DataChangeCallback {
|
public class SessionController extends Controller implements DataCallback {
|
||||||
private Server server;
|
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
private final ArrayList<ServerClient> clients = new ArrayList<>();
|
||||||
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
private final HashMap<String, Thread> clientThreads = new HashMap<>();
|
||||||
private boolean listening;
|
private boolean listening;
|
||||||
|
|
||||||
public SessionController(Server server) {
|
public SessionController() {
|
||||||
this.server = server;
|
|
||||||
this.listening = true;
|
this.listening = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +87,7 @@ public class SessionController extends Controller implements DataChangeCallback
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("[SERVER] got username " + username);
|
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);
|
Thread t = new Thread(serverClient);
|
||||||
t.start();
|
t.start();
|
||||||
@@ -166,7 +160,7 @@ public class SessionController extends Controller implements DataChangeCallback
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDataChange(Data data) {
|
public void onDataReceived(Data data, DataSource source) {
|
||||||
serverMessage(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class RestartSessionControllerTest {
|
public class RestartSessionControllerTest {
|
||||||
|
|||||||
Reference in New Issue
Block a user