Chat-Applikation/src/server/ServerHandelClientsThread.java

83 lines
2.9 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.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.*;
import utils.FifoPipe;
import utils.Message;
/**
*
* @author eichehome
*/
public class ServerHandelClientsThread extends Thread {
private Socket clientSocket = null;
private ClientMessageStore centralMessageStore = null;
private FifoPipe pipe = null;
@Override
public void run() {
ObjectOutputStream out = null;
boolean continueLoop = true;
while (continueLoop) {
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream())) {
out = new ObjectOutputStream(clientSocket.getOutputStream());
Message empfangen = null;
while ((empfangen = (Message) in.readObject()) != null) {
System.out.println("client: " + empfangen);
Message message = empfangen;
out.writeObject(message);
out.flush();
System.out.println("Nachricht gesendet");
}
} catch (ClassNotFoundException ex) {
System.err.println(ex);
} catch (SocketException ex) {
System.err.println("Socket geschlossen");
centralMessageStore.removeClient(this);
continueLoop = false;
} catch (EOFException ex) {
System.err.println("Fehler: " + ex);
continueLoop = false;
} catch (IOException ex) {
System.err.println("Exeption: " + ex);
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
centralMessageStore.removeClient(this);
continueLoop = false;
} finally {
if(out != null) {
try {
out.close();
} catch (IOException ex) {
System.err.println("Fehler: " + ex);
}
}
}
}
}
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, FifoPipe pipe) {
this.clientSocket = socket;
this.centralMessageStore = messageStore;
this.pipe = pipe;
}
}