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

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

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

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


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 = "SSCOP.ref";
        FILE *fp = fopen(refFile, "r");
        if (fp == 0)
	{
	    cerr << argv[0] << ": can't open `" << refFile << "': "
		 << strerror(errno) << endl;
	    return 1;
	}
	
	char s[1000];
        fscanf(fp, "%s", s);
        fclose(fp);
	
	CORBA_Object_var obj = orb -> string_to_object(s);
	assert(!CORBA_is_nil(obj));
	
	aaAdapter_var adapter = aaAdapter::_narrow(obj);
	assert(!CORBA_is_nil(adapter));
	
	//
	// Main loop
	//
	char c[100];
        char i[100];
        int n=0;
        int t=0;
	do
	{
	    cout << "# of packets to transmit or 'x' to exit > ";
	    cin >> c;
	    if((n = atoi(c)) > 0)
            {
                cout << "Interval between packets in msec > ";
                cin >> i;
                t = atoi(i);
                cout << "Requesting " << n << " packet";
                if (n > 1)
                {
                    cout << "s";
                }
                cout << " with " << t << " msec interval" << endl;
		adapter->inject(n, t);
            }
	}
	while(c[0] != 'x');
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
	OBPrintException(ex);
	return 1;
    }
    
    return 0;
}
