Server auf mehrere Threads und Pipes umgestellt.

This commit is contained in:
eichehome 2021-05-15 22:53:58 +02:00
parent 70dded9ca2
commit 246abce07b
12 changed files with 515 additions and 241 deletions

171
src/client/Client.java Normal file
View file

@ -0,0 +1,171 @@
package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import utils.Message;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.awt.event.ActionEvent;
public class Client extends JFrame {
private static JPanel contentPane;
JTextField txtMessage;
private JTextField txtUsername;
public static String currentText = " ";
public static int i = 0;
public static Object tosend = new Message("Leer", "Leer");
public static Client t1;
public static Socket socket;
public static ObjectInputStream obinstr;
public static ObjectOutputStream oboust;
public static void main(String[] args) {
t1 = new Client();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
start();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Client() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtMessage = new JTextField();
txtMessage.setText("Message");
txtMessage.setBounds(117, 29, 218, 20);
contentPane.add(txtMessage);
txtMessage.setColumns(10);
txtUsername = new JTextField();
txtUsername.setText("Username");
txtUsername.setBounds(21, 29, 86, 20);
contentPane.add(txtUsername);
txtUsername.setColumns(10);
JButton btnSend = new JButton("Send");
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String themessage = txtMessage.getText();
String theusername = txtUsername.getText();
tosend = new Message(theusername, themessage);
i = 1;
System.out.println("Button pressed");
try {
oboust.writeObject(tosend);
oboust.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnSend.setBounds(345, 28, 79, 23);
contentPane.add(btnSend);
JTextArea textArea = new JTextArea(currentText);
textArea.setLineWrap(true);
textArea.setForeground(Color.WHITE);
textArea.setBackground(Color.LIGHT_GRAY);
textArea.setBounds(20, 60, 323, 176);
contentPane.add(textArea);
}
public static void print(JTextArea textArea, String asd){
currentText = asd + "\n" + currentText;
textArea.setText(currentText);
}
public static void start() throws IOException, ClassNotFoundException{
Scanner scan = new Scanner(System.in);
JTextArea textArea = new JTextArea(currentText);
textArea.setLineWrap(true);
textArea.setForeground(Color.WHITE);
textArea.setBackground(Color.BLACK);
textArea.setBounds(20, 60, 323, 176);
contentPane.add(textArea);
socket = new Socket( "localhost", 1236 );
oboust = new ObjectOutputStream(socket.getOutputStream());
obinstr = new ObjectInputStream(socket.getInputStream());
Object erhalten = null;
Message message = new Message("leer", "leer");
while(!message.getMessage().equals("exit")){
System.out.println("While");
try{
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
System.out.println("Nachricht erhalten");
message = (Message)erhalten;
System.out.println(message.getMessage());
String Ausgeben = "[" + message.getUsername() + "] " + message.getMessage();
print(textArea, Ausgeben);
System.out.println("[" + message.getUsername() + "] " + message.getMessage());
oboust.flush();
}catch(Exception f){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
oboust.close();
obinstr.close();
socket.close();
}
}

View file

@ -221,10 +221,11 @@ public class Client1 extends JFrame {
System.out.println("WHILE");
try{
//System.out.println("hile4");
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
System.out.println("Nachricht erhalten");
erhalten = obinstr.readObject(); //Bleibt hier stehen, bis ein neues Object reinkommt
message = (Message)erhalten;
System.out.println("[" + message.getUsername() + "] " + message.getMessage());
String Ausgeben = message.getTime() + " [" + message.getUsername() + "] " + message.getMessage();
print(textArea, Ausgeben);
//System.out.println("[CLIENT] NACHRICHT ERHALTEN");

View file

@ -1,61 +0,0 @@
/*
* 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 server;
import java.io.PipedWriter;
import utils.FifoPipe;
import utils.ArrayHelper;
import utils.Client;
/**
*
* @author eichehome
*/
public class ClientMessageStore {
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
/**
* Verzeichniss der Threads, die die Clients überwachen
*/
private Client[] clientThreads = null;
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 {
FifoPipe 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 (Client client : clientThreads) {
client.getThread().interrupt();
}
}
}

View file

@ -0,0 +1,65 @@
/*
* 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 server;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import utils.Message;
/**
*
* @author eichehome
*/
public class ClientPushMessageThread extends Thread {
private final ObjectOutputStream out;
private final ObjectInputStream pipedObjectInputStream;
private ClientStore clientStore;
public void run() {
boolean continueLoop = true;
while (continueLoop) {
try {
Message empfangen = null;
while ((empfangen = (Message) this.pipedObjectInputStream.readObject()) != null) {
this.out.writeObject(empfangen);
this.out.flush();
}
} catch (ClassNotFoundException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex);
} catch (EOFException ex) {
System.err.println("Thread push " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
clientStore.removeClientByPusher(this);
} catch (IOException ex) {
System.err.println("Thread push" + this.getId() + ": Fehler: " + ex);
continueLoop = false;
clientStore.removeClientByPusher(this);
System.out.println("Thread recive " + this.getId() + ": Pipe wurde beendet");
}
}
}
public ClientPushMessageThread(ObjectOutputStream out, ObjectInputStream pipedObjectInputStream, ClientStore clientStore) {
this.out = out;
this.pipedObjectInputStream = pipedObjectInputStream;
this.clientStore = clientStore;
}
}

View file

@ -0,0 +1,65 @@
/*
* 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 server;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import utils.Message;
/**
*
* @author eichehome
*/
public class ClientReciveMessageThread extends Thread {
private ClientStore clientStore;
private final ObjectInputStream in;
public void run() {
System.out.println("Thread" + this.getId() + ": Gestartet");
boolean continueLoop = true;
while (continueLoop) {
try {
Message empfangen = null;
while ((empfangen = (Message) this.in.readObject()) != null) {
System.out.println("Thread" + this.getId() + ": Client: " + empfangen.getMessage() + " from: " + empfangen.getUsername());
for (ObjectOutputStream stream : clientStore.getAllOutputStreams()) {
stream.writeObject(empfangen);
stream.flush();
System.out.println("Weitergeleitet");
}
}
} catch (ClassNotFoundException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
} catch (EOFException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
System.out.println("Thread push " + this.getId() + ": Socket wurde beendet");
} catch (IOException ex) {
System.err.println("Thread recive " + this.getId() + ": Fehler: " + ex);
continueLoop = false;
}
}
}
public ClientReciveMessageThread(ObjectInputStream in, ClientStore clientStore) {
this.in = in;
this.clientStore = clientStore;
}
}

148
src/server/ClientStore.java Normal file
View file

@ -0,0 +1,148 @@
/*
* 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 server;
import java.io.ObjectOutputStream;
import utils.Client;
/**
*
* @author eichehome
*/
public class ClientStore {
private Client[] clients = null;
public void addClient(Client newClient) {
if (this.clients != null) {
System.out.println("Weiterer Durchlauf");
Client [] temp = new Client [this.clients.length + 1];
System.arraycopy(clients, 0, temp, 0, clients.length);
temp[temp.length - 1] = newClient;
System.out.println(newClient.getPusher() + "/" + newClient.getReciver() + "/" + newClient.getOutputStream());
this.clients = temp;
} else {
System.out.println("Erser Durchlauf");
Client[] temp = new Client[1];
temp[0] = newClient;
this.clients = temp;
}
printAll();
}
public Client removeClientByPusher(Thread pusher) throws IllegalArgumentException {
if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (this.clients[i].getPusher().equals(pusher)) {
index = i;
break;
}
}
if (index != -1) {
Client[] temp = new Client[this.clients.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = this.clients[i];
}
for (int i = (index + 1); i < this.clients.length; i++) {
temp[i - 1] = this.clients[i];
}
Client result = this.clients[index];
this.clients = temp;
return result;
} else {
throw new IllegalArgumentException("Element not found" + index);
}
} else {
throw new IllegalArgumentException("No clients present");
}
}
public Client removeClientByReciver(Thread reciver) throws IllegalArgumentException {
if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (this.clients[i].getReciver().equals(reciver)) {
index = i;
break;
}
}
if (index != -1) {
Client[] temp = new Client[this.clients.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = this.clients[i];
}
for (int i = (index + 1); i < this.clients.length; i++) {
temp[i - 1] = this.clients[i];
}
Client result = this.clients[index];
this.clients = temp;
return result;
} else {
throw new IllegalArgumentException("Element not found");
}
} else {
throw new IllegalArgumentException("No clients present");
}
}
public Client removeClientByOutputStream(ObjectOutputStream out) throws IllegalArgumentException {
if (this.clients != null) {
int index = -1;
for (int i = 0; i < this.clients.length; i++) {
if (this.clients[i].getOutputStream().equals(out)) {
index = i;
}
}
if (index != -1) {
Client[] temp = new Client[this.clients.length - 1];
for (int i = 0; i < index; i++) {
temp[i] = this.clients[i];
}
for (int i = (index + 1); i < this.clients.length; i++) {
temp[i - 1] = this.clients[i];
}
Client result = this.clients[index];
this.clients = temp;
return result;
} else {
throw new IllegalArgumentException("Element not found");
}
} else {
throw new IllegalArgumentException("No clients present");
}
}
public ObjectOutputStream[] getAllOutputStreams() {
ObjectOutputStream[] streams = new ObjectOutputStream[this.clients.length];
for (int i = 0; i < this.clients.length; i++) {
streams[i] = this.clients[i].getOutputStream();
}
return streams;
}
public
private void printAll() {
for (int i = 0; i < clients.length; i++) {
System.out.println("ClientStore" + i + ": " + clients[i].getPusher());
System.out.println("ClientStore" + i + ": " + clients[i].getReciver());
System.out.println("ClientStore" + i + ": " + clients[i].getOutputStream());
}
}
}

View file

@ -17,9 +17,15 @@
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.lang.invoke.MethodHandles;
import java.net.*;
import utils.Client;
import utils.FifoPipe;
/**
@ -34,16 +40,38 @@ public class MainServer {
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean continueLoop = true;
try (ServerSocket serverSocket = new ServerSocket(1236)) {
ClientMessageStore messageStore = new ClientMessageStore();
while (true) {
ClientStore clientStore = new ClientStore();
while (continueLoop) {
System.out.println("Warte auf Clients");
Socket client = serverSocket.accept();
System.out.println("client connected");
FifoPipe pipe = new FifoPipe();
Thread thread = new ServerHandelClientsThread(client, messageStore, pipe);
System.out.println("Test");
thread.run();
messageStore.addClient(thread, pipe);
PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream);
ObjectOutputStream pipedObjectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectInputStream pipedObjectInputStream = new ObjectInputStream(pipedInputStream);
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
System.out.println("Streams created");
Thread threadRecive = new ClientReciveMessageThread(in, clientStore);
Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore);
System.out.println("Threads created");
threadPush.start();
threadRecive.start();
System.out.println("Threads started");
Client client = new Client(pipedObjectOutputStream, threadPush, threadRecive);
System.out.println(client.getPusher() + "/" + client.getReciver() + "/" + client.getOutputStream());
clientStore.addClient(client);
}
} catch (Exception ex) {

View file

@ -1,82 +0,0 @@
/*
* 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 server;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.*;
import utils.FifoPipe;
import utils.Message;
/**
*
* @author eichehome
*/
public class ServerHandelClientsThread extends Thread {
private Socket clientSocket = null;
private ClientMessageStore centralMessageStore = null;
private FifoPipe pipe = null;
@Override
public void run() {
ObjectOutputStream out = null;
boolean continueLoop = true;
while (continueLoop) {
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);
Message message = empfangen;
out.writeObject(message);
out.flush();
System.out.println("Nachricht gesendet");
}
} catch (ClassNotFoundException ex) {
System.err.println(ex);
} 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;
} finally {
if(out != null) {
try {
out.close();
} catch (IOException ex) {
System.err.println("Fehler: " + ex);
}
}
}
}
}
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, FifoPipe pipe) {
this.clientSocket = socket;
this.centralMessageStore = messageStore;
this.pipe = pipe;
}
}

View file

@ -11,7 +11,9 @@ public class MainClient {
public MainClient() throws IOException, ClassNotFoundException {
System.out.println("[CLIENT] START");
Socket socket = new Socket( "localhost", 1236 );
System.out.println("[CLIENT] Debug1");
ObjectOutputStream oboust = new ObjectOutputStream(socket.getOutputStream());
System.out.println("[CLIENT] Debug2");
ObjectInputStream obinstr = new ObjectInputStream(socket.getInputStream());
System.out.println("[CLIENT] START1");
Message senden = new Message( "berdan", "test" );
@ -28,6 +30,7 @@ public class MainClient {
while(erhalten==null){
erhalten = obinstr.readObject();
System.out.println(erhalten);
}
System.out.println("[CLIENT] NACHRICHT ERHALTEN");
System.out.println(erhalten); //nickname=berdan, message=test

View file

@ -16,14 +16,6 @@
*/
package test;
import utils.ArrayHelper;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import utils.FifoPipe;
import utils.Message;
import utils.PipeElement;
/**
*
* @author eichehome

View file

@ -1,69 +0,0 @@
/*
* 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;
/**
*
* @author eichehome
* @param <T> Type des Arrays, mit dem gearbeitet wird.
*/
public class ArrayHelper<T> {
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++) {
temp[i] = array[i];
}
temp[temp.length - 1] = element;//Letzter eintrag
array = temp;
} else {
T[] temp = new T[1];
temp[temp.length - 1] = element;
array = temp;
}
return array;
}
public T pop(T element, T[] array) throws IllegalArgumentException {
if (array != null) {
int index = getIndex(element, array);
T result = array[index];
T[] temp = new T[array.length - 2];
for (int i = 0; i < index; i++) {
temp[i] = array[i];
}
for (int i = ++index; i < temp.length; i++) {
temp[i - 1] = array[i];
}
return result;
} else {
throw new IllegalArgumentException("Element not found");
}
}
int getIndex(T match, T[] array) {
int result = -1;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(match)) {
result = i;
break;
}
}
return result;
}
}

View file

@ -16,29 +16,42 @@
*/
package utils;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
*
* @author eichehome
*/
public class Client {
private Thread thread = null;
private FifoPipe pipe = null;
private final ObjectOutputStream out;
private final Thread pusher;
private final Thread reciver;
public Client(Thread thread, FifoPipe pipe) {
this.thread = thread;
this.pipe = pipe;
public Client(ObjectOutputStream out, Thread pusher, Thread reciver) {
this.out = out;
this.pusher = pusher;
this.reciver = reciver;
}
public void writePipe(Message message) {
this.pipe.setElement(message);
public void writeMessage(Message message) {
try {
this.out.writeObject(message);
} catch (IOException ex) {
System.err.println("Cliet: Fehler: " + ex);
}
}
public Thread getThread() {
return this.thread;
public ObjectOutputStream getOutputStream() {
return this.out;
}
public FifoPipe getPipe() {
return this.pipe;
public Thread getPusher() {
return this.pusher;
}
public Thread getReciver() {
return this.reciver;
}
}