package io.xpipe.app.beacon;

import io.xpipe.app.beacon.api.DaemonStopExchange;

import lombok.SneakyThrows;

import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class BeaconServer {

    @SneakyThrows
    public static boolean isReachable(int port) {
        var local = Inet4Address.getByAddress(new byte[] {0x7f, 0x00, 0x00, 0x01});

        try (var socket = new Socket()) {
            InetSocketAddress adress = new InetSocketAddress(local, port);
            socket.connect(adress, 5000);
        } catch (Exception e) {
            return false;
        }

        // If there's some kind of networking tool interfering with sockets by for example proxying socket connections
        // The previous connect might succeed even though nothing is running.
        // To be sure, check that the socket is indeed occupied
        try (var ignored = new ServerSocket(port, 0, local)) {
            return false;
        } catch (Exception e) {
            return true;
        }
    }

    @SuppressWarnings("unused")
    public static boolean tryStop(BeaconClient client) throws Exception {
        DaemonStopExchange.Response res =
                client.performRequest(DaemonStopExchange.Request.builder().build());
        return res.isSuccess();
    }
}
