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;
|
package server;
|
||||||
|
|
||||||
|
import java.io.PipedWriter;
|
||||||
import utils.FifoPipe;
|
import utils.FifoPipe;
|
||||||
import utils.ArrayHelper;
|
import utils.ArrayHelper;
|
||||||
|
import utils.Client;
|
||||||
import utils.Message;
|
import utils.Message;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,11 +28,11 @@ import utils.Message;
|
||||||
*/
|
*/
|
||||||
public class ClientMessageStore {
|
public class ClientMessageStore {
|
||||||
|
|
||||||
private ArrayHelper<Thread> arrayHelper = new ArrayHelper<>();
|
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
|
||||||
/**
|
/**
|
||||||
* Verzeichniss der Threads, die die Clients überwachen
|
* Verzeichniss der Threads, die die Clients überwachen
|
||||||
*/
|
*/
|
||||||
private Thread[] clientThreads = null;
|
private Client[] clientThreads = null;
|
||||||
/**
|
/**
|
||||||
* Puffer der Nachrichten
|
* Puffer der Nachrichten
|
||||||
*/
|
*/
|
||||||
|
@ -45,17 +47,29 @@ public class ClientMessageStore {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void addThread(Thread thread) {
|
public synchronized void addClient(Thread thread, PipedWriter pipe) {
|
||||||
arrayHelper.pushThread(thread, clientThreads);
|
Client client = new Client(thread, pipe);
|
||||||
|
arrayHelper.push(client, clientThreads);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized Thread removeThread(Thread thread) {
|
public synchronized Client removeClient(Thread thread) throws IllegalArgumentException {
|
||||||
return arrayHelper.popThread(thread, clientThreads);
|
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() {
|
public void notifyAllClients() {
|
||||||
for (int i = 0; i < clientThreads.length; i++) {
|
for (Client client : clientThreads) {
|
||||||
clientThreads[i].interrupt();
|
client.getThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
package server;
|
package server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PipedReader;
|
||||||
|
import java.io.PipedWriter;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,10 +38,12 @@ 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");
|
||||||
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");
|
System.out.println("Test");
|
||||||
thread.run();
|
thread.run();
|
||||||
messageStore.addThread(thread);
|
messageStore.addClient(thread, pipeIn);
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class ServerHandelClientsThread extends Thread {
|
||||||
|
|
||||||
private Socket clientSocket = null;
|
private Socket clientSocket = null;
|
||||||
private ClientMessageStore centralMessageStore = null;
|
private ClientMessageStore centralMessageStore = null;
|
||||||
private PrintWriter pr = null;
|
private PipedReader pipe = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -56,17 +56,23 @@ public class ServerHandelClientsThread extends Thread {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
} catch (SocketException ex) {
|
} catch (SocketException ex) {
|
||||||
System.err.println("Socket geschlossen");
|
System.err.println("Socket geschlossen");
|
||||||
|
centralMessageStore.removeClient(this);
|
||||||
|
continueLoop = false;
|
||||||
|
} catch (EOFException ex) {
|
||||||
|
System.err.println("Fehler: " + ex);
|
||||||
continueLoop = false;
|
continueLoop = false;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
System.err.println("Exeption: " + ex);
|
System.err.println("Exeption: " + ex);
|
||||||
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
centralMessageStore.removeClient(this);
|
||||||
continueLoop = false;
|
continueLoop = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore) {
|
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, PipedReader pipe) {
|
||||||
clientSocket = socket;
|
this.clientSocket = socket;
|
||||||
centralMessageStore = messageStore;
|
this.centralMessageStore = messageStore;
|
||||||
|
this.pipe = pipe;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ package utils;
|
||||||
*/
|
*/
|
||||||
public class ArrayHelper<T> {
|
public class ArrayHelper<T> {
|
||||||
|
|
||||||
public T[] pushThread(T element, T[] array) {
|
public T[] push(T element, T[] array) {
|
||||||
if (array != null) {
|
if (array != null) {
|
||||||
T[] temp = new T[1 + array.length];
|
T[] temp = new T[1 + array.length];
|
||||||
for (int i = 0; i < array.length; i++) {
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
@ -39,7 +39,7 @@ public class ArrayHelper<T> {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T popThread(T element, T[] array) throws IllegalArgumentException {
|
public T pop(T element, T[] array) throws IllegalArgumentException {
|
||||||
if (array != null) {
|
if (array != null) {
|
||||||
int index = getIndex(element, array);
|
int index = getIndex(element, array);
|
||||||
T result = array[index];
|
T result = array[index];
|
||||||
|
|
50
src/utils/Client.java
Normal file
50
src/utils/Client.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue