Disconnect clients properly and cleaned server package
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -11,19 +11,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 {
|
||||
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 +56,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 +95,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
|
||||
|
||||
Reference in New Issue
Block a user