//
// Derived from the Hello example of OmniBroker 1.0
//

#include <OB/CORBA.h>
#include <OB/Util.h>

#include <loopconduit.h>

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_FSTREAM
#   include <fstream>
#else
#   include <fstream.h>
#endif

#include <pf/debug.h>


int main(int argc, char* argv[], char*[])
{
    try
    {
	//
	// Create ORB
	//
	CORBA_ORB_var orb = CORBA_ORB_init(argc, argv);
	
	//
	// Get loop object
	//
	const char* refFile = "Loop.ref";
        FILE *fp = fopen(refFile, "r");
        if (fp == 0)
	{
	    debugString("client: can't open `", refFile);
	    exit(-1);
	}
	
	char s[1000];
        fscanf(fp, "%s", s);
        fclose(fp);
	
	CORBA_Object_var obj = orb -> string_to_object(s);
	assert(!CORBA_is_nil(obj));
	
	loopConduit_var loop = loopConduit::_narrow(obj);
	assert(!CORBA_is_nil(loop));
	
	//
	// Main loop
	//
	cout << "Enter number for # of rounds or 'x' for exit:\n";
	char c[100];
        int n=0;
	do
	{
	    cout << "> ";
	    cin >> c;
	    if((n = atoi(c)) > 0)
            {
                cout << "Requesting " << n << " iteration";
                if (n > 1)
                {
                    cout << "s";
                }
                cout << "..." << endl;
		loop->inject(n);
            }
	}
	while(c[0] != 'x');
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
	OBPrintException(ex);
	return 1;
    }
    
    return 0;
}
