Implementiere die Verteilung von den Nachrichten
This commit is contained in:
parent
e0ee8be9f6
commit
fcb9dc9b5e
4 changed files with 31 additions and 42 deletions
|
@ -20,7 +20,6 @@ import java.io.PipedWriter;
|
|||
import utils.FifoPipe;
|
||||
import utils.ArrayHelper;
|
||||
import utils.Client;
|
||||
import utils.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -33,27 +32,14 @@ public class ClientMessageStore {
|
|||
* Verzeichniss der Threads, die die Clients überwachen
|
||||
*/
|
||||
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);
|
||||
arrayHelper.push(client, clientThreads);
|
||||
}
|
||||
|
||||
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
|
||||
PipedWriter pipe = null;
|
||||
FifoPipe pipe = null;
|
||||
for (int i = 0; i < clientThreads.length; i++) {
|
||||
if (clientThreads[i].getThread().equals(thread)) {
|
||||
pipe = clientThreads[i].getPipe();
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
|||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
import java.net.*;
|
||||
import utils.FifoPipe;
|
||||
|
||||
/**
|
||||
* Hauptklasse des Servers
|
||||
|
@ -38,12 +39,11 @@ public class MainServer {
|
|||
while (true) {
|
||||
Socket client = serverSocket.accept();
|
||||
System.out.println("client connected");
|
||||
PipedReader pipeOut = new PipedReader();
|
||||
PipedWriter pipeIn = new PipedWriter(pipeOut);
|
||||
Thread thread = new ServerHandelClientsThread(client, messageStore, pipeOut);
|
||||
FifoPipe pipe = new FifoPipe();
|
||||
Thread thread = new ServerHandelClientsThread(client, messageStore, pipe);
|
||||
System.out.println("Test");
|
||||
thread.run();
|
||||
messageStore.addClient(thread, pipeIn);
|
||||
messageStore.addClient(thread, pipe);
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.io.*;
|
|||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.net.*;
|
||||
import utils.FifoPipe;
|
||||
import utils.Message;
|
||||
|
||||
/**
|
||||
|
@ -30,14 +31,15 @@ public class ServerHandelClientsThread extends Thread {
|
|||
|
||||
private Socket clientSocket = null;
|
||||
private ClientMessageStore centralMessageStore = null;
|
||||
private PipedReader pipe = null;
|
||||
private FifoPipe pipe = null;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ObjectOutputStream out = null;
|
||||
boolean continueLoop = true;
|
||||
while (continueLoop) {
|
||||
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
|
||||
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) {
|
||||
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);
|
||||
|
@ -47,12 +49,11 @@ public class ServerHandelClientsThread extends Thread {
|
|||
}
|
||||
} catch (ClassNotFoundException ex) {
|
||||
System.err.println(ex);
|
||||
//for(String clientInput = in.readLine(); clientInput != null; clientInput = in.readLine()) {
|
||||
//}
|
||||
} catch (InterruptedException ex) {
|
||||
System.err.println("test gegkückt");
|
||||
System.err.println(ex);
|
||||
pr.write("Test");
|
||||
Message message = pipe.read();
|
||||
out.writeObject();
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (SocketException ex) {
|
||||
System.err.println("Socket geschlossen");
|
||||
|
@ -66,11 +67,19 @@ public class ServerHandelClientsThread extends Thread {
|
|||
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, PipedReader pipe) {
|
||||
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, FifoPipe pipe) {
|
||||
this.clientSocket = socket;
|
||||
this.centralMessageStore = messageStore;
|
||||
this.pipe = pipe;
|
||||
|
|
|
@ -16,35 +16,29 @@
|
|||
*/
|
||||
package utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PipedWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author eichehome
|
||||
*/
|
||||
public class Client {
|
||||
private Thread thread = null;
|
||||
private PipedWriter pipe = null;
|
||||
|
||||
public Client(Thread thread, PipedWriter pipe) {
|
||||
private Thread thread = null;
|
||||
private FifoPipe pipe = null;
|
||||
|
||||
public Client(Thread thread, FifoPipe pipe) {
|
||||
this.thread = thread;
|
||||
this.pipe = pipe;
|
||||
}
|
||||
|
||||
public void writePipe(Message message) {
|
||||
try {
|
||||
this.pipe.write(message.toString());
|
||||
} catch (IOException ex) {
|
||||
System.err.println("Fehler: " + ex);
|
||||
}
|
||||
this.pipe.setElement(message);
|
||||
}
|
||||
|
||||
public Thread getThread() {
|
||||
return this.thread;
|
||||
}
|
||||
|
||||
public PipedWriter getPipe() {
|
||||
public FifoPipe getPipe() {
|
||||
return this.pipe;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue