98 lines
4 KiB
Java
98 lines
4 KiB
Java
/*
|
|
* 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.EOFException;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import utils.Message;
|
|
|
|
/**
|
|
* Diese Klasse stellt einen Thread dar, die Nachrichten, die er in einem Stream
|
|
* erhällt an einen Client weiterleitet.
|
|
*
|
|
* @version 1.0.0
|
|
* @author eichehome
|
|
*/
|
|
public class ClientSendMessageThread extends Thread {
|
|
|
|
/**
|
|
* Attribut, welches den Stream enthält, in dem die Nachrichten an den
|
|
* Client geschickt werden.
|
|
*/
|
|
private final ObjectOutputStream out;
|
|
/**
|
|
* Attribut, welches den Stream enthält, in dem die Nachrichten ankommen,
|
|
* die an den Client geschickt werden sollen.
|
|
*/
|
|
private final ObjectInputStream pipedObjectInputStream;
|
|
/**
|
|
* Attribut, welches einen Zeiger auf das zentrale Verzeichnis aller
|
|
* verbundenen Clients enthält.
|
|
*/
|
|
private ClientStore clientStore;
|
|
|
|
/**
|
|
* 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 ClientSendMessageThread(ObjectOutputStream out, ObjectInputStream pipedObjectInputStream, ClientStore clientStore) {
|
|
this.out = out;
|
|
this.pipedObjectInputStream = pipedObjectInputStream;
|
|
this.clientStore = clientStore;
|
|
}
|
|
|
|
/**
|
|
* Hauptmethode der Threads, in der alles abläuft.
|
|
*/
|
|
public void run() {
|
|
boolean continueLoop = true;
|
|
while (continueLoop) {
|
|
try {
|
|
Message empfangen = null;
|
|
while ((empfangen = (Message) this.pipedObjectInputStream.readObject()) != null) {//Hier bleibt dieser Thread stehen bis er eine Nchricht erhält, die er an den Client weiterleiten soll.
|
|
this.out.writeObject(empfangen);
|
|
this.out.flush();
|
|
Logger.getLogger(ClientSendMessageThread.class.getName()).log(Level.INFO, String.format("Sending Thread %d: Nachricht weitergegeben", this.getId()));
|
|
}
|
|
} catch (ClassNotFoundException ex) {
|
|
Logger.getLogger(ClientSendMessageThread.class.getName()).log(Level.SEVERE, String.format("Sending Thread %d: Fehler: %s", this.getId(), ex));
|
|
} catch (EOFException ex) {
|
|
Logger.getLogger(ClientSendMessageThread.class.getName()).log(Level.SEVERE, String.format("Sending Thread %d: Fehler: %s", this.getId(), ex));
|
|
continueLoop = false;
|
|
this.clientStore.close(this);
|
|
this.clientStore.removeClient(this);
|
|
} catch (IOException ex) {//Diese Exeption zeigt, dass sich der Stream zum Client geschlossen hat und deshalb Aufräumarbeiten angebracht sind.
|
|
Logger.getLogger(ClientSendMessageThread.class.getName()).log(Level.INFO, String.format("Sending Thread %d: Pipe wurde beendet", this.getId()));
|
|
this.clientStore.close(this);
|
|
this.clientStore.removeClient(this);
|
|
continueLoop = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|