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 java.net.Socket;
import utils.ArrayHelper;
import utils.Message;
/**
@ -26,27 +27,30 @@ import utils.Message;
*/
public class ClientMessageStore {
private ArrayHelper<Thread> 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<Message>();
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);
}
}

View file

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

View file

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

View file

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

View file

@ -14,51 +14,50 @@
* 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;
package utils;
/**
*
* @author eichehome
*/
public class ArrayHelper {
public class ArrayHelper<T> {
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;
}
}

View file

@ -21,7 +21,7 @@ package utils;
*
* @author eichehome
*/
public class FifoPipe<T> {
public class FifoPipe {
PipeElement firstElement;
PipeElement lastElement;
@ -31,7 +31,7 @@ public class FifoPipe<T> {
*
* @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<T> {
*
* @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;
}
}
}

View file

@ -21,17 +21,17 @@ package utils;
*
* @author eichehome
*/
public class PipeElement<T> {
public class PipeElement {
private PipeElement<T> 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<T> {
*
* @return Einen Zeiger auf das nächste Objekt
*/
public PipeElement<T> getNext() {
public PipeElement getNext() {
return next;
}
@ -50,7 +50,7 @@ public class PipeElement<T> {
*
* @param element Das einzureihende Element
*/
public void setNextElement(PipeElement<T> element) {
public void setNextElement(PipeElement element) {
next = element;
}
@ -59,7 +59,7 @@ public class PipeElement<T> {
*
* @return Inhalt diese Elements
*/
public T getData() {
public Message getData() {
return data;
}