/** \example ListMarkets.java */
/** \@code */

// A very simple program that just connects to our server to print the list of
// configured feeds and markets on the console. This is meant as a convenient
// utility, and as a way to demonstrate fetching the list of supported feeds and
// markets.

package com.marketfactory.sample_zerogc;

import com.marketfactory.zerogc.api.MFClient;
import com.marketfactory.zerogc.api.APIResult;
import com.marketfactory.zerogc.api.APIFeed;
import com.marketfactory.zerogc.api.APIMarket;
import com.marketfactory.zerogc.api.MFString;
import java.util.logging.Logger;
import java.io.IOException;


public class ListMarkets
{

    private static final String DEFAULT_HOSTNAME = "ny9.marketfactory.com";
    private static final int DEFAULT_PORT = 10033;

    private static final String USAGE_PARAMETERS = "username password [hostname] [port]";

    public static void main(String args[]) throws IOException, InterruptedException
    {
        // Parse command-line arguments, 1980's-style.
        if (args.length < 2) {
            throw new RuntimeException("Not enough parameters provided. Parameters: " + USAGE_PARAMETERS);
        }

        final String username = args[0];
        final String password = args[1];
        final String hostname = args.length >=3 ? args[2] : DEFAULT_HOSTNAME;
        final int port = args.length >=4 ? Integer.valueOf(args[3]) : DEFAULT_PORT;

        // Connect to our server (and automatically receive the list of feeds
        // and markets).
        MFClient client = new MFClient(new MFString(username), new MFString(password), new MFString(hostname), port);
        APIResult result = client.connect();
        if ( result != APIResult.OK ) {
            System.err.println("Could not connect: " + result);
            System.exit(1);
        }

        // List all the markets from the client object.
        for ( APIFeed feed : client.getFeeds() ) {
            System.out.printf("\n%s (%s)\n", feed.name, feed.feedID);
            for ( APIMarket mkt : feed.marketList ) {
                System.out.printf("   %-16s %8d %16s %16s\n",
                                  mkt.name, mkt.marketID, mkt.bigIncrement, mkt.minIncrement);
            }
        }
    }
}

/** \@endcode */
