//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / testing
//
//File: server2.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_impl.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);
    }

    try
    {
        CORBA_ORB_var orb = CORBA_ORB_init(argc, argv);
        CORBA_BOA_var boa = orb->BOA_init(argc, argv);

        ORBacustest_var testImpl = new ORBacustestImpl();

        //
        // 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
        {
            //
            // Create and bind some Naming Contexts
            //
            CosNaming_Name nc1Name;
            nc1Name.length(1);
            nc1Name[0].id = CORBA_string_dup("ORBacustest");
            nc1Name[0].kind = CORBA_string_dup("");
            CosNaming_NamingContext_var nc1 = nc->bind_new_context(nc1Name);
            
            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("");
            nc -> bind(objName, testImpl);
            
            //
            // Run implementation
            //
            boa -> impl_is_ready(CORBA_ImplementationDef::_nil());
            
            //
            // Unregister names with the Naming Service
            //
            nc -> unbind(objName);
            nc -> unbind(nc1Name);
        }
        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::AlreadyBound&)
        {
            cerr << "Got an `AlreadyBound' 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);
    }
    
    return 0;
}
