Server nimmt nun Nachrichten an und Schickt diese zurück.

This commit is contained in:
eichehome 2021-05-14 20:02:26 +02:00
parent 6f39b1ba46
commit e0ee8be9f6
5 changed files with 90 additions and 16 deletions

View file

@ -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();
}
}
}

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -23,7 +23,7 @@ package utils;
*/
public class ArrayHelper<T> {
public T[] pushThread(T element, T[] array) {
public T[] push(T element, T[] array) {
if (array != null) {
T[] temp = new T[1 + array.length];
for (int i = 0; i < array.length; i++) {
@ -39,7 +39,7 @@ public class ArrayHelper<T> {
return array;
}
public T popThread(T element, T[] array) throws IllegalArgumentException {
public T pop(T element, T[] array) throws IllegalArgumentException {
if (array != null) {
int index = getIndex(element, array);
T result = array[index];

50
src/utils/Client.java Normal file
View file

@ -0,0 +1,50 @@
/*
* 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 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) {
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);
}
}
public Thread getThread() {
return this.thread;
}
public PipedWriter getPipe() {
return this.pipe;
}
}