diff --git a/src/client/Client1.java b/src/client/Client1.java
index b0953db..5502290 100644
--- a/src/client/Client1.java
+++ b/src/client/Client1.java
@@ -5,6 +5,7 @@ package client;
*/
+import utils.Message;
import java.net.*;
import java.io.*;
import java.text.SimpleDateFormat;
diff --git a/src/server/ArrayHelper.java b/src/server/ArrayHelper.java
new file mode 100644
index 0000000..4d5dd14
--- /dev/null
+++ b/src/server/ArrayHelper.java
@@ -0,0 +1,66 @@
+/*
+ * 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 server;
+
+/**
+ *
+ * @author eichehome
+ */
+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];
+ }
+ temp[temp.length - 1] = message;//Letzter eintrag
+ Threads = temp;
+ } else {
+ String[] temp = new String[1];
+ temp[temp.length - 1] = message;
+ Threads = temp;
+ }
+ }
+
+ public String popThread(String thread) {
+ if (Threads != null) {
+ int index = getIndex(thread, Threads);
+ String result = Threads[index];
+ String[] temp = new String[Threads.length - 2];
+ for (int i = 0; i < index; i++) {
+ temp[i] = Threads[i];
+ }
+ for (int i = ++index; i < temp.length; i++) {
+ temp[i - 1] = Threads[i];
+ }
+ return result;
+ } else {
+ return "";
+ }
+ }
+
+ public int getIndex(String match, String[] array) {
+ for (int i = 0; i < array.length; i++) {
+ if (array[i] == match) {
+ return i;
+ }
+ }
+ }
+}
diff --git a/src/server/ClientMessageDistributorThread.java b/src/server/ClientMessageDistributorThread.java
deleted file mode 100644
index 4d2bacc..0000000
--- a/src/server/ClientMessageDistributorThread.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-package server;
-
-import java.net.Socket;
-
-/**
- *
- * @author eichehome
- */
-public class ClientMessageDistributorThread implements Runnable {
-
- /**
- * Verzeichniss der Threads, die die Clients überwachen
- */
- private Socket[] clientThreads = null;
- /**
- * Puffer der Nachrichten
- */
- private Object[] messages = null;
-
- public void run() {
-
- }
-
- public void ClientMessageDistributorThread(Socket s) {
-
- }
-
- public void pushMessage(Object message) {
- Object[] temp = new Object[messages.length + 1];
- temp[temp.length -1] = message;
- messages = temp;
- }
-
- public Object 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;
- }
-}
diff --git a/src/server/ClientMessageStore.java b/src/server/ClientMessageStore.java
new file mode 100644
index 0000000..d2b4211
--- /dev/null
+++ b/src/server/ClientMessageStore.java
@@ -0,0 +1,52 @@
+/*
+ * 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 server;
+
+import utils.FifoPipe;
+import java.net.Socket;
+import utils.Message;
+
+/**
+ *
+ * @author eichehome
+ */
+public class ClientMessageStore {
+
+ /**
+ * Verzeichniss der Threads, die die Clients überwachen
+ */
+ private Socket[] clientThreads = null;
+ /**
+ * Puffer der Nachrichten
+ */
+ 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 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;
+ }*/
+}
diff --git a/src/server/Element.java b/src/server/Element.java
deleted file mode 100644
index 7585075..0000000
--- a/src/server/Element.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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 server;
-
-/**
- * Dummy für den inhalt der FIFO-Pipe
- *
- * @author eichehome
- */
-public class Element {
- private String bezeichner;
-
- public Element(String inhalt) {
- bezeichner = inhalt;
- }
- public String gebeData() {
- return bezeichner;
- }
-}
diff --git a/src/server/MainServer.java b/src/server/MainServer.java
index 43297f9..4c9a828 100644
--- a/src/server/MainServer.java
+++ b/src/server/MainServer.java
@@ -1,13 +1,23 @@
/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * 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 server;
-import java.io.*;
+import java.io.IOException;
import java.net.*;
-import java.util.concurrent.ExecutionException;
/**
* Hauptklasse des Servers
@@ -21,17 +31,26 @@ public class MainServer {
* @param args the command line arguments
*/
public static void main(String[] args) {
+ ServerSocket serverSocket = null;
try {
- ServerSocket serverSocket = new ServerSocket(1236);
- ClientMessageDistributorThread messageDistributor = new ClientMessageDistributorThread();
+ serverSocket = new ServerSocket(1236);
+ ClientMessageStore messageStore = new ClientMessageStore();
for (int i = 0; i < 3; i++) {
Socket client = serverSocket.accept();
System.out.println("client connected");
- new ServerHandelClientsThread(client, messageDistributor).start();
+ new ServerHandelClientsThread(client, messageStore).run();
}
- } catch (Exception e) {
- System.err.println("Fehler: " + e);
+ } 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 0d95087..03c5fb3 100644
--- a/src/server/PipeTest.java
+++ b/src/server/PipeTest.java
@@ -16,13 +16,18 @@
*/
package server;
+import java.io.IOException;
+import java.io.PipedReader;
+import java.io.PipedWriter;
+
/**
*
* @author eichehome
*/
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");
@@ -56,6 +61,34 @@ public class PipeTest {
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 92c566c..ff26baf 100644
--- a/src/server/ServerHandelClientsThread.java
+++ b/src/server/ServerHandelClientsThread.java
@@ -1,7 +1,18 @@
/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * 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 server;
@@ -14,25 +25,34 @@ import java.util.logging.Logger;
*
* @author eichehome
*/
-public class ServerHandelClientsThread extends Thread {
+public class ServerHandelClientsThread implements Runnable {
private Socket clientSocket = null;
- private ClientMessageDistributorThread messageDistributor = null;
+ @Override
public void run() {
- PrintWriter pr = null;
- try {
- pr = new PrintWriter(clientSocket.getOutputStream(), true);
- InputStreamReader in = new InputStreamReader(clientSocket.getInputStream());
- BufferedReader bf = new BufferedReader(in);
- } catch (IOException ex) {
- Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- pr.close();
+ while (true) {
+ try {
+ PrintWriter pr = new PrintWriter(clientSocket.getOutputStream(), true);
+ InputStreamReader in = new InputStreamReader(clientSocket.getInputStream());
+ BufferedReader bf = new BufferedReader(in);
+ String clientInput;
+ while ((clientInput = bf.readLine()) != null) {
+ System.out.println(clientInput);
+ }
+ } catch (InterruptedException ex) {
+ System.err.println("test gegkückt");
+ System.err.println(ex);
+ } catch (IOException ex) {
+ System.err.println("Exeption: " + ex);
+ Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
+ } finally {
+ pr.close();
+ }
}
}
- public ServerHandelClientsThread(Socket socket, ClientMessageDistributorThread distributor) {
+ public ServerHandelClientsThread(Socket socket, ClientMessageStore distributor) {
clientSocket = socket;
messageDistributor = distributor;
}
diff --git a/src/server/FifoPipe.java b/src/utils/FifoPipe.java
similarity index 95%
rename from src/server/FifoPipe.java
rename to src/utils/FifoPipe.java
index abf6370..a3f9120 100644
--- a/src/server/FifoPipe.java
+++ b/src/utils/FifoPipe.java
@@ -14,14 +14,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package server;
+package utils;
/**
* Diese Klasse stellt eine FIFO-Pipe dar.
*
* @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(Element element) {
+ public void queuElement(T element) {
PipeElement pipeElement = new PipeElement(element);
try {
lastElement.setNextElement(pipeElement);
diff --git a/src/client/Message.java b/src/utils/Message.java
similarity index 71%
rename from src/client/Message.java
rename to src/utils/Message.java
index 75d20c6..c32f71e 100644
--- a/src/client/Message.java
+++ b/src/utils/Message.java
@@ -1,20 +1,20 @@
-package client;
+package utils;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
-class Message implements Serializable {
+public class Message implements Serializable {
+
String username;
String message;
//String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String date = current.format(formatter);
-
- public Message( String username, String message ){
+ public Message(String username, String message) {
this.username = username;
this.message = message;
//this.date = date;
@@ -22,9 +22,9 @@ class Message implements Serializable {
@Override
public String toString() {
- return "nickname=" + username +
- ", message=" + message +
- ", time=" + date;
+ return "nickname=" + username
+ + ", message=" + message
+ + ", time=" + date;
}
-
-}
\ No newline at end of file
+
+}
diff --git a/src/server/PipeElement.java b/src/utils/PipeElement.java
similarity index 85%
rename from src/server/PipeElement.java
rename to src/utils/PipeElement.java
index edf8f75..bc4a9ec 100644
--- a/src/server/PipeElement.java
+++ b/src/utils/PipeElement.java
@@ -14,24 +14,24 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
-package server;
+package utils;
/**
* Diese Klasse stellt eine Element aus der FIFO-Pipe dar.
*
* @author eichehome
*/
-public class PipeElement {
+public class PipeElement {
- private PipeElement next;
- private Element data;
+ private PipeElement next;
+ private T data;
/**
* Der Constructor, welcher ein neues Element erstellt
*
* @param el Der Inhalt, welcher in diesem Element gespeichert ist.
*/
- public PipeElement(Element el) {
+ public PipeElement(T 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 Element getData() {
+ public T getData() {
return data;
}