From 59883e829ab9edd149a391bc0bfd9149829c5b89 Mon Sep 17 00:00:00 2001 From: Brutus5000 Date: Sat, 30 Nov 2024 23:33:06 +0100 Subject: [PATCH] Use callbacks interface in RPCHandler --- .../faforever/iceadapter/FafRpcCallbacks.java | 13 ++++++++ .../com/faforever/iceadapter/IceAdapter.java | 31 +++++++++++-------- .../faforever/iceadapter/rpc/RPCHandler.java | 12 ++++--- .../faforever/iceadapter/rpc/RPCService.java | 8 ++--- 4 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 ice-adapter/src/main/java/com/faforever/iceadapter/FafRpcCallbacks.java diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/FafRpcCallbacks.java b/ice-adapter/src/main/java/com/faforever/iceadapter/FafRpcCallbacks.java new file mode 100644 index 0000000..259b7d8 --- /dev/null +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/FafRpcCallbacks.java @@ -0,0 +1,13 @@ +package com.faforever.iceadapter; + +public interface FafRpcCallbacks { + void onHostGame(String mapName); + + void onJoinGame(String remotePlayerLogin, int remotePlayerId); + + void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, boolean offer); + + void onDisconnectFromPeer(int remotePlayerId); + + void close(); +} 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 d5ca260..bae5ecd 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java @@ -23,7 +23,7 @@ usageHelpAutoWidth = true, description = "An ice (RFC 5245) based network bridge between FAF client and ForgedAlliance.exe") @Slf4j -public class IceAdapter implements Callable, AutoCloseable { +public class IceAdapter implements Callable, AutoCloseable, FafRpcCallbacks { private static IceAdapter INSTANCE; private static String VERSION = "SNAPSHOT"; private static volatile GameSession GAME_SESSION; @@ -59,19 +59,13 @@ public void start() { PeerIceModule.setForceRelay(iceOptions.isForceRelay()); GPGNetServer.init(iceOptions.getGpgnetPort(), iceOptions.getLobbyPort()); - RPCService.init(iceOptions.getRpcPort()); + RPCService.init(iceOptions.getRpcPort(), this); debug().startupComplete(); } @Override - public void close() { - executor.shutdown(); - CompletableFuture.runAsync( - executor::shutdownNow, CompletableFuture.delayedExecutor(250, TimeUnit.MILLISECONDS)); - } - - public static void onHostGame(String mapName) { + public void onHostGame(String mapName) { log.info("onHostGame"); createGameSession(); @@ -82,7 +76,8 @@ public static void onHostGame(String mapName) { }); } - public static void onJoinGame(String remotePlayerLogin, int remotePlayerId) { + @Override + public void onJoinGame(String remotePlayerLogin, int remotePlayerId) { log.info("onJoinGame {} {}", remotePlayerId, remotePlayerLogin); createGameSession(); int port = GAME_SESSION.connectToPeer(remotePlayerLogin, remotePlayerId, false, 0); @@ -94,7 +89,8 @@ public static void onJoinGame(String remotePlayerLogin, int remotePlayerId) { }); } - public static void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, boolean offer) { + @Override + public void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, boolean offer) { if (GPGNetServer.isConnected() && GPGNetServer.getGameState().isPresent() && (GPGNetServer.getGameState().get() == GameState.LAUNCHING @@ -113,7 +109,8 @@ public static void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, }); } - public static void onDisconnectFromPeer(int remotePlayerId) { + @Override + public void onDisconnectFromPeer(int remotePlayerId) { log.info("onDisconnectFromPeer {}", remotePlayerId); GAME_SESSION.disconnectFromPeer(remotePlayerId); @@ -150,6 +147,11 @@ public static void onFAShutdown() { }); } + @Override + public void close() { + this.close(0); + } + /** * Stop the ICE adapter */ @@ -162,8 +164,11 @@ public static void close(int status) { Debug.close(); TrayIcon.close(); INSTANCE.close(); + + INSTANCE.executor.shutdown(); CompletableFuture.runAsync( - () -> System.exit(status), CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS)); + INSTANCE.executor::shutdownNow, CompletableFuture.delayedExecutor(250, TimeUnit.MILLISECONDS)) + .thenRunAsync(() -> System.exit(status), CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS)); } public static int getId() { diff --git a/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCHandler.java b/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCHandler.java index 4ca5809..c7d86d9 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCHandler.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/rpc/RPCHandler.java @@ -1,5 +1,6 @@ package com.faforever.iceadapter.rpc; +import com.faforever.iceadapter.FafRpcCallbacks; import com.faforever.iceadapter.IceAdapter; import com.faforever.iceadapter.IceStatus; import com.faforever.iceadapter.gpgnet.GPGNetServer; @@ -35,21 +36,22 @@ public class RPCHandler { private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); private final Lock lockStatus = new ReentrantLock(); private final int rpcPort; + private final FafRpcCallbacks callbacks; public void hostGame(String mapName) { - IceAdapter.onHostGame(mapName); + callbacks.onHostGame(mapName); } public void joinGame(String remotePlayerLogin, long remotePlayerId) { - IceAdapter.onJoinGame(remotePlayerLogin, (int) remotePlayerId); + callbacks.onJoinGame(remotePlayerLogin, (int) remotePlayerId); } public void connectToPeer(String remotePlayerLogin, long remotePlayerId, boolean offer) { - IceAdapter.onConnectToPeer(remotePlayerLogin, (int) remotePlayerId, offer); + callbacks.onConnectToPeer(remotePlayerLogin, (int) remotePlayerId, offer); } public void disconnectFromPeer(long remotePlayerId) { - IceAdapter.onDisconnectFromPeer((int) remotePlayerId); + callbacks.onDisconnectFromPeer((int) remotePlayerId); } public void setLobbyInitMode(String lobbyInitMode) { @@ -169,6 +171,6 @@ public String status() { public void quit() { log.warn("Close requested, stopping..."); - IceAdapter.close(0); + callbacks.close(); } } 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 563c9d2..9458bd1 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 @@ -2,7 +2,7 @@ import static com.faforever.iceadapter.debug.Debug.debug; -import com.faforever.iceadapter.IceAdapter; +import com.faforever.iceadapter.FafRpcCallbacks; import com.faforever.iceadapter.debug.Debug; import com.faforever.iceadapter.debug.InfoWindow; import com.faforever.iceadapter.gpgnet.GPGNetServer; @@ -28,11 +28,11 @@ public class RPCService { private static TcpServer tcpServer; private static volatile boolean skipRPCMessages = false; - public static void init(int port) { + public static void init(int port, FafRpcCallbacks callbacks) { Debug.RPC_PORT = port; log.info("Creating RPC server on port {}", port); - RPCHandler rpcHandler = new RPCHandler(port); + RPCHandler rpcHandler = new RPCHandler(port, callbacks); tcpServer = new TcpServer(port, rpcHandler); tcpServer.start(); @@ -52,7 +52,7 @@ public static void init(int port) { log.info( "Lost connection to first RPC Peer. GameState: {}, Stopping adapter...", gameState.getName()); - IceAdapter.close(0); + callbacks.close(); } }); });