JavaDoc überall eingefügt.

This commit is contained in:
eichehome 2021-05-16 00:43:36 +02:00
parent 246abce07b
commit 3914fae45d
5 changed files with 135 additions and 115 deletions

View file

@ -20,9 +20,12 @@ import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Message; import utils.Message;
/** /**
* Diese Klasse stellt einen Thread dar, die Nachrichten, die er in einem Stream erhällt an einen Client weiterleitet.
* *
* @author eichehome * @author eichehome
*/ */
@ -32,6 +35,9 @@ public class ClientPushMessageThread extends Thread {
private final ObjectInputStream pipedObjectInputStream; private final ObjectInputStream pipedObjectInputStream;
private ClientStore clientStore; private ClientStore clientStore;
/**
* Haupt methode der Threads, in der alles abläuft.
*/
public void run() { public void run() {
boolean continueLoop = true; boolean continueLoop = true;
while (continueLoop) { while (continueLoop) {
@ -40,22 +46,29 @@ public class ClientPushMessageThread extends Thread {
while ((empfangen = (Message) this.pipedObjectInputStream.readObject()) != null) { while ((empfangen = (Message) this.pipedObjectInputStream.readObject()) != null) {
this.out.writeObject(empfangen); this.out.writeObject(empfangen);
this.out.flush(); this.out.flush();
Logger.getLogger(ClientPushMessageThread.class.getName()).log(Level.INFO, String.format("Pushing Thread %d: Nachricht weitergegeben", this.getId()));
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientPushMessageThread.class.getName()).log(Level.SEVERE, String.format("Pushing Thread %d: Fehler: %s", this.getId(), ex));
} catch (EOFException ex) { } catch (EOFException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientPushMessageThread.class.getName()).log(Level.SEVERE, String.format("Pushing Thread %d: Fehler: %s", this.getId(), ex));
continueLoop = false; continueLoop = false;
clientStore.removeClientByPusher(this); clientStore.close(this);
clientStore.removeClient(this);
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("Thread push" + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientPushMessageThread.class.getName()).log(Level.INFO, String.format("Pushing Thread %d: Pipe wurde beendet", this.getId()));
clientStore.removeClient(this);
continueLoop = false; continueLoop = false;
clientStore.removeClientByPusher(this);
System.out.println("Thread recive " + this.getId() + ": Pipe wurde beendet");
} }
} }
} }
/**
* Konstruktor
* @param out Stream, in dem der Thread die Nachrichten an den Client schickt.
* @param pipedObjectInputStream Stream, in dem der Thread die Nachrichten von anderen Threads erhält.
* @param clientStore Zeiger auf das zentrale Verzeichnis der Clients, um sich dort austragen zu können.
*/
public ClientPushMessageThread(ObjectOutputStream out, ObjectInputStream pipedObjectInputStream, ClientStore clientStore) { public ClientPushMessageThread(ObjectOutputStream out, ObjectInputStream pipedObjectInputStream, ClientStore clientStore) {
this.out = out; this.out = out;
this.pipedObjectInputStream = pipedObjectInputStream; this.pipedObjectInputStream = pipedObjectInputStream;

View file

@ -20,9 +20,12 @@ import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Message; import utils.Message;
/** /**
* Diese Klasse stellt einen Thread dar, der auf Nachrichten eines Clients lauscht und diese dann weiter gibt.
* *
* @author eichehome * @author eichehome
*/ */
@ -31,33 +34,40 @@ public class ClientReciveMessageThread extends Thread {
private ClientStore clientStore; private ClientStore clientStore;
private final ObjectInputStream in; private final ObjectInputStream in;
/**
* Hauptmethode des Threads, in der alles abläuft.
*/
public void run() { public void run() {
System.out.println("Thread" + this.getId() + ": Gestartet"); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Gestartet", this.getId()));
boolean continueLoop = true; boolean continueLoop = true;
while (continueLoop) { while (continueLoop) {
try { try {
Message empfangen = null; Message empfangen = null;
while ((empfangen = (Message) this.in.readObject()) != null) { while ((empfangen = (Message) this.in.readObject()) != null) {
System.out.println("Thread" + this.getId() + ": Client: " + empfangen.getMessage() + " from: " + empfangen.getUsername()); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Time: %s Client: %s Message: %s", this.getId(), empfangen.getTime(), empfangen.getUsername(), empfangen.getUsername()));
for (ObjectOutputStream stream : clientStore.getAllOutputStreams()) { for (ObjectOutputStream stream : clientStore.getAllOutputStreams()) {
stream.writeObject(empfangen); stream.writeObject(empfangen);
stream.flush(); stream.flush();
System.out.println("Weitergeleitet"); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Weitergeleitet", this.getId()));
} }
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.SEVERE, String.format("Reciveing Thread %d: Fehler: %s", this.getId(), ex));
} catch (EOFException ex) { } catch (EOFException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Socket wurde beendet", this.getId()));
continueLoop = false; continueLoop = false;
System.out.println("Thread push " + this.getId() + ": Socket wurde beendet");
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex); Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.SEVERE, String.format("Reciveing Thread %d: Fehler: %s", this.getId(), ex));
continueLoop = false; continueLoop = false;
} }
} }
} }
/**
* Konstruktor
* @param in Stream, in dem die Nachrichten eines Clients ankommen.
* @param clientStore Zeiger auf das zentrale Verzeichnis der Clients, um alle benachrichtigen zu können.
*/
public ClientReciveMessageThread(ObjectInputStream in, ClientStore clientStore) { public ClientReciveMessageThread(ObjectInputStream in, ClientStore clientStore) {
this.in = in; this.in = in;
this.clientStore = clientStore; this.clientStore = clientStore;

View file

@ -16,10 +16,14 @@
*/ */
package server; package server;
import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.util.logging.Logger;
import java.util.logging.Level;
import utils.Client; import utils.Client;
/** /**
* Diese Klasse Stellt das zentrale Verzeichniss aller Clients dar.
* *
* @author eichehome * @author eichehome
*/ */
@ -27,24 +31,31 @@ public class ClientStore {
private Client[] clients = null; private Client[] clients = null;
/**
* Diese Funktion fügt einen Client dem Verzeichniss aller Clients hinzu, die Verbunden sind.
* @param newClient Der neue Client, der hinzugefügt werden soll.
*/
public void addClient(Client newClient) { public void addClient(Client newClient) {
if (this.clients != null) { if (this.clients != null) {
System.out.println("Weiterer Durchlauf"); Client[] temp = new Client[this.clients.length + 1];
Client [] temp = new Client [this.clients.length + 1];
System.arraycopy(clients, 0, temp, 0, clients.length); System.arraycopy(clients, 0, temp, 0, clients.length);
temp[temp.length - 1] = newClient; temp[temp.length - 1] = newClient;
System.out.println(newClient.getPusher() + "/" + newClient.getReciver() + "/" + newClient.getOutputStream());
this.clients = temp; this.clients = temp;
} else { } else {
System.out.println("Erser Durchlauf");
Client[] temp = new Client[1]; Client[] temp = new Client[1];
temp[0] = newClient; temp[0] = newClient;
this.clients = temp; this.clients = temp;
} }
printAll(); Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Client hinzugefügt");
} }
public Client removeClientByPusher(Thread pusher) throws IllegalArgumentException { /**
* Dies Funktion entfernt einen Client aus dem Verzeichnis aller verbundenen Clients.
* @param pusher Der Pusher-Thread des Clients, welcher aus dem Verzeichnis entfernt werden soll.
* @return Gibt die zu dem Client gehörenden Streams und Threads zurück.
* @throws IllegalArgumentException Falls der Thread nicht existiert oder keine Clients vorhanden sind, wird eine Exeption mit der passenden Fehlermeldung geworfen.
*/
public Client removeClient(Thread pusher) throws IllegalArgumentException {
if (this.clients != null) { if (this.clients != null) {
int index = -1; int index = -1;
for (int i = 0; i < this.clients.length; i++) { for (int i = 0; i < this.clients.length; i++) {
@ -63,61 +74,7 @@ public class ClientStore {
} }
Client result = this.clients[index]; Client result = this.clients[index];
this.clients = temp; this.clients = temp;
return result; Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Client gelöscht");
} else {
throw new IllegalArgumentException("Element not found" + index);
}
} else {
throw new IllegalArgumentException("No clients present");
}
}
public Client removeClientByReciver(Thread reciver) throws IllegalArgumentException {
if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (this.clients[i].getReciver().equals(reciver)) {
index = i;
break;
}
}
if (index != -1) {
Client[] temp = new Client[this.clients.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = this.clients[i];
}
for (int i = (index + 1); i < this.clients.length; i++) {
temp[i - 1] = this.clients[i];
}
Client result = this.clients[index];
this.clients = temp;
return result;
} else {
throw new IllegalArgumentException("Element not found");
}
} else {
throw new IllegalArgumentException("No clients present");
}
}
public Client removeClientByOutputStream(ObjectOutputStream out) throws IllegalArgumentException {
if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (this.clients[i].getOutputStream().equals(out)) {
index = i;
}
}
if (index != -1) {
Client[] temp = new Client[this.clients.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = this.clients[i];
}
for (int i = (index + 1); i < this.clients.length; i++) {
temp[i - 1] = this.clients[i];
}
Client result = this.clients[index];
this.clients = temp;
return result; return result;
} else { } else {
throw new IllegalArgumentException("Element not found"); throw new IllegalArgumentException("Element not found");
@ -127,21 +84,43 @@ public class ClientStore {
} }
} }
/**
* Diese Funktion gibt alle Streams zu den Clients zurück, die derzeit Verbunden sind.
* @return Ein Array aus allen Streams
*/
public ObjectOutputStream[] getAllOutputStreams() { public ObjectOutputStream[] getAllOutputStreams() {
ObjectOutputStream[] streams = new ObjectOutputStream[this.clients.length]; ObjectOutputStream[] streams = new ObjectOutputStream[this.clients.length];
for (int i = 0; i < this.clients.length; i++) { for (int i = 0; i < this.clients.length; i++) {
streams[i] = this.clients[i].getOutputStream(); streams[i] = this.clients[i].getOutputStream();
} }
Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Alle Streams zusammengesucht");
return streams; return streams;
} }
public /**
* Schließt den zu einem Thread gehörenden OutputStream
private void printAll() { * @param thread Der Thread, dessen OutputStream geschlossen werden soll
for (int i = 0; i < clients.length; i++) { * @throws IllegalArgumentException Falls der Thread nicht existiert oder keine Clients vorhanden sind, wird eine Exeption mit der passenden Fehlermeldung geworfen.
System.out.println("ClientStore" + i + ": " + clients[i].getPusher()); */
System.out.println("ClientStore" + i + ": " + clients[i].getReciver()); public void close(Thread thread) throws IllegalArgumentException{
System.out.println("ClientStore" + i + ": " + clients[i].getOutputStream()); if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (clients[i].getPusher().equals(thread)) {
index = i;
}
}
if (index != -1) {
try {
this.clients[index].getOutputStream().close();
} catch (IOException ex) {
Logger.getLogger(ClientStore.class.getName()).log(Level.SEVERE, String.format("ClientStore: Fehler: %s", ex));
}
} else {
throw new IllegalArgumentException("Element not found");
}
} else {
throw new IllegalArgumentException("No Clients Present");
} }
} }

View file

@ -16,17 +16,14 @@
*/ */
package server; package server;
import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.lang.invoke.MethodHandles;
import java.net.*; import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.Client; import utils.Client;
import utils.FifoPipe;
/** /**
* Hauptklasse des Servers * Hauptklasse des Servers
@ -37,16 +34,16 @@ import utils.FifoPipe;
public class MainServer { public class MainServer {
/** /**
* Einstiegsklasse des Servers. Diese wartet auf Anfragen der Clients und erstellt dann die nötigen Resourcen (Streams und Threads).
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
boolean continueLoop = true;
try (ServerSocket serverSocket = new ServerSocket(1236)) { try (ServerSocket serverSocket = new ServerSocket(1236)) {
ClientStore clientStore = new ClientStore(); ClientStore clientStore = new ClientStore();
while (continueLoop) { while (true) {
System.out.println("Warte auf Clients"); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Warte auf Clients"));
Socket client = serverSocket.accept(); Socket client = serverSocket.accept();
System.out.println("client connected"); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Client verbunden"));
PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
@ -57,25 +54,23 @@ public class MainServer {
ObjectInputStream in = new ObjectInputStream(client.getInputStream()); ObjectInputStream in = new ObjectInputStream(client.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream()); ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
System.out.println("Streams created"); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Streams erstellt"));
Thread threadRecive = new ClientReciveMessageThread(in, clientStore); Thread threadRecive = new ClientReciveMessageThread(in, clientStore);
Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore); Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore);
System.out.println("Threads created"); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads erstellt"));
threadPush.start(); threadPush.start();
threadRecive.start(); threadRecive.start();
System.out.println("Threads started"); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads gestartet"));
Client client = new Client(pipedObjectOutputStream, threadPush, threadRecive); clientStore.addClient(new Client(pipedObjectOutputStream, threadPush, threadRecive));
System.out.println(client.getPusher() + "/" + client.getReciver() + "/" + client.getOutputStream());
clientStore.addClient(client);
} }
} catch (Exception ex) { } catch (Exception ex) {
System.err.println("Fehler: " + ex); Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, String.format("Main: Fehler: %s", ex));
} }
} }

View file

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
/** /**
* Dies Klasse stellt einen Client mit seinen Streams dar.
* *
* @author eichehome * @author eichehome
*/ */
@ -29,12 +30,22 @@ public class Client {
private final Thread pusher; private final Thread pusher;
private final Thread reciver; private final Thread reciver;
/**
* Konstruktor
* @param out Stream, um diesem Client Nachrichten schicken zu können.
* @param pusher Zeiger auf den Thread, der die Nachrichten an den Client sendet.
* @param reciver Zeiger auf den Thread, der die Nachrichten eines Clients empfängt.
*/
public Client(ObjectOutputStream out, Thread pusher, Thread reciver) { public Client(ObjectOutputStream out, Thread pusher, Thread reciver) {
this.out = out; this.out = out;
this.pusher = pusher; this.pusher = pusher;
this.reciver = reciver; this.reciver = reciver;
} }
/**
* Schreibe eine Nachricht in den OutputStream dieses Clients.
* @param message Die zu verschickende Nachricht.
*/
public void writeMessage(Message message) { public void writeMessage(Message message) {
try { try {
this.out.writeObject(message); this.out.writeObject(message);
@ -43,14 +54,26 @@ public class Client {
} }
} }
/**
* Getter für den OutputStream diese Clients
* @return Zeiger auf den Stream.
*/
public ObjectOutputStream getOutputStream() { public ObjectOutputStream getOutputStream() {
return this.out; return this.out;
} }
/**
* Getter für den Thread, der Nachrichten an diesen Client sendet.
* @return Zeiger auf den Thread.
*/
public Thread getPusher() { public Thread getPusher() {
return this.pusher; return this.pusher;
} }
/**
* Getter für den Thread, der Nchrichten an diesen Client sendet.
* @return Zeiger auf den Thread.
*/
public Thread getReciver() { public Thread getReciver() {
return this.reciver; return this.reciver;
} }