Server auf mehrere Threads und Pipes umgestellt.
This commit is contained in:
parent
70dded9ca2
commit
246abce07b
12 changed files with 515 additions and 241 deletions
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* 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.PipedWriter;
|
||||
import utils.FifoPipe;
|
||||
import utils.ArrayHelper;
|
||||
import utils.Client;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author eichehome
|
||||
*/
|
||||
public class ClientMessageStore {
|
||||
|
||||
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
|
||||
/**
|
||||
* Verzeichniss der Threads, die die Clients überwachen
|
||||
*/
|
||||
private Client[] clientThreads = null;
|
||||
|
||||
public synchronized void addClient(Thread thread, FifoPipe pipe) {
|
||||
Client client = new Client(thread, pipe);
|
||||
arrayHelper.push(client, clientThreads);
|
||||
}
|
||||
|
||||
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
|
||||
FifoPipe pipe = null;
|
||||
for (int i = 0; i < clientThreads.length; i++) {
|
||||
if (clientThreads[i].getThread().equals(thread)) {
|
||||
pipe = clientThreads[i].getPipe();
|
||||
}
|
||||
}
|
||||
if (pipe != null) {
|
||||
Client client = new Client(thread, pipe);
|
||||
return arrayHelper.pop(client, clientThreads);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Element not found");
|
||||
}
|
||||
}
|
||||
|
||||
public void notifyAllClients() {
|
||||
for (Client client : clientThreads) {
|
||||
client.getThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
65
src/server/ClientPushMessageThread.java
Normal file
65
src/server/ClientPushMessageThread.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
65
src/server/ClientReciveMessageThread.java
Normal file
65
src/server/ClientReciveMessageThread.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
148
src/server/ClientStore.java
Normal file
148
src/server/ClientStore.java
Normal file
|
@ -0,0 +1,148 @@
|
|||
/*
|
||||
* 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.ObjectOutputStream;
|
||||
import utils.Client;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author eichehome
|
||||
*/
|
||||
public class ClientStore {
|
||||
|
||||
private Client[] clients = null;
|
||||
|
||||
public void addClient(Client newClient) {
|
||||
if (this.clients != null) {
|
||||
System.out.println("Weiterer Durchlauf");
|
||||
Client [] temp = new Client [this.clients.length + 1];
|
||||
System.arraycopy(clients, 0, temp, 0, clients.length);
|
||||
temp[temp.length - 1] = newClient;
|
||||
System.out.println(newClient.getPusher() + "/" + newClient.getReciver() + "/" + newClient.getOutputStream());
|
||||
this.clients = temp;
|
||||
} else {
|
||||
System.out.println("Erser Durchlauf");
|
||||
Client[] temp = new Client[1];
|
||||
temp[0] = newClient;
|
||||
this.clients = temp;
|
||||
}
|
||||
printAll();
|
||||
}
|
||||
|
||||
public Client removeClientByPusher(Thread pusher) throws IllegalArgumentException {
|
||||
if (this.clients != null) {
|
||||
int index = -1;
|
||||
for (int i = 0; i < this.clients.length; i++) {
|
||||
if (this.clients[i].getPusher().equals(pusher)) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index != -1) {
|
||||
Client[] temp = new Client[this.clients.length - 1];
|
||||
for (int i = 0; i < index; i++) {
|
||||
temp[i] = this.clients[i];
|
||||
}
|
||||
for (int i = (index + 1); i < this.clients.length; i++) {
|
||||
temp[i - 1] = this.clients[i];
|
||||
}
|
||||
Client result = this.clients[index];
|
||||
this.clients = temp;
|
||||
return result;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Element not found" + index);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("No clients present");
|
||||
}
|
||||
}
|
||||
|
||||
public Client removeClientByReciver(Thread reciver) throws IllegalArgumentException {
|
||||
if (this.clients != null) {
|
||||
int index = -1;
|
||||
for (int i = 0; i < this.clients.length; i++) {
|
||||
if (this.clients[i].getReciver().equals(reciver)) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index != -1) {
|
||||
Client[] temp = new Client[this.clients.length - 1];
|
||||
for (int i = 0; i < index; i++) {
|
||||
temp[i] = this.clients[i];
|
||||
}
|
||||
for (int i = (index + 1); i < this.clients.length; i++) {
|
||||
temp[i - 1] = this.clients[i];
|
||||
}
|
||||
Client result = this.clients[index];
|
||||
this.clients = temp;
|
||||
return result;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Element not found");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("No clients present");
|
||||
}
|
||||
}
|
||||
|
||||
public Client removeClientByOutputStream(ObjectOutputStream out) throws IllegalArgumentException {
|
||||
if (this.clients != null) {
|
||||
int index = -1;
|
||||
for (int i = 0; i < this.clients.length; i++) {
|
||||
if (this.clients[i].getOutputStream().equals(out)) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
if (index != -1) {
|
||||
Client[] temp = new Client[this.clients.length - 1];
|
||||
for (int i = 0; i < index; i++) {
|
||||
temp[i] = this.clients[i];
|
||||
}
|
||||
for (int i = (index + 1); i < this.clients.length; i++) {
|
||||
temp[i - 1] = this.clients[i];
|
||||
}
|
||||
Client result = this.clients[index];
|
||||
this.clients = temp;
|
||||
return result;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Element not found");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("No clients present");
|
||||
}
|
||||
}
|
||||
|
||||
public ObjectOutputStream[] getAllOutputStreams() {
|
||||
ObjectOutputStream[] streams = new ObjectOutputStream[this.clients.length];
|
||||
for (int i = 0; i < this.clients.length; i++) {
|
||||
streams[i] = this.clients[i].getOutputStream();
|
||||
}
|
||||
return streams;
|
||||
}
|
||||
|
||||
public
|
||||
|
||||
private void printAll() {
|
||||
for (int i = 0; i < clients.length; i++) {
|
||||
System.out.println("ClientStore" + i + ": " + clients[i].getPusher());
|
||||
System.out.println("ClientStore" + i + ": " + clients[i].getReciver());
|
||||
System.out.println("ClientStore" + i + ": " + clients[i].getOutputStream());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -17,9 +17,15 @@
|
|||
package server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.net.*;
|
||||
import utils.Client;
|
||||
import utils.FifoPipe;
|
||||
|
||||
/**
|
||||
|
@ -34,17 +40,39 @@ public class MainServer {
|
|||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
boolean continueLoop = true;
|
||||
try (ServerSocket serverSocket = new ServerSocket(1236)) {
|
||||
ClientMessageStore messageStore = new ClientMessageStore();
|
||||
while (true) {
|
||||
ClientStore clientStore = new ClientStore();
|
||||
while (continueLoop) {
|
||||
System.out.println("Warte auf Clients");
|
||||
Socket client = serverSocket.accept();
|
||||
System.out.println("client connected");
|
||||
FifoPipe pipe = new FifoPipe();
|
||||
Thread thread = new ServerHandelClientsThread(client, messageStore, pipe);
|
||||
System.out.println("Test");
|
||||
thread.run();
|
||||
messageStore.addClient(thread, pipe);
|
||||
|
||||
|
||||
PipedOutputStream pipedOutputStream = new PipedOutputStream();
|
||||
PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
|
||||
|
||||
ObjectOutputStream pipedObjectOutputStream = new ObjectOutputStream(pipedOutputStream);
|
||||
ObjectInputStream pipedObjectInputStream = new ObjectInputStream(pipedInputStream);
|
||||
|
||||
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
|
||||
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
|
||||
|
||||
System.out.println("Streams created");
|
||||
|
||||
Thread threadRecive = new ClientReciveMessageThread(in, clientStore);
|
||||
Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore);
|
||||
|
||||
System.out.println("Threads created");
|
||||
|
||||
threadPush.start();
|
||||
threadRecive.start();
|
||||
|
||||
System.out.println("Threads started");
|
||||
|
||||
Client client = new Client(pipedObjectOutputStream, threadPush, threadRecive);
|
||||
System.out.println(client.getPusher() + "/" + client.getReciver() + "/" + client.getOutputStream());
|
||||
clientStore.addClient(client);
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.err.println("Fehler: " + ex);
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue