128 lines
5 KiB
Java
128 lines
5 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.IOException;
|
|
import java.io.ObjectOutputStream;
|
|
import java.util.logging.Logger;
|
|
import java.util.logging.Level;
|
|
import utils.Client;
|
|
|
|
/**
|
|
* Diese Klasse Stellt das zentrale Verzeichniss aller Clients dar.
|
|
*
|
|
* @author eichehome
|
|
*/
|
|
public class ClientStore {
|
|
|
|
private Client[] clients = null;
|
|
|
|
/**
|
|
* Diese Funktion fügt einen Client dem Verzeichniss aller Clients hinzu, die Verbunden sind.
|
|
* @param newClient Der neue Client, der hinzugefügt werden soll.
|
|
*/
|
|
public void addClient(Client newClient) {
|
|
if (this.clients != null) {
|
|
Client[] temp = new Client[this.clients.length + 1];
|
|
System.arraycopy(clients, 0, temp, 0, clients.length);
|
|
temp[temp.length - 1] = newClient;
|
|
this.clients = temp;
|
|
} else {
|
|
Client[] temp = new Client[1];
|
|
temp[0] = newClient;
|
|
this.clients = temp;
|
|
}
|
|
Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Client hinzugefügt");
|
|
}
|
|
|
|
/**
|
|
* Dies Funktion entfernt einen Client aus dem Verzeichnis aller verbundenen Clients.
|
|
* @param pusher Der Pusher-Thread des Clients, welcher aus dem Verzeichnis entfernt werden soll.
|
|
* @return Gibt die zu dem Client gehörenden Streams und Threads zurück.
|
|
* @throws IllegalArgumentException Falls der Thread nicht existiert oder keine Clients vorhanden sind, wird eine Exeption mit der passenden Fehlermeldung geworfen.
|
|
*/
|
|
public Client removeClient(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;
|
|
Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Client gelöscht");
|
|
return result;
|
|
} else {
|
|
throw new IllegalArgumentException("Element not found");
|
|
}
|
|
} else {
|
|
throw new IllegalArgumentException("No clients present");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Diese Funktion gibt alle Streams zu den Clients zurück, die derzeit Verbunden sind.
|
|
* @return Ein Array aus allen Streams
|
|
*/
|
|
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();
|
|
}
|
|
Logger.getLogger(ClientStore.class.getName()).log(Level.INFO, "ClientStore: Alle Streams zusammengesucht");
|
|
return streams;
|
|
}
|
|
|
|
/**
|
|
* Schließt den zu einem Thread gehörenden OutputStream
|
|
* @param thread Der Thread, dessen OutputStream geschlossen werden soll
|
|
* @throws IllegalArgumentException Falls der Thread nicht existiert oder keine Clients vorhanden sind, wird eine Exeption mit der passenden Fehlermeldung geworfen.
|
|
*/
|
|
public void close(Thread thread) throws IllegalArgumentException{
|
|
if (this.clients != null) {
|
|
int index = -1;
|
|
for (int i = 0; i < this.clients.length; i++) {
|
|
if (clients[i].getPusher().equals(thread)) {
|
|
index = i;
|
|
}
|
|
}
|
|
if (index != -1) {
|
|
try {
|
|
this.clients[index].getOutputStream().close();
|
|
} catch (IOException ex) {
|
|
Logger.getLogger(ClientStore.class.getName()).log(Level.SEVERE, String.format("ClientStore: Fehler: %s", ex));
|
|
}
|
|
} else {
|
|
throw new IllegalArgumentException("Element not found");
|
|
}
|
|
} else {
|
|
throw new IllegalArgumentException("No Clients Present");
|
|
}
|
|
}
|
|
|
|
}
|