Chat-Applikation/src/server/ClientReciveMessageThread.java

66 lines
2.4 KiB
Java
Raw Normal View History

/*
* 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 utils.Message;
/**
*
* @author eichehome
*/
public class ClientReciveMessageThread extends Thread {
private ClientStore clientStore;
private final ObjectInputStream in;
public void run() {
System.out.println("Thread" + this.getId() + ": Gestartet");
boolean continueLoop = true;
while (continueLoop) {
try {
Message empfangen = null;
while ((empfangen = (Message) this.in.readObject()) != null) {
System.out.println("Thread" + this.getId() + ": Client: " + empfangen.getMessage() + " from: " + empfangen.getUsername());
for (ObjectOutputStream stream : clientStore.getAllOutputStreams()) {
stream.writeObject(empfangen);
stream.flush();
System.out.println("Weitergeleitet");
}
}
} catch (ClassNotFoundException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
} catch (EOFException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
System.out.println("Thread push " + this.getId() + ": Socket wurde beendet");
} catch (IOException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
}
}
}
public ClientReciveMessageThread(ObjectInputStream in, ClientStore clientStore) {
this.in = in;
this.clientStore = clientStore;
}
}