Chat-Applikation/src/server/ClientMessageStore.java

62 lines
1.7 KiB
Java
Raw Normal View History

/*
* 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 utils.FifoPipe;
import utils.ArrayHelper;
import utils.Message;
/**
*
* @author eichehome
*/
public class ClientMessageStore {
private ArrayHelper<Thread> arrayHelper = new ArrayHelper<>();
/**
* Verzeichniss der Threads, die die Clients überwachen
*/
private Thread[] 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 addThread(Thread thread) {
arrayHelper.pushThread(thread, clientThreads);
}
public synchronized Thread removeThread(Thread thread) {
return arrayHelper.popThread(thread, clientThreads);
}
public void notifyAllClients() {
for (int i = 0; i < clientThreads.length; i++) {
clientThreads[i].interrupt();
}
}
}