package javakurs.musterloesung.uebung07;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

/**
 * {@link XmlRpcFridgeClient} is a test client for {@link XmlRpcFridgeServer}.
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: XmlRpcFridgeClient.java,v 1.1 2010/12/13 13:15:55 steffen Exp $
 */
public final class XmlRpcFridgeClient {

  /**
   * This would create a new instance of {@link XmlRpcFridgeClient}. Not to be
   * used.
   */
  private XmlRpcFridgeClient() {

    // private constructor to enforce noninstantiability
  }


  /**
   * This is the main method. It requires no arguments.
   *
   * @param args
   *          the arguments; not used here
   */
  public static void main(String[] args) {

    try {
      // init client
      XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
      config.setServerURL(new URL("http://127.0.0.1:16384/xmlrpc"));
      XmlRpcClient client = new XmlRpcClient();
      client.setConfig(config);

      // get the fridge inventory via XML RPC
      String inventory =
        (String)client.execute(
          "servedFridge.getInventoryAsString",
          new Object[] {});
      System.out.println(inventory);

      // get the weather via XML RPC
      String weatherReport =
        (String)client.execute(
          "servedFridge.getWeatherAsString",
          new Object[] {});
      System.out.println(weatherReport);

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (XmlRpcException e) {
      e.printStackTrace();
    }
  }
}
