/*       Mark set
 * class: DomHubOsci
 *
 * Version $Id: DomHubOsci.txt,v 1.2 2004/07/14 01:39:52 atlee Exp $
 *
 * Date: May 30 2003
 *
 * (c) 2003 IceCube Collaboration
 */

package icecube.testdaq.tools;


import icecube.testdaq.domrawrecord.DomRawRecord;
import icecube.testdaq.domrawrecord.ObjectFactory;

import javax.xml.bind.JAXBException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;

/**
 * This class ...does what?
 *
 * @version $Id: DomHubOsci.txt,v 1.2 2004/07/14 01:39:52 atlee Exp $
 * @author mka
 */
public class DomHubOsci {

    private static SocketChannel socketChannel;
    private static DomRawRecord domRawRecord;

    // public static final member data

    // protected static final member data

    // static final member data

    // private static final member data

    // private static member data

    // private instance member data

    // constructors

    /**
     * Create an instance of this class.
     * Default constructor is declared, but private, to stop accidental
     * creation of an instance of the class.
     */
    private DomHubOsci() {
    }

    // instance member method (alphabetic)

    // static member methods (alphabetic)

    // Description of this object.
    // public String toString() {}

    /**
     * Main to connect to a port of a DomHub and read fresh data from there;
     * Then a second thread displays the data record by record.
     *
     * @param args Two args: hostname and prot number.
     */
    public static void main( String args[] ) {

        // Check the command line:
        if ( args.length >= 2 ) {

            if ( tryToConnect( args ) < 0 ) {

                System.out.println( "Could not connect to specified host." );
                System.exit( -1 );
            }

        } else {

            System.out.println( "Usage: call this viewing program with the " +
                    "name of a DomHub host computer as first and the port " +
                    "number as second argument. " );
            System.exit( -1 );
        }


        //Create the misc. stuff:
        try {
            domRawRecord = ObjectFactory.createDomRawRecord();
        } catch ( JAXBException jaxbE ) {

            System.out.println( " Could not create a new DomRawRecord...." );
            jaxbE.printStackTrace();
            System.exit( -2 );
        }


        // Create the bytebuffer to read to from the socket
        ByteBuffer byteBuffer = ByteBuffer.allocate(100000);

        // Create and start the thread that reads entries from the byte buffer
        // and puts them into the dom raw record and then calls the display
        // routines that the new entries get displayed.
        ByteBufferReader byteBufferReader = new ByteBufferReader( domRawRecord,
                byteBuffer);

    }

    private static int tryToConnect( String[] args ) {

        try {

            socketChannel = SocketChannel.open(new InetSocketAddress(
                    args[0], Integer.parseInt(args[1])));
            socketChannel.configureBlocking(true);
        } catch ( IOException e ) {

            System.out.println(e.getMessage());
            System.out.println( "Something is wrong with your socket." );
            System.out.println( "Host: " + args[0] + " Port: " + args[1] );
            e.printStackTrace();
            System.exit( -3 );
        } catch ( NumberFormatException e ) {

            System.out.println( "Something is wrong with your socket." );
            e.printStackTrace();
            System.exit( -4 );
        }

        return ( 0 );
    }
}
