Implementiere die Verteilung von den Nachrichten

This commit is contained in:
eichehome 2021-05-14 22:18:23 +02:00
parent e0ee8be9f6
commit fcb9dc9b5e
4 changed files with 31 additions and 42 deletions

View file

@ -20,7 +20,6 @@ import java.io.PipedWriter;
import utils.FifoPipe; import utils.FifoPipe;
import utils.ArrayHelper; import utils.ArrayHelper;
import utils.Client; import utils.Client;
import utils.Message;
/** /**
* *
@ -33,27 +32,14 @@ public class ClientMessageStore {
* Verzeichniss der Threads, die die Clients überwachen * Verzeichniss der Threads, die die Clients überwachen
*/ */
private Client[] clientThreads = null; private Client[] clientThreads = null;
/**
* Puffer der Nachrichten
*/
private FifoPipe messages = new FifoPipe();
public synchronized void pushMessage(Message message) {
messages.setElement(message);
}
public synchronized Message popMessage() {
Message result = messages.getNextElement();
return result;
}
public synchronized void addClient(Thread thread, PipedWriter pipe) { public synchronized void addClient(Thread thread, FifoPipe pipe) {
Client client = new Client(thread, pipe); Client client = new Client(thread, pipe);
arrayHelper.push(client, clientThreads); arrayHelper.push(client, clientThreads);
} }
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException { public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
PipedWriter pipe = null; FifoPipe pipe = null;
for (int i = 0; i < clientThreads.length; i++) { for (int i = 0; i < clientThreads.length; i++) {
if (clientThreads[i].getThread().equals(thread)) { if (clientThreads[i].getThread().equals(thread)) {
pipe = clientThreads[i].getPipe(); pipe = clientThreads[i].getPipe();

View file

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.PipedReader; import java.io.PipedReader;
import java.io.PipedWriter; import java.io.PipedWriter;
import java.net.*; import java.net.*;
import utils.FifoPipe;
/** /**
* Hauptklasse des Servers * Hauptklasse des Servers
@ -38,12 +39,11 @@ public class MainServer {
while (true) { while (true) {
Socket client = serverSocket.accept(); Socket client = serverSocket.accept();
System.out.println("client connected"); System.out.println("client connected");
PipedReader pipeOut = new PipedReader(); FifoPipe pipe = new FifoPipe();
PipedWriter pipeIn = new PipedWriter(pipeOut); Thread thread = new ServerHandelClientsThread(client, messageStore, pipe);
Thread thread = new ServerHandelClientsThread(client, messageStore, pipeOut);
System.out.println("Test"); System.out.println("Test");
thread.run(); thread.run();
messageStore.addClient(thread, pipeIn); messageStore.addClient(thread, pipe);
} }
} catch (Exception ex) { } catch (Exception ex) {

View file

@ -20,6 +20,7 @@ import java.io.*;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.net.*; import java.net.*;
import utils.FifoPipe;
import utils.Message; import utils.Message;
/** /**
@ -30,14 +31,15 @@ public class ServerHandelClientsThread extends Thread {
private Socket clientSocket = null; private Socket clientSocket = null;
private ClientMessageStore centralMessageStore = null; private ClientMessageStore centralMessageStore = null;
private PipedReader pipe = null; private FifoPipe pipe = null;
@Override @Override
public void run() { public void run() {
ObjectOutputStream out = null;
boolean continueLoop = true; boolean continueLoop = true;
while (continueLoop) { while (continueLoop) {
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream()); try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream())) {
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) { out = new ObjectOutputStream(clientSocket.getOutputStream());
Message empfangen = null; Message empfangen = null;
while ((empfangen = (Message) in.readObject()) != null) { while ((empfangen = (Message) in.readObject()) != null) {
System.out.println("client: " + empfangen); System.out.println("client: " + empfangen);
@ -47,12 +49,11 @@ public class ServerHandelClientsThread extends Thread {
} }
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
System.err.println(ex); System.err.println(ex);
//for(String clientInput = in.readLine(); clientInput != null; clientInput = in.readLine()) {
//}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
System.err.println("test gegkückt"); System.err.println("test gegkückt");
System.err.println(ex); System.err.println(ex);
pr.write("Test"); Message message = pipe.read();
out.writeObject();
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (SocketException ex) { } catch (SocketException ex) {
System.err.println("Socket geschlossen"); System.err.println("Socket geschlossen");
@ -66,11 +67,19 @@ public class ServerHandelClientsThread extends Thread {
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
centralMessageStore.removeClient(this); centralMessageStore.removeClient(this);
continueLoop = false; continueLoop = false;
} finally {
if(out != null) {
try {
out.close();
} catch (IOException ex) {
System.err.println("Fehler: " + ex);
}
}
} }
} }
} }
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, PipedReader pipe) { public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, FifoPipe pipe) {
this.clientSocket = socket; this.clientSocket = socket;
this.centralMessageStore = messageStore; this.centralMessageStore = messageStore;
this.pipe = pipe; this.pipe = pipe;

View file

@ -16,35 +16,29 @@
*/ */
package utils; package utils;
import java.io.IOException;
import java.io.PipedWriter;
/** /**
* *
* @author eichehome * @author eichehome
*/ */
public class Client { public class Client {
private Thread thread = null; private Thread thread = null;
private PipedWriter pipe = null; private FifoPipe pipe = null;
public Client(Thread thread, PipedWriter pipe) { public Client(Thread thread, FifoPipe pipe) {
this.thread = thread; this.thread = thread;
this.pipe = pipe; this.pipe = pipe;
} }
public void writePipe(Message message) { public void writePipe(Message message) {
try { this.pipe.setElement(message);
this.pipe.write(message.toString());
} catch (IOException ex) {
System.err.println("Fehler: " + ex);
}
} }
public Thread getThread() { public Thread getThread() {
return this.thread; return this.thread;
} }
public PipedWriter getPipe() { public FifoPipe getPipe() {
return this.pipe; return this.pipe;
} }
} }