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

@ -5,6 +5,7 @@ package client;
*/ */
import utils.Message;
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;

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,13 +1,23 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Copyright (C) 2021 eichehome
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * 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; package server;
import java.io.*; import java.io.IOException;
import java.net.*; import java.net.*;
import java.util.concurrent.ExecutionException;
/** /**
* Hauptklasse des Servers * Hauptklasse des Servers
@ -21,17 +31,26 @@ 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 { try {
ServerSocket serverSocket = new ServerSocket(1236); serverSocket = new ServerSocket(1236);
ClientMessageDistributorThread messageDistributor = new ClientMessageDistributorThread(); ClientMessageStore messageStore = new ClientMessageStore();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
Socket client = serverSocket.accept(); Socket client = serverSocket.accept();
System.out.println("client connected"); System.out.println("client connected");
new ServerHandelClientsThread(client, messageDistributor).start(); new ServerHandelClientsThread(client, messageStore).run();
} }
} catch (Exception e) { } catch (Exception ex) {
System.err.println("Fehler: " + e); System.err.println("Fehler: " + ex);
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException ex) {
System.err.println("Fehler");
}
}
} }
} }

View file

@ -16,13 +16,18 @@
*/ */
package server; package server;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
/** /**
* *
* @author eichehome * @author eichehome
*/ */
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"); Element el = new Element("Test");
@ -56,6 +61,34 @@ public class PipeTest {
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();
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. * Copyright (C) 2021 eichehome
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * 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; package server;
@ -14,25 +25,34 @@ import java.util.logging.Logger;
* *
* @author eichehome * @author eichehome
*/ */
public class ServerHandelClientsThread extends Thread { public class ServerHandelClientsThread implements Runnable {
private Socket clientSocket = null; private Socket clientSocket = null;
private ClientMessageDistributorThread messageDistributor = null;
@Override
public void run() { public void run() {
PrintWriter pr = null; while (true) {
try { try {
pr = new PrintWriter(clientSocket.getOutputStream(), true); PrintWriter pr = new PrintWriter(clientSocket.getOutputStream(), true);
InputStreamReader in = new InputStreamReader(clientSocket.getInputStream()); InputStreamReader in = new InputStreamReader(clientSocket.getInputStream());
BufferedReader bf = new BufferedReader(in); BufferedReader bf = new BufferedReader(in);
} catch (IOException ex) { String clientInput;
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); while ((clientInput = bf.readLine()) != null) {
} finally { System.out.println(clientInput);
pr.close(); }
} 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; clientSocket = socket;
messageDistributor = distributor; messageDistributor = distributor;
} }

View file

@ -14,14 +14,14 @@
* 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;
/** /**
* Diese Klasse stellt eine FIFO-Pipe dar. * Diese Klasse stellt eine FIFO-Pipe dar.
* *
* @author eichehome * @author eichehome
*/ */
public class FifoPipe { public class FifoPipe<T> {
PipeElement firstElement; PipeElement firstElement;
PipeElement lastElement; PipeElement lastElement;
@ -31,7 +31,7 @@ public class FifoPipe {
* *
* @param element ein Element, welches eingereiht werden soll * @param element ein Element, welches eingereiht werden soll
*/ */
public void queuElement(Element element) { public void queuElement(T element) {
PipeElement pipeElement = new PipeElement(element); PipeElement pipeElement = new PipeElement(element);
try { try {
lastElement.setNextElement(pipeElement); lastElement.setNextElement(pipeElement);

View file

@ -1,11 +1,12 @@
package client; package utils;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle; import java.time.format.FormatStyle;
class Message implements Serializable { public class Message implements Serializable {
String username; String username;
String message; String message;
//String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); //String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
@ -13,8 +14,7 @@ class Message implements Serializable {
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String date = current.format(formatter); String date = current.format(formatter);
public Message(String username, String message) {
public Message( String username, String message ){
this.username = username; this.username = username;
this.message = message; this.message = message;
//this.date = date; //this.date = date;
@ -22,9 +22,9 @@ class Message implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "nickname=" + username + return "nickname=" + username
", message=" + message + + ", message=" + message
", time=" + date; + ", time=" + date;
} }
} }

View file

@ -14,24 +14,24 @@
* 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;
/** /**
* Diese Klasse stellt eine Element aus der FIFO-Pipe dar. * Diese Klasse stellt eine Element aus der FIFO-Pipe dar.
* *
* @author eichehome * @author eichehome
*/ */
public class PipeElement { public class PipeElement<T> {
private PipeElement next; private PipeElement<T> next;
private Element data; private T 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(Element el) { public PipeElement(T el) {
data = el; data = el;
next = null; next = null;
} }
@ -41,7 +41,7 @@ public class PipeElement {
* *
* @return Einen Zeiger auf das nächste Objekt * @return Einen Zeiger auf das nächste Objekt
*/ */
public PipeElement getNext() { public PipeElement<T> getNext() {
return next; return next;
} }
@ -50,7 +50,7 @@ public class PipeElement {
* *
* @param element Das einzureihende Element * @param element Das einzureihende Element
*/ */
public void setNextElement(PipeElement element) { public void setNextElement(PipeElement<T> element) {
next = element; next = element;
} }
@ -59,7 +59,7 @@ public class PipeElement {
* *
* @return Inhalt diese Elements * @return Inhalt diese Elements
*/ */
public Element getData() { public T getData() {
return data; return data;
} }