Search in sources :

Example 1 with TcpClient

use of com.nbarraille.jjsonrpc.TcpClient in project downlords-faf-client by FAForever.

the class IceAdapterImpl method start.

@Override
public CompletableFuture<Integer> start() {
    iceAdapterClientFuture = new CompletableFuture<>();
    Thread thread = new Thread(() -> {
        String nativeDir = System.getProperty("nativeDir", "lib");
        int adapterPort = SocketUtils.findAvailableTcpPort();
        int gpgPort = SocketUtils.findAvailableTcpPort();
        Player currentPlayer = playerService.getCurrentPlayer().orElseThrow(() -> new IllegalStateException("Player has not been set"));
        Path workDirectory = Paths.get(nativeDir);
        String[] cmd = new String[] { // FIXME make linux compatible
        workDirectory.resolve("faf-ice-adapter.exe").toString(), "--id", String.valueOf(currentPlayer.getId()), "--login", currentPlayer.getUsername(), "--rpc-port", String.valueOf(adapterPort), "--gpgnet-port", String.valueOf(gpgPort) };
        try {
            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.directory(workDirectory.toFile());
            processBuilder.command(cmd);
            log.debug("Starting ICE adapter with command: {}", asList(cmd));
            process = processBuilder.start();
            Logger logger = LoggerFactory.getLogger("faf-ice-adapter");
            gobbleLines(process.getInputStream(), logger::debug);
            gobbleLines(process.getErrorStream(), logger::error);
            IceAdapterCallbacks iceAdapterCallbacks = applicationContext.getBean(IceAdapterCallbacks.class);
            for (int attempt = 0; attempt < CONNECTION_ATTEMPTS; attempt++) {
                try {
                    TcpClient tcpClient = new TcpClient("localhost", adapterPort, iceAdapterCallbacks);
                    peer = tcpClient.getPeer();
                    setIceServers();
                    setLobbyInitMode();
                    break;
                } catch (ConnectException e) {
                    logger.debug("Could not connect to ICE adapter (attempt {}/{})", attempt, CONNECTION_ATTEMPTS);
                }
            }
            iceAdapterClientFuture.complete(gpgPort);
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                logger.debug("ICE adapter terminated normally");
            } else {
                logger.warn("ICE adapter terminated with exit code: {}", exitCode);
            }
        } catch (Exception e) {
            iceAdapterClientFuture.completeExceptionally(e);
        }
    });
    thread.setDaemon(true);
    thread.start();
    return iceAdapterClientFuture;
}
Also used : Path(java.nio.file.Path) Player(com.faforever.client.player.Player) Logger(org.slf4j.Logger) ConnectException(java.net.ConnectException) TcpClient(com.nbarraille.jjsonrpc.TcpClient) ConnectException(java.net.ConnectException)

Aggregations

Player (com.faforever.client.player.Player)1 TcpClient (com.nbarraille.jjsonrpc.TcpClient)1 ConnectException (java.net.ConnectException)1 Path (java.nio.file.Path)1 Logger (org.slf4j.Logger)1