package client;

// Client/Client.java class represents the client that has to relay on the Hello engine
// to perform tasks remotely.
// it needs the Hello interface from Hello package
import compute.Compute;

// it also needs those to access the registry,
// so it can get remote object reference of the server
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client {
    private Client() {
    }

    public static void main(String[] args) {

        // normally, the address of the server is passed here on the command line as an
        // argument
        // we get that address from args[0] if provided, else we use null for localhost
        String host = (args.length < 1) ? null : args[0];

        try (Scanner scanner = new Scanner(System.in);) {

            // get the registry from host(server)
            Registry registry = LocateRegistry.getRegistry(host);

            // look up the remote object by name in the registry
            // stub is the a reference to the remote object
            Compute stub = (Compute) registry.lookup(Compute.SERVICE_NAME);
            boolean isAuthenticated = stub.authenticate();
            if (isAuthenticated) {
                System.out.println("Client authenticated successfully. Door opens.");
            } else {
                System.out.println("Client authentication failed. Door remains closed.");
            }

        } catch (Exception e) {
            System.err.println("Client exception: " + e.getMessage());
        }
    }
}
