#include "MathImpl.h"
#include <fstream.h>

int main(int argc, char* const* argv) 
{
    try {

	//
	// The following is not application specific
	//

	// Initialize the ORB.
	CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

	// get a reference to the root POA
	CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
	PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
    
	// Get and activate the POA Manager
	PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager();
	poa_manager->activate();

	//
	// Create and activate servant
	//

	// Create the servant 
	MathImpl mathServant;
	Math_var math = mathServant._this();

	// Write reference to file
	CORBA::String_var string_ref = orb->object_to_string(math);
	ofstream refFile("ior.dat");
	refFile << string_ref << endl;
	refFile.close();

	cout << "Math server is ready" << endl; 

	// Wait for incoming requests
	orb->run();

    } catch(const CORBA::Exception& e) {
	cerr << e << endl;
	return 1;
    }
    return 0;
}
