#include #include "CipresCommlib/CipresSimpleServer.h" #include "CipresCommlib/CipresFacilitator.h" #include "CipresCommlib/poautil.h" using std::cerr; using std::endl; using std::string; /// called if there are multiple services to be launched in the same command int CipresSimpleServer::usage() const { cerr << name << " usage: required arguments are " << endl; cerr << " '-ns ' where servicename is {"; MapServantFactoryFunc::const_iterator sffIt = servantFactoryFunctions.begin(); cerr << sffIt->first; for (++sffIt; sffIt != servantFactoryFunctions.end(); ++sffIt) cerr << ", " << sffIt->first; cerr << '}'<< endl; cerr << " 'or -CipresImpl' which is the same as -ns" << endl; cerr << "Optional arguments are:" << endl; cerr << " '-CipresIOR ' write ior to file" << endl; cerr << " '-CipresNoNsReg' don't register with name service" << endl; cerr << " '-CipresNoNSLookup' don't look for objects in name service" << endl; cerr << " '<-ORBxxx>'any ORB initialization arguments." << endl; return 1; } void CipresSimpleServer::addCallback(const char * serviceName, CipresSimpleServer::ServantFactoryFunc callback) { // perhaps we should do some checking of serviceName ? servantFactoryFunctions[string(serviceName)] = callback; } CipresSimpleServer::MainReturnCode CipresSimpleServer::initializeAndRun(int argc, char *argv[]) { CipresFacilitator * facilitator = NULL; MainReturnCode rc = NO_PROBLEM; try { name = string(argv[0]); facilitator = CipresFacilitator::getSingletonPtr(); if (! facilitator) { cerr << "Could not access the Cipres' TheFacilitator instance" << endl; return NO_FACILITATOR_ERROR; } facilitator->initialize(argc, argv); const char * implNameCString = facilitator->getImplName(); const string serviceName = (implNameCString ? string(implNameCString) : string() ); MapServantFactoryFunc::const_iterator factoryIt = servantFactoryFunctions.find(serviceName); if (factoryIt == servantFactoryFunctions.end()) { cerr << "Service name \""<< serviceName <<"\" not recognized" << endl; usage(); throw(1); } const ServantFactoryFunc factoryFunction = factoryIt->second; // Get the RootPOA and manager CORBA::ORB_var orb = CORBA::ORB::_duplicate(facilitator->getORB()); CORBA::Object_var poa_object = orb->resolve_initial_references ("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow (poa_object.in ()); PortableServer::POAManager_var poa_manager = poa->the_POAManager (); string poaName = name; poaName.append("POA"); PortableServer::POA_var poaVar; poaVar = POAUtil::create_basic_POA(poa.in(), poa_manager.in(), poaName.c_str(), isMultithreaded, isPersistent); poa_manager->activate(); # if defined(CIPRES_USING_IORTABLE) && CIPRES_USING_IORTABLE CORBA::Object_var iortable_obj = orb->resolve_initial_references("IORTable"); IORTable::Table_var iortable = IORTable::Table::_narrow(iortable_obj.in()); # endif PortableServer::ServantBase_var servantVar; string servantName; try { NamedServantPtr namedServant = factoryFunction(serviceName.c_str()); servantVar = namedServant.second; servantName = namedServant.first; } catch (...) { cerr << serviceName << " servant creation failed"; return SERVANT_CREATION_FAILED; } CORBA::Object_var object = POAUtil::activate_servant( orb.in(), poaVar.in(), # if defined(CIPRES_USING_IORTABLE) && CIPRES_USING_IORTABLE iortable.in(), # endif servantVar.in(), name.c_str(), name.c_str()); facilitator->publishIOR(object.in(), serviceName.c_str()); cerr << "Running server..." << endl; facilitator->run(); } catch (CORBA::Exception &) { cerr << "CORBA exception raised!" << endl; rc = CORBA_ERROR; } catch (...) { cerr << "Terminating as the result of an unkown exception caught by cipresServerMainFunc" << endl; rc = UNKOWN_EXCEPTION_ERROR; } return rc; }