Chat-Applikation/src/server/ClientPushMessageThread.java

66 lines
2.3 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 ClientPushMessageThread extends Thread {
private final ObjectOutputStream out;
private final ObjectInputStream pipedObjectInputStream;
private ClientStore clientStore;
public void run() {
boolean continueLoop = true;
while (continueLoop) {
try {
Message empfangen = null;
while ((empfangen = (Message) this.pipedObjectInputStream.readObject()) != null) {
this.out.writeObject(empfangen);
this.out.flush();
}
} catch (ClassNotFoundException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex);
} catch (EOFException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
clientStore.removeClientByPusher(this);
} catch (IOException ex) {
System.err.println("Thread push" + this.getId() + ": Fehler: " + ex);
continueLoop = false;
clientStore.removeClientByPusher(this);
System.out.println("Thread recive " + this.getId() + ": Pipe wurde beendet");
}
}
}
public ClientPushMessageThread(ObjectOutputStream out, ObjectInputStream pipedObjectInputStream, ClientStore clientStore) {
this.out = out;
this.pipedObjectInputStream = pipedObjectInputStream;
this.clientStore = clientStore;
}
}