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 5, 2024
1 parent 1f672ed commit 3e2eace
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 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();
}
32 changes: 18 additions & 14 deletions ice-adapter/src/main/java/com/faforever/iceadapter/IceAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,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 @@ -83,7 +77,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 @@ -95,7 +90,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 @@ -114,7 +110,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 @@ -151,10 +148,15 @@ public static void onFAShutdown() {
});
}

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

/**
* Stop the ICE adapter
*/
public static void close(int status) {
public void close(int status) {
log.info("close() - stopping the adapter. Status: {}", status);

onFAShutdown(); // will close gameSession aswell
Expand All @@ -163,8 +165,10 @@ public static void close(int status) {
Debug.close();
TrayIcon.close();
INSTANCE.close();
CompletableFuture.runAsync(
() -> System.exit(status), CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS));

executor.shutdown();
CompletableFuture.runAsync(executor::shutdownNow, CompletableFuture.delayedExecutor(250, TimeUnit.MILLISECONDS))
.thenRunAsync(() -> System.exit(status), CompletableFuture.delayedExecutor(500, 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 3e2eace

Please sign in to comment.