Server nimmt nun Nachrichten an und Schickt diese zurück.
This commit is contained in:
parent
6f39b1ba46
commit
e0ee8be9f6
5 changed files with 90 additions and 16 deletions
|
@ -16,8 +16,10 @@
|
|||
*/
|
||||
package server;
|
||||
|
||||
import java.io.PipedWriter;
|
||||
import utils.FifoPipe;
|
||||
import utils.ArrayHelper;
|
||||
import utils.Client;
|
||||
import utils.Message;
|
||||
|
||||
/**
|
||||
|
@ -26,11 +28,11 @@ import utils.Message;
|
|||
*/
|
||||
public class ClientMessageStore {
|
||||
|
||||
private ArrayHelper<Thread> arrayHelper = new ArrayHelper<>();
|
||||
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
|
||||
/**
|
||||
* Verzeichniss der Threads, die die Clients überwachen
|
||||
*/
|
||||
private Thread[] clientThreads = null;
|
||||
private Client[] clientThreads = null;
|
||||
/**
|
||||
* Puffer der Nachrichten
|
||||
*/
|
||||
|
@ -45,17 +47,29 @@ public class ClientMessageStore {
|
|||
return result;
|
||||
}
|
||||
|
||||
public synchronized void addThread(Thread thread) {
|
||||
arrayHelper.pushThread(thread, clientThreads);
|
||||
public synchronized void addClient(Thread thread, PipedWriter pipe) {
|
||||
Client client = new Client(thread, pipe);
|
||||
arrayHelper.push(client, clientThreads);
|
||||
}
|
||||
|
||||
public synchronized Thread removeThread(Thread thread) {
|
||||
return arrayHelper.popThread(thread, clientThreads);
|
||||
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
|
||||
PipedWriter 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 (int i = 0; i < clientThreads.length; i++) {
|
||||
clientThreads[i].interrupt();
|
||||
for (Client client : clientThreads) {
|
||||
client.getThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
package server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
|
@ -36,10 +38,12 @@ public class MainServer {
|
|||
while (true) {
|
||||
Socket client = serverSocket.accept();
|
||||
System.out.println("client connected");
|
||||
Thread thread = new ServerHandelClientsThread(client, messageStore);
|
||||
PipedReader pipeOut = new PipedReader();
|
||||
PipedWriter pipeIn = new PipedWriter(pipeOut);
|
||||
Thread thread = new ServerHandelClientsThread(client, messageStore, pipeOut);
|
||||
System.out.println("Test");
|
||||
thread.run();
|
||||
messageStore.addThread(thread);
|
||||
messageStore.addClient(thread, pipeIn);
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
|
|
@ -30,7 +30,7 @@ public class ServerHandelClientsThread extends Thread {
|
|||
|
||||
private Socket clientSocket = null;
|
||||
private ClientMessageStore centralMessageStore = null;
|
||||
private PrintWriter pr = null;
|
||||
private PipedReader pipe = null;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -56,17 +56,23 @@ public class ServerHandelClientsThread extends Thread {
|
|||
Thread.currentThread().interrupt();
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore) {
|
||||
clientSocket = socket;
|
||||
centralMessageStore = messageStore;
|
||||
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, PipedReader pipe) {
|
||||
this.clientSocket = socket;
|
||||
this.centralMessageStore = messageStore;
|
||||
this.pipe = pipe;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue