
package client;

import java.util.Vector;
import java.util.Scanner;
import java.net.URL;
import java.net.URI;

/**
 * Client class for XML-RPC communication
 * 
 * @author Tinsae Ghilay
 *         from the example on
 *         https://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm
 */
// import the org.apache.xmlrpc package
// it contains classes for XML-RPC client (and server) implementation
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

// logging
import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

public class Client {

    private static String TAG = "Client";

    public static void main(String[] args) {

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

            // create an XML-RPC client object with address to server @ localhost
            // URL can be any valid URL. the default port for XML-RPC is 80
            // but here we use 8080 since 80 may need root privileges
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            // config.setServerURL(new URL("http://localhost:8080/RPC2"));
            config.setServerURL(new URI("http://localhost:8080/RPC2").toURL());
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);

            InputHandler handler = new InputHandler();

            while (true) {
                // create parameters to be sent to server method in a vector
                // and populate it
                Vector params = new Vector();

                // get two integers from user
                // promptForInt returns null if user wants to exit
                Integer a = handler.promptForInt(scanner);
                if (a == null) { // user wants to exit
                    notifyExit();
                    break;
                }
                Integer b = handler.promptForInt(scanner);
                if (b == null) { // user wants to exit
                    notifyExit();
                    break;
                }

                // get command from user
                String command = handler.promptForCommand(scanner);
                if (command.equals("exit")) { // user wants to exit
                    notifyExit();
                    break;
                }

                // add parameters to vector
                params.addElement(Integer.valueOf(a));
                params.addElement(Integer.valueOf(b));
                // make the remote method call
                // the result of the remot call must be an Object
                // and be casted to the appropriate type
                Object result = client.execute("calculator." + command, params);
                // process the result
                int sum = ((Integer) result).intValue();
                System.out.println("The " + handler.mapCommand(command) + " of " + a + " and " + b + " is: " + sum);

            }

        } catch (Exception e) { // catch exceptions if problems occur
            System.err.println(TAG + ": " + e.getMessage());
        }
    }

    private static void notifyExit() {
        System.out.println("Exit command received. Exiting...");
    }

}
