//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / testing
//
//File: client.cpp
//
//Version: $Revision: 1.1 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/09/30 15:47:08 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author: Timo Pärnänen
//
//
//Description:
//
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <typeinfo>
#include <iostream.h>
#include <string>

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

#include "ORBacustest.h"

//
//Function: main
//

int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        cout << "\nUSAGE: " << argv[0] << " <interface> -ORBnaming `cat /PATH/FILE.ior`" << endl;
        cout << "e.g. " << argv[0] << " s1 -ORBnaming `cat /projects/IORs/Naming.ior`\n" 
             << endl;
        exit(0);
    }

    ORBacustest_var testImpl;
    
    try
    {
        CORBA_ORB_var orb = CORBA_ORB_init(argc, argv);
        
        //
        // Get naming service
        //
        CORBA_Object_var obj;
        
        try
        {
            obj = orb->resolve_initial_references("NameService");
        }
        catch(const CORBA_ORB::InvalidName&)
        {
            cerr << ": can't resolve `NameService'" << endl;
            exit(1);
        }
        
        if(CORBA_is_nil(obj))
        {
            cerr << ": `NameService' is a nil object reference" << endl;
            exit(1);
        }
        
        CosNaming_NamingContext_var nc = CosNaming_NamingContext::_narrow(obj);
        
        if(CORBA_is_nil(nc))
        {
            cerr << "`NameService' is not a NamingContext object reference" << endl;
            exit(1);
        }

        CosNaming_Name objName;
        
        try
        {
            objName.length(2);
            objName[0].id = CORBA_string_dup("ORBacustest");
            objName[0].kind = CORBA_string_dup("");
            objName[1].id = CORBA_string_dup(argv[1]);
            objName[1].kind = CORBA_string_dup("");

            CORBA_Object_var testObj = nc->resolve(objName);
            testImpl = ORBacustest::_narrow(testObj);
            assert(!CORBA_is_nil(testImpl));
        }
        catch(const CosNaming_NamingContext::NotFound& ex)
        {
            cerr << "Got a `NotFound' exception (";
            switch(ex.why)
            {
                case CosNaming_NamingContext::missing_node:
                    cerr << "missing node";
                    break;
                    
                case CosNaming_NamingContext::not_context:
                    cerr << "not context";
                    break;
                    
                case CosNaming_NamingContext::not_object:
                    cerr << "not object";
                    break;
            }
            cerr << ")" << endl;
            exit(1);
        }
        catch(const CosNaming_NamingContext::CannotProceed&)
        {
            cerr << "Got a `CannotProceed' exception" << endl;
            exit(1);
        }
        catch(const CosNaming_NamingContext::InvalidName&)
        {
            cerr << "Got an `InvalidName' exception" << endl;
            exit(1);
        }
        catch(const CosNaming_NamingContext::NotEmpty&)
        {
            cerr << "Got a `NotEmpty' exception" << endl;
            exit(1);
        }
    }
    catch(...)
    {
        cerr << "ORB initialization failed" << endl;
        exit(1);
    }

    try
    {
        int counter = 0;
        char c;
        cout << "x exits and t call test method from server" << endl;
        while (1)
        {
            cout << "input >";
            cin >> c;
            cout << endl;
            
            if (c == 't')
            {
                for (int i=0; i<200; i++)
                {
                    ++counter;
                    testImpl->test();
                }
                cout << "counter = " << counter << endl;
            }
            if (c == 'x')
            {
                break;
            }
        }
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
        catch(CORBA_SystemException& ex)
#endif
    {
        OBPrintException(ex);
        return 1;
    }
    
    return 0;
}
