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 6cfd090..c9ebe7e 100644 --- a/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java +++ b/ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java @@ -20,7 +20,7 @@ usageHelpAutoWidth = true, description = "An ice (RFC 5245) based network bridge between FAF client and ForgedAlliance.exe") @Slf4j -public class IceAdapter implements Callable { +public class IceAdapter implements Callable, FafRpcCallbacks, AutoCloseable { private static IceAdapter INSTANCE; private static String VERSION = "SNAPSHOT"; private static volatile GameSession GAME_SESSION; @@ -55,12 +55,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(); } - public static void onHostGame(String mapName) { + @Override + public void onHostGame(String mapName) { log.info("onHostGame"); createGameSession(); @@ -71,7 +72,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); @@ -83,7 +85,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 @@ -102,7 +105,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); @@ -138,7 +142,8 @@ public static synchronized void onFAShutdown() { /** * Stop the ICE adapter */ - public static void close() { + @Override + public void close() { log.info("close() - stopping the adapter"); Executor.executeDelayed(500, () -> System.exit(0)); 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 a712c98..7f951e0 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; @@ -32,21 +33,22 @@ public class RPCHandler { private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); 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) { @@ -163,6 +165,6 @@ public String status() { public void quit() { log.warn("Close requested, stopping..."); - IceAdapter.close(); + 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 c9200e9..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(); + callbacks.close(); } }); });