2021-05-15 20:53:58 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2021-05-15 22:43:36 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2021-05-15 20:53:58 +00:00
|
|
|
import utils.Message;
|
|
|
|
|
|
|
|
/**
|
2021-05-15 22:43:36 +00:00
|
|
|
* Diese Klasse stellt einen Thread dar, der auf Nachrichten eines Clients lauscht und diese dann weiter gibt.
|
|
|
|
*
|
2021-05-15 20:53:58 +00:00
|
|
|
* @author eichehome
|
|
|
|
*/
|
|
|
|
public class ClientReciveMessageThread extends Thread {
|
|
|
|
|
|
|
|
private ClientStore clientStore;
|
|
|
|
private final ObjectInputStream in;
|
|
|
|
|
2021-05-15 22:43:36 +00:00
|
|
|
/**
|
|
|
|
* Hauptmethode des Threads, in der alles abläuft.
|
|
|
|
*/
|
2021-05-15 20:53:58 +00:00
|
|
|
public void run() {
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Gestartet", this.getId()));
|
2021-05-15 20:53:58 +00:00
|
|
|
boolean continueLoop = true;
|
|
|
|
while (continueLoop) {
|
|
|
|
try {
|
|
|
|
Message empfangen = null;
|
|
|
|
while ((empfangen = (Message) this.in.readObject()) != null) {
|
2021-05-16 10:25:46 +00:00
|
|
|
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.getMessage()));
|
2021-05-15 20:53:58 +00:00
|
|
|
for (ObjectOutputStream stream : clientStore.getAllOutputStreams()) {
|
|
|
|
stream.writeObject(empfangen);
|
|
|
|
stream.flush();
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Weitergeleitet", this.getId()));
|
2021-05-15 20:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ClassNotFoundException ex) {
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.SEVERE, String.format("Reciveing Thread %d: Fehler: %s", this.getId(), ex));
|
2021-05-15 20:53:58 +00:00
|
|
|
} catch (EOFException ex) {
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.INFO, String.format("Reciveing Thread %d: Socket wurde beendet", this.getId()));
|
2021-05-15 20:53:58 +00:00
|
|
|
continueLoop = false;
|
|
|
|
} catch (IOException ex) {
|
2021-05-15 22:43:36 +00:00
|
|
|
Logger.getLogger(ClientReciveMessageThread.class.getName()).log(Level.SEVERE, String.format("Reciveing Thread %d: Fehler: %s", this.getId(), ex));
|
2021-05-15 20:53:58 +00:00
|
|
|
continueLoop = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:43:36 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-05-15 20:53:58 +00:00
|
|
|
public ClientReciveMessageThread(ObjectInputStream in, ClientStore clientStore) {
|
|
|
|
this.in = in;
|
|
|
|
this.clientStore = clientStore;
|
|
|
|
}
|
|
|
|
}
|