76 lines
2.2 KiB
Java
76 lines
2.2 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.PipedWriter;
|
|
import utils.FifoPipe;
|
|
import utils.ArrayHelper;
|
|
import utils.Client;
|
|
import utils.Message;
|
|
|
|
/**
|
|
*
|
|
* @author eichehome
|
|
*/
|
|
public class ClientMessageStore {
|
|
|
|
private ArrayHelper<Client> arrayHelper = new ArrayHelper<>();
|
|
/**
|
|
* Verzeichniss der Threads, die die Clients überwachen
|
|
*/
|
|
private Client[] clientThreads = null;
|
|
/**
|
|
* Puffer der Nachrichten
|
|
*/
|
|
private FifoPipe messages = new FifoPipe();
|
|
|
|
public synchronized void pushMessage(Message message) {
|
|
messages.setElement(message);
|
|
}
|
|
|
|
public synchronized Message popMessage() {
|
|
Message result = messages.getNextElement();
|
|
return result;
|
|
}
|
|
|
|
public synchronized void addClient(Thread thread, PipedWriter pipe) {
|
|
Client client = new Client(thread, pipe);
|
|
arrayHelper.push(client, 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 (Client client : clientThreads) {
|
|
client.getThread().interrupt();
|
|
}
|
|
}
|
|
}
|