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

#include <OB/CORBA.h>
#include <OB/Util.h>
#include <../../OB-1.0/naming/CosNaming.h>

#include "protocol/cc/inap.h"

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

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

#include "sf/orbscheduler.h"
#include "protocol/cc/ccinadapter.h"
#include "protocol/cc/ccmanagementadapter.h"
#include "pf/system.h"



/*

void saveReference(CORBA_ORB_ptr orb, inap_scpRequired_ptr p )
{
    //
	// Save reference
	//
	CORBA_String_var s = orb->object_to_string(p);
	const char* refFile = "ccadapter.ref";

        //
        // NOTE: use C FILEs rather than C++ file streams, because the
        // latter don't work with g++ when RTTI is in use
        //
        FILE *fp = fopen(refFile, "w");
        if(fp == 0)
	{
	    extern int errno;
	    cerr << ": can't open `" << refFile << "': "
		 << strerror(errno) << endl;
	    return;
	}
	
        fprintf(fp, "%s\n", s.in());
        fclose(fp);
}

void registerToNS(inap_scpRequired_ptr p)
{
    //
    // Get naming service
    //
    CORBA_Object_var obj;
    

    cout << "Get naming service" << endl;
    
    try
    {
	obj = sfORBScheduler::getORB()->
             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);
    }

    try
    {
	//
	// Create and bind some Naming Contexts
	//
        
	CosNaming_Name nc1Name;
	nc1Name.length(1);
	nc1Name[0].id = CORBA_string_dup("in");
	nc1Name[0].kind = CORBA_string_dup("");
        cout << "bind_new_context(in-)" << endl;
	CosNaming_NamingContext_var nc1 = nc -> bind_new_context(nc1Name);
    }
    catch(const CosNaming_NamingContext::AlreadyBound&)
    {
	cerr << ": Context 'in' already bound " << endl;
    }
    try
    {

        CosNaming_Name nc2Name;
	nc2Name.length(2);
	nc2Name[0].id = CORBA_string_dup("in");
	nc2Name[0].kind = CORBA_string_dup("");
	nc2Name[1].id = CORBA_string_dup("cc");
	nc2Name[1].kind = CORBA_string_dup("");
        cout << "bind_new_context(in-.cc-)" << endl;
	CosNaming_NamingContext_var nc2 = nc -> bind_new_context(nc2Name);
    }
    catch(const CosNaming_NamingContext::AlreadyBound&)
    {
	cerr << ": Context 'cc' already bound " << endl;
    }

    try
    {
	//
	// Bind names with the Naming Service
	//
        CosNaming_Name bName;
	bName.length(3);
	bName[0].id = CORBA_string_dup("in");
	bName[0].kind = CORBA_string_dup("");
	bName[1].id = CORBA_string_dup("cc");
	bName[1].kind = CORBA_string_dup("");
 	bName[2].id = CORBA_string_dup("instance");
	bName[2].kind = CORBA_string_dup("");
        cout << "bind(in-.cc-.instance-)" << endl;
	nc -> bind(bName, p);
       
    }
    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 (name) `AlreadyBound' exception" << endl;
	return;
    }
    catch(const CosNaming_NamingContext::NotEmpty&)
    {
	cerr << ": Got a `NotEmpty' exception" << endl;
	exit(1);
    }
}
*/

int main(int argc, char* argv[], char*[])
{
    //
    // pfSystem must be instantiated with the sfORBScheduler instance
    // as the parameter, otherwise it will use sfFIFOScheduler as its
    // default scheduler.
    //
    sfScheduler *scheduler = sfORBScheduler::instance();
    pfSystem::instance(scheduler);
    
    sfORBScheduler::initORB(argc, argv);
      
    pfConduit inAdapter = ccINadapter::createINadapter();
    pfConduit clone = ccINadapter::cloneINadapter();
    
    ccManagementAdapter::instance();

    cout << "Server is ready for network" << endl;
    //
    // Run implementation (DON'T call impl_is_ready!)
    //	
    pfSystem::instance()->run();
    

    return 0;
}




