package org.lilyproject.tools.printhost;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.lilyproject.cli.BaseCliTool;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;

public class PrintHostTool extends BaseCliTool {
    protected Option nameserverOption;

    @Override
    protected String getCmdName() {
        return "lily-print-host";
    }

    @Override
    public List<Option> getOptions() {
        List<Option> options = super.getOptions();

        nameserverOption = OptionBuilder
                .withArgName("nameserver")
                .hasArg()
                .withDescription("Non-default nameserver to use.")
                .withLongOpt("host")
                .create("n");

        options.add(nameserverOption);

        return options;
    }

    public static void main(String[] args) {
        new PrintHostTool().start(args);
    }

    @Override
    public int run(CommandLine cmd) throws Exception {
        int result = super.run(cmd);
        if (result != 0)
            return result;

        System.out.println("Below we print the detected host name and address.");
        System.out.println("These are used by Lily and Hadoop. For example, this is what is");
        System.out.println("published in ZooKeeper so that other nodes or clients can");
        System.out.println("connect to this node.");
        System.out.println();
        System.out.println("If this shows localhost and 127.0.0.1, adjust your system setup.");
        System.out.println();

        String nameserver = "default";
        if (cmd.hasOption(nameserverOption.getOpt())) {
            nameserver = cmd.getOptionValue(nameserverOption.getOpt());
        }

        System.out.println("Using nameserver: " + nameserver);

        String cn = InetAddress.getLocalHost().getCanonicalHostName();
        System.out.println("Canonical host name: " + cn);

        InetSocketAddress ad = new InetSocketAddress(cn, 1234);
        System.out.println("Address of the canonical host name: " + ad.getAddress().getHostAddress());

        return 0;
    }
}

@@ -2155,24 +2155,711 @@
 ddress());%0A%0A
+        System.out.println();%0A        System.out.println(%22If you use the hostname %5C%22%22 + cn + %22%5C%22 in your ZooKeeper connection%22);%0A        System.out.println(%22string, it is interesting to know that what ZooKeeper actually does%22);%0A        System.out.println(%22is retrieving all IP addresses of all hosts listed in the ZK connection%22);%0A        System.out.println(%22string, and then it picks one out of these to connect to.%22);%0A        System.out.println(%22These are all the IP-addresses coupled to %22 + cn + %22:%22);%0A        InetAddress addrs%5B%5D = InetAddress.getAllByName(cn);%0A        for (InetAddress addr : addrs) %7B%0A            System.out.println(%22  %22 + addr.getHostAddress());%0A        %7D%0A        %0A
         retu

print host name tool: also print all the ip addresses coupled to the hostname, since this is what ZooKeeper uses.