Fast fertiger Server, Letztes Stück fehlt noch und Fehlersuche.

This commit is contained in:
eichehome 2021-05-14 00:25:42 +02:00
parent 040319dcbb
commit 8f2c12dab8
7 changed files with 92 additions and 113 deletions

View file

@ -18,6 +18,7 @@ package server;
import utils.FifoPipe; import utils.FifoPipe;
import java.net.Socket; import java.net.Socket;
import utils.ArrayHelper;
import utils.Message; import utils.Message;
/** /**
@ -26,27 +27,30 @@ import utils.Message;
*/ */
public class ClientMessageStore { public class ClientMessageStore {
private ArrayHelper<Thread> arrayHelper = new ArrayHelper<>();
/** /**
* Verzeichniss der Threads, die die Clients überwachen * Verzeichniss der Threads, die die Clients überwachen
*/ */
private Socket[] clientThreads = null; private Thread[] clientThreads = null;
/** /**
* Puffer der Nachrichten * Puffer der Nachrichten
*/ */
private FifoPipe messages = new FifoPipe<Message>(); private FifoPipe messages = new FifoPipe();
/*public synchronized void pushMessage(Object message) { public synchronized void pushMessage(Message message) {
Object[] temp = new Object[messages.length + 1]; messages.setElement(message);
temp[temp.length -1] = message; }
messages = temp;
}*/ public synchronized Message popMessage() {
Message result = messages.getNextElement();
/*public synchronized Message popMessage() { return result;
Object[] temp = new Object[messages.length - 1]; }
Object message = messages[0];
for (int i = 1; i < messages.length; i++) { public synchronized void addThread(Thread thread) {
temp[i - 1] = messages[i]; arrayHelper.pushThread(thread, clientThreads);
}
public synchronized Thread removeThread(Thread thread) {
return arrayHelper.popThread(thread, clientThreads);
} }
messages = temp;
}*/
} }

View file

@ -31,26 +31,19 @@ public class MainServer {
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
ServerSocket serverSocket = null; try (ServerSocket serverSocket = new ServerSocket(1236)) {
try {
serverSocket = new ServerSocket(1236);
ClientMessageStore messageStore = new ClientMessageStore(); ClientMessageStore messageStore = new ClientMessageStore();
for (int i = 0; i < 3; i++) { while (true) {
Socket client = serverSocket.accept(); Socket client = serverSocket.accept();
System.out.println("client connected"); 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) { } catch (Exception ex) {
System.err.println("Fehler: " + ex); System.err.println("Fehler: " + ex);
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException ex) {
System.err.println("Fehler");
}
}
} }
} }

View file

@ -16,9 +16,13 @@
*/ */
package server; package server;
import utils.ArrayHelper;
import java.io.IOException; import java.io.IOException;
import java.io.PipedReader; import java.io.PipedReader;
import java.io.PipedWriter; 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 class PipeTest {
public static void main(String[] args) { public static void main(String[] args) {
/*FifoPipe pipe = new FifoPipe(); FifoPipe pipe = new FifoPipe();
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
Element el = new Element("Test"); Message el = new Message("Christian", "Test");
pipe.queuElement(el); pipe.setElement(el);
System.out.println("Angehängt"); System.out.println("Angehängt");
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData());
System.out.println("Letztes Element Inhalt: " + pipe.lastElement.getData().gebeData()); System.out.println("Letztes Element Inhalt: " + pipe.lastElement.getData().gebeData());
Element el2 = new Element("Test2"); Message el2 = new Message("Christian", "Test");
pipe.queuElement(el2); pipe.setElement(el2);
System.out.println("Angehängt2"); System.out.println("Angehängt2");
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData());
System.out.println("Letztes Element Inhalt: " + pipe.lastElement.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("Oben entnommen");
System.out.println("Entnommen Inhalt: " + pipeElement.getData().gebeData()); System.out.println("Entnommen Inhalt: " + pipeElement.getData().gebeData());
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData()); System.out.println("Erstes Element Inhalt: " + pipe.firstElement.getData().gebeData());
System.out.println("Letztes Element Inhalt: " + pipe.lastElement.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("Oben entnommen2");
System.out.println("Entnommen Inhalt: " + pipeElement2.getData().gebeData()); System.out.println("Entnommen Inhalt: " + pipeElement2.getData().gebeData());
System.out.println("Erstes Element: " + pipe.firstElement); System.out.println("Erstes Element: " + pipe.firstElement);
System.out.println("Letztes Element: " + pipe.lastElement); System.out.println("Letztes Element: " + pipe.lastElement);
//Wieso wills nicht funktionieren? //Wieso wills nicht funktionieren?
PipeElement pipeElement3 = pipe.getNextElement(); Message pipeElement3 = pipe.getNextElement();
System.out.println("Oben entnommen3"); System.out.println("Oben entnommen3");
//System.out.println(pipeElement3.getData().gebeData()); //System.out.println(pipeElement3.getData().gebeData());
System.out.println("Erstes Element: " + pipe.firstElement); 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(); 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()))
} }
} }

View file

@ -25,35 +25,39 @@ import java.util.logging.Logger;
* *
* @author eichehome * @author eichehome
*/ */
public class ServerHandelClientsThread implements Runnable { public class ServerHandelClientsThread extends Thread {
private Socket clientSocket = null; private Socket clientSocket = null;
private ClientMessageStore centralMessageStore = null;
private PrintWriter pr = null;
@Override @Override
public void run() { public void run() {
while (true) { boolean continueLoop = true;
try { while (continueLoop) {
PrintWriter pr = new PrintWriter(clientSocket.getOutputStream(), true); try (InputStreamReader in = new InputStreamReader(clientSocket.getInputStream())) {
InputStreamReader in = new InputStreamReader(clientSocket.getInputStream()); pr = new PrintWriter(clientSocket.getOutputStream());
BufferedReader bf = new BufferedReader(in); BufferedReader bf = new BufferedReader(in);
String clientInput; for(String clientInput = bf.readLine(); clientInput != null; clientInput = bf.readLine()) {
while ((clientInput = bf.readLine()) != null) { System.out.println("client: " + clientInput);
System.out.println(clientInput); pr.write("yes");
pr.flush();
} }
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
System.err.println("test gegkückt"); System.err.println("test gegkückt");
System.err.println(ex); System.err.println(ex);
pr.write("Test");
Thread.currentThread().interrupt();
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("Exeption: " + ex); System.err.println("Exeption: " + ex);
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
} finally { continueLoop = false;
pr.close();
} }
} }
} }
public ServerHandelClientsThread(Socket socket, ClientMessageStore distributor) { public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore) {
clientSocket = socket; clientSocket = socket;
messageDistributor = distributor; centralMessageStore = messageStore;
} }
} }

View file

@ -14,51 +14,50 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package server; package utils;
/** /**
* *
* @author eichehome * @author eichehome
*/ */
public class ArrayHelper { public class ArrayHelper<T> {
public String[] Threads; public T[] pushThread(T element, T[] array) {
if (array != null) {
public void pushThread(String message) { T[] temp = new T[1 + array.length];
if (Threads != null) { for (int i = 0; i < array.length; i++) {
String[] temp = new String[Threads.length + 1]; temp[i] = array[i];
for (int i = 0; i < Threads.length; i++) {
temp[i] = Threads[i];
} }
temp[temp.length - 1] = message;//Letzter eintrag temp[temp.length - 1] = element;//Letzter eintrag
Threads = temp; array = temp;
} else { } else {
String[] temp = new String[1]; T[] temp = new T[1];
temp[temp.length - 1] = message; temp[temp.length - 1] = element;
Threads = temp; array = temp;
} }
return array;
} }
public String popThread(String thread) { public T popThread(T element, T[] array) throws IllegalArgumentException {
if (Threads != null) { if (array != null) {
int index = getIndex(thread, Threads); int index = getIndex(element, array);
String result = Threads[index]; T result = array[index];
String[] temp = new String[Threads.length - 2]; T[] temp = new T[2 - array.length];
for (int i = 0; i < index; i++) { for (int i = 0; i < index; i++) {
temp[i] = Threads[i]; temp[i] = array[i];
} }
for (int i = ++index; i < temp.length; i++) { for (int i = ++index; i < temp.length; i++) {
temp[i - 1] = Threads[i]; temp[i - 1] = array[i];
} }
return result; return result;
} else { } 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++) { for (int i = 0; i < array.length; i++) {
if (array[i] == match) { if (array[i].equals(match)) {
return i; return i;
} }
} }

View file

@ -21,7 +21,7 @@ package utils;
* *
* @author eichehome * @author eichehome
*/ */
public class FifoPipe<T> { public class FifoPipe {
PipeElement firstElement; PipeElement firstElement;
PipeElement lastElement; PipeElement lastElement;
@ -31,7 +31,7 @@ public class FifoPipe<T> {
* *
* @param element ein Element, welches eingereiht werden soll * @param element ein Element, welches eingereiht werden soll
*/ */
public void queuElement(T element) { public void setElement(Message element) {
PipeElement pipeElement = new PipeElement(element); PipeElement pipeElement = new PipeElement(element);
try { try {
lastElement.setNextElement(pipeElement); lastElement.setNextElement(pipeElement);
@ -47,22 +47,21 @@ public class FifoPipe<T> {
* *
* @return Erstes Element der FIFO-Pipe * @return Erstes Element der FIFO-Pipe
*/ */
public PipeElement getNextElement() { public Message getNextElement() {
PipeElement result = null; Message result = null;
if (firstElement == null) { if (firstElement == null) {
return result; return null;
} else { } else {
if (firstElement == lastElement) { if (firstElement == lastElement) {
result = firstElement; result = firstElement.getData();
firstElement = null; firstElement = null;
lastElement = null; lastElement = null;
return result; return result;
} else { } else {
result = firstElement; result = firstElement.getData();
PipeElement next = firstElement.getNext(); PipeElement next = firstElement.getNext();
firstElement = next; firstElement = next;
return result; return result;
} }
} }
} }

View file

@ -21,17 +21,17 @@ package utils;
* *
* @author eichehome * @author eichehome
*/ */
public class PipeElement<T> { public class PipeElement {
private PipeElement<T> next; private PipeElement next;
private T data; private Message data;
/** /**
* Der Constructor, welcher ein neues Element erstellt * Der Constructor, welcher ein neues Element erstellt
* *
* @param el Der Inhalt, welcher in diesem Element gespeichert ist. * @param el Der Inhalt, welcher in diesem Element gespeichert ist.
*/ */
public PipeElement(T el) { public PipeElement(Message el) {
data = el; data = el;
next = null; next = null;
} }
@ -41,7 +41,7 @@ public class PipeElement<T> {
* *
* @return Einen Zeiger auf das nächste Objekt * @return Einen Zeiger auf das nächste Objekt
*/ */
public PipeElement<T> getNext() { public PipeElement getNext() {
return next; return next;
} }
@ -50,7 +50,7 @@ public class PipeElement<T> {
* *
* @param element Das einzureihende Element * @param element Das einzureihende Element
*/ */
public void setNextElement(PipeElement<T> element) { public void setNextElement(PipeElement element) {
next = element; next = element;
} }
@ -59,7 +59,7 @@ public class PipeElement<T> {
* *
* @return Inhalt diese Elements * @return Inhalt diese Elements
*/ */
public T getData() { public Message getData() {
return data; return data;
} }