/**
 * This class is almost exactly the same as HelloMOBY, but uses the asynchronous call
 * to MobyRequest.invokeService().  This allows the main thread to print "hourglass" dots 
 * in the terminal while waiting for the response.
 */

import org.biomoby.client.*;
import org.biomoby.shared.*;
import org.biomoby.shared.data.*;

public class HelloMOBY2 implements MobyRequestEventHandler{
    public static void main(String[] args) throws Exception{

        Central worker = new CentralImpl();
        MobyService templateService = new MobyService("MOBYSHoundGetGenBankff");
        MobyService[] validServices = worker.findService(templateService);

	MobyRequest mr = new MobyRequest(worker);
	mr.setService(validServices[0]);
	mr.setInput(new MobyDataObject("NCBI_gi", "111076"));

        // HelloMOBY2 (ourselves) is a valid listener, must use "new" to 
        // have a non-static reference for callback
	mr.invokeService(new HelloMOBY2());  

        // Infinite loop broken only by exit in callback function
        while(true){
           System.err.print("."); // Print a dot every second (poor man's text hourglass) 
           try{Thread.sleep(100);}catch(InterruptedException ie){}
        }
    }

    // Ignoring start of request here, nothing for us to do
    // requestID is a unique ID for this invocation
    public void start(MobyRequestEvent mre){
	System.out.println("Data being sent is: ");
	System.out.println(mre.getContent().toString());
    }

    // Called by MobyRequest when the response is available
    public void processEvent(MobyRequestEvent mre){
        System.out.println("");  // Blank line to seperate response from hourglass dots
        System.out.println(mre.getContent().toString());
        System.exit(0);
    }

    // Ignoring end of request, nothing for us to do
    public void stop(MobyRequest request, int requestID){
    }
}
