Server nimmt nun Nachrichten an und Schickt diese zurück.

This commit is contained in:
eichehome 2021-05-14 20:02:26 +02:00
parent 6f39b1ba46
commit e0ee8be9f6
5 changed files with 90 additions and 16 deletions

View file

@ -16,8 +16,10 @@
*/
package server;
import java.io.PipedWriter;
import utils.FifoPipe;
import utils.ArrayHelper;
import utils.Client;
import utils.Message;
/**
@ -26,11 +28,11 @@ import utils.Message;
*/
public class ClientMessageStore {
private ArrayHelper<Thread> arrayHelper = new ArrayHelper<>();
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
/**
* Verzeichniss der Threads, die die Clients überwachen
*/
private Thread[] clientThreads = null;
private Client[] clientThreads = null;
/**
* Puffer der Nachrichten
*/
@ -45,17 +47,29 @@ public class ClientMessageStore {
return result;
}
public synchronized void addThread(Thread thread) {
arrayHelper.pushThread(thread, clientThreads);
public synchronized void addClient(Thread thread, PipedWriter pipe) {
Client client = new Client(thread, pipe);
arrayHelper.push(client, clientThreads);
}
public synchronized Thread removeThread(Thread thread) {
return arrayHelper.popThread(thread, clientThreads);
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
PipedWriter pipe = null;
for (int i = 0; i < clientThreads.length; i++) {
if (clientThreads[i].getThread().equals(thread)) {
pipe = clientThreads[i].getPipe();
}
}
if (pipe != null) {
Client client = new Client(thread, pipe);
return arrayHelper.pop(client, clientThreads);
} else {
throw new IllegalArgumentException("Element not found");
}
}
public void notifyAllClients() {
for (int i = 0; i < clientThreads.length; i++) {
clientThreads[i].interrupt();
for (Client client : clientThreads) {
client.getThread().interrupt();
}
}
}