// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <iostream>

using namespace std;

#include "test.h"
#include "testimpl.h"

using namespace CORBA;
using namespace CosNaming;
using namespace PortableServer;
using namespace omniORB;

using namespace corbatest::common;

static const char *NS = "NameService=corbaname::localhost:2809";
static const char *NAME = "TestC1";

int main(int argc, char *argv[])
{
    try
    {
        const char *options[][2] = { { "InitRef", NS }, { 0, 0 } };
        ORB_var orb = ORB_init(argc, argv, "omniORB4", options);
        BOA_var boa = orb->BOA_init(argc, argv, "omniORB4_BOA");
        TestImpl *srv = new TestImpl();
        srv->_obj_is_ready(boa);
        NamingContext_var nc = NamingContext::_narrow(orb->resolve_initial_references("NameService"));
        Name n;
        n.length(1);
        n[0].id   = (const char *)NAME;
        n[0].kind = (const char *)"";
        try
        {
            nc->bind(n, srv->_this());
        }
        catch(NamingContext::AlreadyBound& ex)
        {
            nc->rebind(n, srv->_this());
        }
        boa->impl_is_ready();
        //puts("Press enter to exit");
        //getchar();
        //nc->unbind(n);
        //boa->destroy();
        //orb->destroy();
    }
    catch(Exception& ex)
    {
         cerr << ex._name() << endl;
    }
    catch(fatalException& fe)
    {
        cerr << fe.errmsg() << endl;
    }
    catch(...) 
    {
        cerr << "Ooops" << endl;
    }
    return 0;
}
