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 Dec 6, 2024
1 parent 88fa8e2 commit 2d39c88
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 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();
}
31 changes: 18 additions & 13 deletions ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer>, AutoCloseable {
public class IceAdapter implements Callable<Integer>, AutoCloseable, FafRpcCallbacks {
private static IceAdapter INSTANCE;
private static String VERSION = "SNAPSHOT";
private static volatile GameSession GAME_SESSION;
Expand Down Expand Up @@ -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();

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

Expand Down Expand Up @@ -150,6 +147,11 @@ public static void onFAShutdown() {
});
}

@Override
public void close() {
this.close(0);
}

/**
* Stop the ICE adapter
*/
Expand All @@ -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(250, TimeUnit.MILLISECONDS));
}

public static int getId() {
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 @@ -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) {
Expand Down Expand Up @@ -169,6 +171,6 @@ public String status() {

public void quit() {
log.warn("Close requested, stopping...");
IceAdapter.close(0);
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(0);
callbacks.close();
}
});
});
Expand Down

0 comments on commit 2d39c88

Please sign in to comment.