Chat-Applikation/src/server/ClientStore.java

149 lines
5.3 KiB
Java

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