refactor: deleted MessageRelay class

This commit is contained in:
Yannik Bretschneider 2021-06-06 17:43:29 +02:00
parent cc45c821b7
commit a6ecf6fa30

View File

@ -1,70 +0,0 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.java_websocket.WebSocket;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.Tuple;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Relay for the messages field. It basically relays {@link Event} Events and
* {@link uulm.teamname.marvelous.gamelibrary.requests.Request} Requests to
* the participants or lobbies that they are supposed to be sent to.
*/
public class MessageRelay {
public static MessageRelay instance;
private final BlockingQueue<Tuple<Participant, Event[]>> eventsToSend;
private MessageRelay() {
this.eventsToSend = new LinkedBlockingQueue<>();
}
public static MessageRelay getInstance() {
if (instance == null) {
instance = new MessageRelay();
}
return instance;
}
public void relayMessage (WebSocket conn, LobbyConnection targetLobby) {
// TODO: Parse JSON
// TODO: send to target lobby
}
public void sendMessage (LobbyConnection origin, Participant recipient, Event[] events) {
if (!origin.contains(recipient)) {
Logger.warn("Lobby sent message to not-in-lobby recipient '{}'", recipient.name);
} else {
eventsToSend.add(Tuple.of(recipient, events));
}
}
/**
* Broadcasts events to all {@link Participant Participants} of a {@link LobbyConnection}.
* @param origin is the {@link LobbyConnection} that the {@link Event Events} are broadcasted from
* @param events are the {@link Event Events} to be broadcasted
*/
public void broadcastEvents (LobbyConnection origin, Event[] events) {
eventsToSend.add(Tuple.of(origin.getPlayer1(), events));
eventsToSend.add(Tuple.of(origin.getPlayer2(), events));
origin.getSpectators()
.forEach(spectator -> eventsToSend.add(Tuple.of(spectator, events)));
}
/**
* Terminates a given {@link LobbyConnection}. This is a End-of-Lifecycle method for
* {@link LobbyConnection LobbyConnections}, and they may not be reused again.
* @param origin is the LobbyConnection to be terminated
*/
public void terminate (LobbyConnection origin) {
// TODO: implement
}
}