From 8f2c12dab86f7f0297cf671ee7f04147e52d1909 Mon Sep 17 00:00:00 2001 From: eichehome Date: Fri, 14 May 2021 00:25:42 +0200 Subject: [PATCH] =?UTF-8?q?Fast=20fertiger=20Server,=20Letztes=20St=C3=BCc?= =?UTF-8?q?k=20fehlt=20noch=20und=20Fehlersuche.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/ClientMessageStore.java | 34 +++++++++------- src/server/MainServer.java | 19 +++------ src/server/PipeTest.java | 48 +++++++---------------- src/server/ServerHandelClientsThread.java | 28 +++++++------ src/{server => utils}/ArrayHelper.java | 47 +++++++++++----------- src/utils/FifoPipe.java | 15 ++++--- src/utils/PipeElement.java | 14 +++---- 7 files changed, 92 insertions(+), 113 deletions(-) rename src/{server => utils}/ArrayHelper.java (53%) diff --git a/src/server/ClientMessageStore.java b/src/server/ClientMessageStore.java index d2b4211..4835439 100644 --- a/src/server/ClientMessageStore.java +++ b/src/server/ClientMessageStore.java @@ -18,6 +18,7 @@ package server; import utils.FifoPipe; import java.net.Socket; +import utils.ArrayHelper; import utils.Message; /** @@ -26,27 +27,30 @@ import utils.Message; */ public class ClientMessageStore { + private ArrayHelper arrayHelper = new ArrayHelper<>(); /** * Verzeichniss der Threads, die die Clients überwachen */ - private Socket[] clientThreads = null; + private Thread[] clientThreads = null; /** * Puffer der Nachrichten */ - private FifoPipe messages = new FifoPipe(); + private FifoPipe messages = new FifoPipe(); - /*public synchronized void pushMessage(Object message) { - Object[] temp = new Object[messages.length + 1]; - temp[temp.length -1] = message; - messages = temp; - }*/ + public synchronized void pushMessage(Message message) { + messages.setElement(message); + } - /*public synchronized Message popMessage() { - Object[] temp = new Object[messages.length - 1]; - Object message = messages[0]; - for (int i = 1; i < messages.length; i++) { - temp[i - 1] = messages[i]; - } - messages = temp; - }*/ + public synchronized Message popMessage() { + Message result = messages.getNextElement(); + return result; + } + + public synchronized void addThread(Thread thread) { + arrayHelper.pushThread(thread, clientThreads); + } + + public synchronized Thread removeThread(Thread thread) { + return arrayHelper.popThread(thread, clientThreads); + } } diff --git a/src/server/MainServer.java b/src/server/MainServer.java index 4c9a828..4105268 100644 --- a/src/server/MainServer.java +++ b/src/server/MainServer.java @@ -31,26 +31,19 @@ public class MainServer { * @param args the command line arguments */ public static void main(String[] args) { - ServerSocket serverSocket = null; - try { - serverSocket = new ServerSocket(1236); + try (ServerSocket serverSocket = new ServerSocket(1236)) { ClientMessageStore messageStore = new ClientMessageStore(); - for (int i = 0; i < 3; i++) { + while (true) { Socket client = serverSocket.accept(); System.out.println("client connected"); - new ServerHandelClientsThread(client, messageStore).run(); + Thread thread = new ServerHandelClientsThread(client, messageStore); + System.out.println("Test"); + thread.run(); + messageStore.addThread(thread); } } catch (Exception ex) { System.err.println("Fehler: " + ex); - } finally { - if (serverSocket != null) { - try { - serverSocket.close(); - } catch (IOException ex) { - System.err.println("Fehler"); - } - } } } diff --git a/src/server/PipeTest.java b/src/server/PipeTest.java index 03c5fb3..bae1bac 100644 --- a/src/server/PipeTest.java +++ b/src/server/PipeTest.java @@ -16,9 +16,13 @@ */ package server; +import utils.ArrayHelper; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; +import utils.FifoPipe; +import utils.Message; +import utils.PipeElement; /** * @@ -27,68 +31,44 @@ import java.io.PipedWriter; public class PipeTest { public static void main(String[] args) { - /*FifoPipe pipe = new FifoPipe(); + FifoPipe pipe = new FifoPipe(); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); - Element el = new Element("Test"); - pipe.queuElement(el); + Message el = new Message("Christian", "Test"); + pipe.setElement(el); System.out.println("Angehängt"); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Letztes Element Inhalt: " + pipe.lastElement.getData().gebeData()); - Element el2 = new Element("Test2"); - pipe.queuElement(el2); + Message el2 = new Message("Christian", "Test"); + pipe.setElement(el2); System.out.println("Angehängt2"); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Letztes Element Inhalt: " + pipe.lastElement.getData().gebeData()); - PipeElement pipeElement = pipe.getNextElement(); + Message pipeElement = pipe.getNextElement(); System.out.println("Oben entnommen"); System.out.println("Entnommen Inhalt: " + pipeElement.getData().gebeData()); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Letztes Element Inhalt: " + pipe.lastElement.getData().gebeData()); - PipeElement pipeElement2 = pipe.getNextElement(); + Message pipeElement2 = pipe.getNextElement(); System.out.println("Oben entnommen2"); System.out.println("Entnommen Inhalt: " + pipeElement2.getData().gebeData()); System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Letztes Element: " + pipe.lastElement); //Wieso wills nicht funktionieren? - PipeElement pipeElement3 = pipe.getNextElement(); + Message pipeElement3 = pipe.getNextElement(); System.out.println("Oben entnommen3"); //System.out.println(pipeElement3.getData().gebeData()); System.out.println("Erstes Element: " + pipe.firstElement); - System.out.println("Letztes Element: " + pipe.lastElement);*/ + System.out.println("Letztes Element: " + pipe.lastElement); ArrayHelper test = new ArrayHelper(); - test.pushThread("Test1"); - test.pushThread("Test2"); - test.pushThread("Test3"); - System.out.println(test.messages[0]); - System.out.println(test.messages[1]); - System.out.println(test.messages[2]); - System.out.println(test.popThread("Test2")); - PipedWriter pipedWriter1 = new PipedWriter(); - PipedWriter pipedWriter2 = new PipedWriter(); - PipedReader pipedReader = new PipedReader(); - try { - pipedWriter1.connect(pipedReader); - } catch (IOException ex) { - System.out.println("1: " + ex); - } - try { - pipedWriter2.connect(pipedReader); - } catch (IOException ex) { - System.out.println("2: " + ex); - } - pipedWriter2 = pipedWriter1; - pipedWriter2.write("Test"); - pipedWriter2.flush(); - String test; - while ((test = pipedReader.readline())) + } } diff --git a/src/server/ServerHandelClientsThread.java b/src/server/ServerHandelClientsThread.java index ff26baf..be38dfe 100644 --- a/src/server/ServerHandelClientsThread.java +++ b/src/server/ServerHandelClientsThread.java @@ -25,35 +25,39 @@ import java.util.logging.Logger; * * @author eichehome */ -public class ServerHandelClientsThread implements Runnable { +public class ServerHandelClientsThread extends Thread { private Socket clientSocket = null; + private ClientMessageStore centralMessageStore = null; + private PrintWriter pr = null; @Override public void run() { - while (true) { - try { - PrintWriter pr = new PrintWriter(clientSocket.getOutputStream(), true); - InputStreamReader in = new InputStreamReader(clientSocket.getInputStream()); + boolean continueLoop = true; + while (continueLoop) { + try (InputStreamReader in = new InputStreamReader(clientSocket.getInputStream())) { + pr = new PrintWriter(clientSocket.getOutputStream()); BufferedReader bf = new BufferedReader(in); - String clientInput; - while ((clientInput = bf.readLine()) != null) { - System.out.println(clientInput); + for(String clientInput = bf.readLine(); clientInput != null; clientInput = bf.readLine()) { + System.out.println("client: " + clientInput); + pr.write("yes"); + pr.flush(); } } catch (InterruptedException ex) { System.err.println("test gegkückt"); System.err.println(ex); + pr.write("Test"); + Thread.currentThread().interrupt(); } catch (IOException ex) { System.err.println("Exeption: " + ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); - } finally { - pr.close(); + continueLoop = false; } } } - public ServerHandelClientsThread(Socket socket, ClientMessageStore distributor) { + public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore) { clientSocket = socket; - messageDistributor = distributor; + centralMessageStore = messageStore; } } diff --git a/src/server/ArrayHelper.java b/src/utils/ArrayHelper.java similarity index 53% rename from src/server/ArrayHelper.java rename to src/utils/ArrayHelper.java index 4d5dd14..86168fd 100644 --- a/src/server/ArrayHelper.java +++ b/src/utils/ArrayHelper.java @@ -14,51 +14,50 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package server; +package utils; /** * * @author eichehome */ -public class ArrayHelper { +public class ArrayHelper { - public String[] Threads; - - public void pushThread(String message) { - if (Threads != null) { - String[] temp = new String[Threads.length + 1]; - for (int i = 0; i < Threads.length; i++) { - temp[i] = Threads[i]; + public T[] pushThread(T element, T[] array) { + if (array != null) { + T[] temp = new T[1 + array.length]; + for (int i = 0; i < array.length; i++) { + temp[i] = array[i]; } - temp[temp.length - 1] = message;//Letzter eintrag - Threads = temp; + temp[temp.length - 1] = element;//Letzter eintrag + array = temp; } else { - String[] temp = new String[1]; - temp[temp.length - 1] = message; - Threads = temp; + T[] temp = new T[1]; + temp[temp.length - 1] = element; + array = temp; } + return array; } - public String popThread(String thread) { - if (Threads != null) { - int index = getIndex(thread, Threads); - String result = Threads[index]; - String[] temp = new String[Threads.length - 2]; + public T popThread(T element, T[] array) throws IllegalArgumentException { + if (array != null) { + int index = getIndex(element, array); + T result = array[index]; + T[] temp = new T[2 - array.length]; for (int i = 0; i < index; i++) { - temp[i] = Threads[i]; + temp[i] = array[i]; } for (int i = ++index; i < temp.length; i++) { - temp[i - 1] = Threads[i]; + temp[i - 1] = array[i]; } return result; } else { - return ""; + throw new IllegalArgumentException("Element not found"); } } - public int getIndex(String match, String[] array) { + int getIndex(T match, T[] array) { for (int i = 0; i < array.length; i++) { - if (array[i] == match) { + if (array[i].equals(match)) { return i; } } diff --git a/src/utils/FifoPipe.java b/src/utils/FifoPipe.java index a3f9120..d27f1b2 100644 --- a/src/utils/FifoPipe.java +++ b/src/utils/FifoPipe.java @@ -21,7 +21,7 @@ package utils; * * @author eichehome */ -public class FifoPipe { +public class FifoPipe { PipeElement firstElement; PipeElement lastElement; @@ -31,7 +31,7 @@ public class FifoPipe { * * @param element ein Element, welches eingereiht werden soll */ - public void queuElement(T element) { + public void setElement(Message element) { PipeElement pipeElement = new PipeElement(element); try { lastElement.setNextElement(pipeElement); @@ -47,22 +47,21 @@ public class FifoPipe { * * @return Erstes Element der FIFO-Pipe */ - public PipeElement getNextElement() { - PipeElement result = null; + public Message getNextElement() { + Message result = null; if (firstElement == null) { - return result; + return null; } else { if (firstElement == lastElement) { - result = firstElement; + result = firstElement.getData(); firstElement = null; lastElement = null; return result; } else { - result = firstElement; + result = firstElement.getData(); PipeElement next = firstElement.getNext(); firstElement = next; return result; - } } } diff --git a/src/utils/PipeElement.java b/src/utils/PipeElement.java index bc4a9ec..4633901 100644 --- a/src/utils/PipeElement.java +++ b/src/utils/PipeElement.java @@ -21,17 +21,17 @@ package utils; * * @author eichehome */ -public class PipeElement { +public class PipeElement { - private PipeElement next; - private T data; + private PipeElement next; + private Message data; /** * Der Constructor, welcher ein neues Element erstellt * * @param el Der Inhalt, welcher in diesem Element gespeichert ist. */ - public PipeElement(T el) { + public PipeElement(Message el) { data = el; next = null; } @@ -41,7 +41,7 @@ public class PipeElement { * * @return Einen Zeiger auf das nächste Objekt */ - public PipeElement getNext() { + public PipeElement getNext() { return next; } @@ -50,7 +50,7 @@ public class PipeElement { * * @param element Das einzureihende Element */ - public void setNextElement(PipeElement element) { + public void setNextElement(PipeElement element) { next = element; } @@ -59,7 +59,7 @@ public class PipeElement { * * @return Inhalt diese Elements */ - public T getData() { + public Message getData() { return data; }