diff --git a/src/server/ClientMessageStore.java b/src/server/ClientMessageStore.java index 50956ff..8bbb8c0 100644 --- a/src/server/ClientMessageStore.java +++ b/src/server/ClientMessageStore.java @@ -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 arrayHelper = new ArrayHelper<>(); + private ArrayHelper 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(); } } } diff --git a/src/server/MainServer.java b/src/server/MainServer.java index 4105268..19df211 100644 --- a/src/server/MainServer.java +++ b/src/server/MainServer.java @@ -17,6 +17,8 @@ package server; import java.io.IOException; +import java.io.PipedReader; +import java.io.PipedWriter; import java.net.*; /** @@ -36,10 +38,12 @@ public class MainServer { while (true) { Socket client = serverSocket.accept(); System.out.println("client connected"); - Thread thread = new ServerHandelClientsThread(client, messageStore); + PipedReader pipeOut = new PipedReader(); + PipedWriter pipeIn = new PipedWriter(pipeOut); + Thread thread = new ServerHandelClientsThread(client, messageStore, pipeOut); System.out.println("Test"); thread.run(); - messageStore.addThread(thread); + messageStore.addClient(thread, pipeIn); } } catch (Exception ex) { diff --git a/src/server/ServerHandelClientsThread.java b/src/server/ServerHandelClientsThread.java index ad8c95e..f460957 100644 --- a/src/server/ServerHandelClientsThread.java +++ b/src/server/ServerHandelClientsThread.java @@ -30,7 +30,7 @@ public class ServerHandelClientsThread extends Thread { private Socket clientSocket = null; private ClientMessageStore centralMessageStore = null; - private PrintWriter pr = null; + private PipedReader pipe = null; @Override public void run() { @@ -56,17 +56,23 @@ public class ServerHandelClientsThread extends Thread { Thread.currentThread().interrupt(); } catch (SocketException ex) { System.err.println("Socket geschlossen"); + centralMessageStore.removeClient(this); + continueLoop = false; + } catch (EOFException ex) { + System.err.println("Fehler: " + ex); continueLoop = false; } catch (IOException ex) { System.err.println("Exeption: " + ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); + centralMessageStore.removeClient(this); continueLoop = false; } } } - public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore) { - clientSocket = socket; - centralMessageStore = messageStore; + public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, PipedReader pipe) { + this.clientSocket = socket; + this.centralMessageStore = messageStore; + this.pipe = pipe; } } diff --git a/src/utils/ArrayHelper.java b/src/utils/ArrayHelper.java index 26e0cb5..f632d81 100644 --- a/src/utils/ArrayHelper.java +++ b/src/utils/ArrayHelper.java @@ -23,7 +23,7 @@ package utils; */ public class ArrayHelper { - public T[] pushThread(T element, T[] array) { + public T[] push(T element, T[] array) { if (array != null) { T[] temp = new T[1 + array.length]; for (int i = 0; i < array.length; i++) { @@ -39,7 +39,7 @@ public class ArrayHelper { return array; } - public T popThread(T element, T[] array) throws IllegalArgumentException { + public T pop(T element, T[] array) throws IllegalArgumentException { if (array != null) { int index = getIndex(element, array); T result = array[index]; diff --git a/src/utils/Client.java b/src/utils/Client.java new file mode 100644 index 0000000..01f03e8 --- /dev/null +++ b/src/utils/Client.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 eichehome + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package utils; + +import java.io.IOException; +import java.io.PipedWriter; + +/** + * + * @author eichehome + */ +public class Client { + private Thread thread = null; + private PipedWriter pipe = null; + + public Client(Thread thread, PipedWriter pipe) { + this.thread = thread; + this.pipe = pipe; + } + + public void writePipe(Message message) { + try { + this.pipe.write(message.toString()); + } catch (IOException ex) { + System.err.println("Fehler: " + ex); + } + } + + public Thread getThread() { + return this.thread; + } + + public PipedWriter getPipe() { + return this.pipe; + } +}