Aufräumarbeitenund einige Formatierungen

This commit is contained in:
eichehome 2021-05-13 21:09:10 +02:00
parent d7bbfdea2b
commit 040319dcbb
11 changed files with 238 additions and 127 deletions

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}
}
}

View file

@ -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;
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<Message>();
/*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;
}*/
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View file

@ -1,70 +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 <http://www.gnu.org/licenses/>.
*/
package server;
/**
* Diese Klasse stellt eine FIFO-Pipe dar.
*
* @author eichehome
*/
public class FifoPipe {
PipeElement firstElement;
PipeElement lastElement;
/**
* Diese Funktion dient zum Einreihen von Elementen in die FIFO-Pipe
*
* @param element ein Element, welches eingereiht werden soll
*/
public void queuElement(Element element) {
PipeElement pipeElement = new PipeElement(element);
try {
lastElement.setNextElement(pipeElement);
lastElement = pipeElement;
} catch (NullPointerException ex) {
firstElement = pipeElement;
lastElement = pipeElement;
}
}
/**
* Diese funktion gibt das erste Element der FIFO-Pipe zurück
*
* @return Erstes Element der FIFO-Pipe
*/
public PipeElement getNextElement() {
PipeElement result = null;
if (firstElement == null) {
return result;
} else {
if (firstElement == lastElement) {
result = firstElement;
firstElement = null;
lastElement = null;
return result;
} else {
result = firstElement;
PipeElement next = firstElement.getNext();
firstElement = next;
return result;
}
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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");
}
}
}
}

View file

@ -1,66 +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 <http://www.gnu.org/licenses/>.
*/
package server;
/**
* Diese Klasse stellt eine Element aus der FIFO-Pipe dar.
*
* @author eichehome
*/
public class PipeElement {
private PipeElement next;
private Element data;
/**
* Der Constructor, welcher ein neues Element erstellt
*
* @param el Der Inhalt, welcher in diesem Element gespeichert ist.
*/
public PipeElement(Element el) {
data = el;
next = null;
}
/**
* Diese Funktion gubt einen Zeiger auf das nächste Objekt zurück
*
* @return Einen Zeiger auf das nächste Objekt
*/
public PipeElement getNext() {
return next;
}
/**
* Diese Funktion reiht ein Element nach diesem ein.
*
* @param element Das einzureihende Element
*/
public void setNextElement(PipeElement element) {
next = element;
}
/**
* Eine Funktion, die den Inhalt diese Elements zurück gibt
*
* @return Inhalt diese Elements
*/
public Element getData() {
return data;
}
}

View file

@ -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()))
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}