2021-05-11 11:59:08 +00:00
|
|
|
/*
|
2021-05-13 19:09:10 +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/>.
|
2021-05-11 11:59:08 +00:00
|
|
|
*/
|
|
|
|
package server;
|
|
|
|
|
|
|
|
import java.io.*;
|
2021-05-13 16:33:30 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2021-05-14 15:44:48 +00:00
|
|
|
import java.net.*;
|
2021-05-14 20:18:23 +00:00
|
|
|
import utils.FifoPipe;
|
2021-05-14 15:44:48 +00:00
|
|
|
import utils.Message;
|
2021-05-11 11:59:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author eichehome
|
|
|
|
*/
|
2021-05-13 22:25:42 +00:00
|
|
|
public class ServerHandelClientsThread extends Thread {
|
2021-05-11 11:59:08 +00:00
|
|
|
|
|
|
|
private Socket clientSocket = null;
|
2021-05-13 22:25:42 +00:00
|
|
|
private ClientMessageStore centralMessageStore = null;
|
2021-05-14 20:18:23 +00:00
|
|
|
private FifoPipe pipe = null;
|
2021-05-11 11:59:08 +00:00
|
|
|
|
2021-05-13 19:09:10 +00:00
|
|
|
@Override
|
2021-05-11 11:59:08 +00:00
|
|
|
public void run() {
|
2021-05-14 20:18:23 +00:00
|
|
|
ObjectOutputStream out = null;
|
2021-05-13 22:25:42 +00:00
|
|
|
boolean continueLoop = true;
|
|
|
|
while (continueLoop) {
|
2021-05-14 20:18:23 +00:00
|
|
|
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream())) {
|
|
|
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
2021-05-14 15:44:48 +00:00
|
|
|
Message empfangen = null;
|
|
|
|
while ((empfangen = (Message) in.readObject()) != null) {
|
|
|
|
System.out.println("client: " + empfangen);
|
|
|
|
Message message = empfangen;
|
|
|
|
out.writeObject(message);
|
|
|
|
out.flush();
|
2021-05-13 19:09:10 +00:00
|
|
|
}
|
2021-05-14 15:44:48 +00:00
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
|
System.err.println(ex);
|
2021-05-13 19:09:10 +00:00
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
System.err.println("test gegkückt");
|
|
|
|
System.err.println(ex);
|
2021-05-14 20:18:23 +00:00
|
|
|
Message message = pipe.read();
|
|
|
|
out.writeObject();
|
2021-05-13 22:25:42 +00:00
|
|
|
Thread.currentThread().interrupt();
|
2021-05-14 15:44:48 +00:00
|
|
|
} catch (SocketException ex) {
|
|
|
|
System.err.println("Socket geschlossen");
|
2021-05-14 18:02:26 +00:00
|
|
|
centralMessageStore.removeClient(this);
|
|
|
|
continueLoop = false;
|
|
|
|
} catch (EOFException ex) {
|
|
|
|
System.err.println("Fehler: " + ex);
|
2021-05-14 15:44:48 +00:00
|
|
|
continueLoop = false;
|
2021-05-13 19:09:10 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
System.err.println("Exeption: " + ex);
|
|
|
|
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
|
2021-05-14 18:02:26 +00:00
|
|
|
centralMessageStore.removeClient(this);
|
2021-05-13 22:25:42 +00:00
|
|
|
continueLoop = false;
|
2021-05-14 20:18:23 +00:00
|
|
|
} finally {
|
|
|
|
if(out != null) {
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
System.err.println("Fehler: " + ex);
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 19:09:10 +00:00
|
|
|
}
|
2021-05-13 16:33:30 +00:00
|
|
|
}
|
2021-05-11 11:59:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 20:18:23 +00:00
|
|
|
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, FifoPipe pipe) {
|
2021-05-14 18:02:26 +00:00
|
|
|
this.clientSocket = socket;
|
|
|
|
this.centralMessageStore = messageStore;
|
|
|
|
this.pipe = pipe;
|
2021-05-11 11:59:08 +00:00
|
|
|
}
|
|
|
|
}
|