Skip to content

Commit

Permalink
Use callbacks interface in RPCHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Nov 30, 2024
1 parent 551a55f commit 88692f2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 12 additions & 7 deletions ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> {
public class IceAdapter implements Callable<Integer>, FafRpcCallbacks, AutoCloseable {
private static IceAdapter INSTANCE;
private static String VERSION = "SNAPSHOT";
private static volatile GameSession GAME_SESSION;
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -163,6 +165,6 @@ public String status() {

public void quit() {
log.warn("Close requested, stopping...");
IceAdapter.close();
callbacks.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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();
}
});
});
Expand Down

0 comments on commit 88692f2

Please sign in to comment.