From 38c107439e1abbcfbba8d2af2dfe395fed5d75ae Mon Sep 17 00:00:00 2001 From: Brutus5000 Date: Mon, 9 Dec 2024 21:27:42 +0100 Subject: [PATCH] De-static RPCService --- .../com/faforever/iceadapter/IceAdapter.java | 12 ++++++--- .../iceadapter/gpgnet/GPGNetServer.java | 10 ++++--- .../iceadapter/ice/PeerIceModule.java | 12 ++++++--- .../faforever/iceadapter/rpc/RPCService.java | 26 ++++++++----------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java b/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java index 01adf97..953111f 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java @@ -32,6 +32,7 @@ public class IceAdapter implements Callable, AutoCloseable, FafRpcCallb private IceOptions iceOptions; private GPGNetServer gpgNetServer; + private RPCService rpcService; private final ExecutorService executor = ExecutorHolder.getExecutor(); private static final Lock lockGameSession = new ReentrantLock(); @@ -61,8 +62,11 @@ public void start() { PeerIceModule.setForceRelay(iceOptions.isForceRelay()); gpgNetServer = new GPGNetServer(); - gpgNetServer.init(iceOptions.getGpgnetPort(), iceOptions.getLobbyPort()); - RPCService.init(iceOptions.getRpcPort(), this); + gpgNetServer.init(iceOptions.getGpgnetPort(), iceOptions.getLobbyPort(), rpcService); + rpcService.init(iceOptions.getRpcPort(), gpgNetServer, this); + + PeerIceModule.setForceRelay(iceOptions.isForceRelay()); + PeerIceModule.setRpcService(rpcService); debug().startupComplete(); } @@ -88,7 +92,7 @@ public void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, boolea if (gpgNetServer.isConnected() && gpgNetServer.getGameState().isPresent() && (gpgNetServer.getGameState().get() == GameState.LAUNCHING - || GPGNetServer.getGameState().get() == GameState.ENDED)) { + || gpgNetServer.getGameState().get() == GameState.ENDED)) { log.warn("Game ended or in progress, ABORTING connectToPeer"); return; } @@ -146,7 +150,7 @@ public static void close(int status) { onFAShutdown(); // will close gameSession aswell INSTANCE.gpgNetServer.close(); - RPCService.close(); + INSTANCE.rpcService.close(); Debug.close(); TrayIcon.close(); diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/gpgnet/GPGNetServer.java b/ice-adapter/src/main/java/com/faforever/iceadapter/gpgnet/GPGNetServer.java index 4895565..f10036f 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/gpgnet/GPGNetServer.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/gpgnet/GPGNetServer.java @@ -28,6 +28,7 @@ public class GPGNetServer implements AutoCloseable { private int gpgnetPort; private int lobbyPort; + private RPCService rpcService; private ServerSocket serverSocket; private volatile GPGNetClient currentClient; @@ -49,8 +50,9 @@ public static void setLobbyInitMode(LobbyInitMode mode) { INSTANCE.lobbyInitMode = mode; } - public void init(int gpgnetPort, int lobbyPort) { + public void init(int gpgnetPort, int lobbyPort, RPCService rpcService) { INSTANCE = this; + this.rpcService = rpcService; if (gpgnetPort == 0) { this.gpgnetPort = NetworkToolbox.findFreeTCPPort(20000, 65536); @@ -103,7 +105,7 @@ private GPGNetClient(Socket socket) { } listenerThread = Thread.startVirtualThread(this::listenerThread); - RPCService.onConnectionStateChanged("Connected"); + rpcService.onConnectionStateChanged("Connected"); log.info("GPGNetClient has connected"); } @@ -145,7 +147,7 @@ private void processGpgnetMessage(String command, List args) { "Received GPGNet message: {} {}", command, args.stream().map(Object::toString).collect(Collectors.joining(" "))); - RPCService.onGpgNetMessageReceived(command, args); + rpcService.onGpgNetMessageReceived(command, args); } /** @@ -227,7 +229,7 @@ private void onGpgnetConnectionLost() { clientFuture = new CompletableFuture<>(); } - RPCService.onConnectionStateChanged("Disconnected"); + rpcService.onConnectionStateChanged("Disconnected"); IceAdapter.onFAShutdown(); } diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/ice/PeerIceModule.java b/ice-adapter/src/main/java/com/faforever/iceadapter/ice/PeerIceModule.java index 2397711..4a0e33f 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/ice/PeerIceModule.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/ice/PeerIceModule.java @@ -18,6 +18,7 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import lombok.Getter; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.ice4j.TransportAddress; import org.ice4j.ice.*; @@ -28,6 +29,9 @@ @Getter @Slf4j public class PeerIceModule { + @Setter + private static RPCService rpcService; + private static boolean ALLOW_HOST = true; private static boolean ALLOW_REFLEXIVE = true; private static boolean ALLOW_RELAY = true; @@ -85,7 +89,7 @@ public PeerIceModule(Peer peer) { */ private void setState(IceState newState) { this.iceState = newState; - RPCService.onIceConnectionStateChanged(IceAdapter.getId(), peer.getRemoteId(), iceState.getMessage()); + rpcService.onIceConnectionStateChanged(IceAdapter.getId(), peer.getRemoteId(), iceState.getMessage()); debug().peerStateChanged(this.peer); } @@ -200,7 +204,7 @@ a, new LongTermCredential(iceServer.getTurnUsername(), iceServer.getTurnCredenti .map(it -> it.type().toString() + "(" + it.protocol() + ")") .collect(Collectors.joining(", "))); setState(AWAITING_CANDIDATES); - RPCService.onIceMsg(localCandidatesMessage); + rpcService.onIceMsg(localCandidatesMessage); // Make sure to abort the connection process and reinitiate when we haven't received an answer to our offer in 6 // seconds, candidate packet was probably lost @@ -358,7 +362,7 @@ private void startIce() { // We are connected connected = true; - RPCService.onConnected(IceAdapter.getId(), peer.getRemoteId(), true); + rpcService.onConnected(IceAdapter.getId(), peer.getRemoteId(), true); setState(CONNECTED); if (component.getSelectedPair().getLocalCandidate().getType() == CandidateType.RELAYED_CANDIDATE) { @@ -403,7 +407,7 @@ public void onConnectionLost() { if (connected) { connected = false; log.warn("{} ICE connection has been lost for peer", getLogPrefix()); - RPCService.onConnected(IceAdapter.getId(), peer.getRemoteId(), false); + rpcService.onConnected(IceAdapter.getId(), peer.getRemoteId(), false); } setState(DISCONNECTED); diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCService.java b/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCService.java index 9458bd1..6931fbd 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCService.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCService.java @@ -15,20 +15,19 @@ import com.nbarraille.jjsonrpc.TcpServer; import java.util.Arrays; import java.util.List; -import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; /** * Handles communication between client and adapter, opens a server for the client to connect to */ @Slf4j -public class RPCService { +public class RPCService implements AutoCloseable { private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); private static TcpServer tcpServer; private static volatile boolean skipRPCMessages = false; - public static void init(int port, FafRpcCallbacks callbacks) { + public void init(int port, GPGNetServer gpgNetServer, FafRpcCallbacks callbacks) { Debug.RPC_PORT = port; log.info("Creating RPC server on port {}", port); @@ -39,7 +38,7 @@ public static void init(int port, FafRpcCallbacks callbacks) { debug().rpcStarted(tcpServer.getFirstPeer()); tcpServer.getFirstPeer().thenAccept(firstPeer -> { firstPeer.onConnectionLost(() -> { - GameState gameState = GPGNetServer.getGameState().orElse(null); + GameState gameState = gpgNetServer.getGameState().orElse(null); if (gameState == GameState.LAUNCHING) { skipRPCMessages = true; log.warn("Lost connection to first RPC Peer. GameState: LAUNCHING, NOT STOPPING!"); @@ -58,19 +57,19 @@ public static void init(int port, FafRpcCallbacks callbacks) { }); } - public static void onConnectionStateChanged(String newState) { + public void onConnectionStateChanged(String newState) { if (!skipRPCMessages) { getPeerOrWait().sendNotification("onConnectionStateChanged", Arrays.asList(newState)); } } - public static void onGpgNetMessageReceived(String header, List chunks) { + public void onGpgNetMessageReceived(String header, List chunks) { if (!skipRPCMessages) { getPeerOrWait().sendNotification("onGpgNetMessageReceived", Arrays.asList(header, chunks)); } } - public static void onIceMsg(CandidatesMessage candidatesMessage) { + public void onIceMsg(CandidatesMessage candidatesMessage) { if (!skipRPCMessages) { try { getPeerOrWait() @@ -86,7 +85,7 @@ public static void onIceMsg(CandidatesMessage candidatesMessage) { } } - public static void onIceConnectionStateChanged(long localPlayerId, long remotePlayerId, String state) { + public void onIceConnectionStateChanged(long localPlayerId, long remotePlayerId, String state) { if (!skipRPCMessages) { getPeerOrWait() .sendNotification( @@ -94,7 +93,7 @@ public static void onIceConnectionStateChanged(long localPlayerId, long remotePl } } - public static void onConnected(long localPlayerId, long remotePlayerId, boolean connected) { + public void onConnected(long localPlayerId, long remotePlayerId, boolean connected) { if (!skipRPCMessages) { getPeerOrWait().sendNotification("onConnected", Arrays.asList(localPlayerId, remotePlayerId, connected)); } @@ -105,7 +104,7 @@ public static void onConnected(long localPlayerId, long remotePlayerId, boolean * * @return the currently connected peer (the client) */ - public static JJsonPeer getPeerOrWait() { + public JJsonPeer getPeerOrWait() { try { return tcpServer.getFirstPeer().get(); } catch (Exception e) { @@ -114,11 +113,8 @@ public static JJsonPeer getPeerOrWait() { return null; } - public static CompletableFuture getPeerFuture() { - return tcpServer.getFirstPeer(); - } - - public static void close() { + @Override + public void close() { tcpServer.stop(); } }