From: caremoli Date: Mon, 24 Jan 2005 13:08:28 +0000 (+0000) Subject: CCAR: Update of start container modifications. X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2FBR_CCAR_V220a1;p=modules%2Fkernel.git CCAR: Update of start container modifications. Adding files forgotten in previous commit Adding C++ functionnality (wrap of python module) --- diff --git a/bin/envSalome.py b/bin/envSalome.py new file mode 100755 index 000000000..3eacc0c12 --- /dev/null +++ b/bin/envSalome.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +import os +import sys + +kernel_root=os.getenv("KERNEL_ROOT_DIR") +sys.path[:0]=[kernel_root+"/bin/salome"] + +import runSalome + +argv=sys.argv[1:] +sys.argv=[] + +args, modules_list, modules_root_dir = runSalome.get_config() +runSalome.set_env(args, modules_list, modules_root_dir) + +os.execvp(argv[0],argv) diff --git a/bin/salomeConsole.py b/bin/salomeConsole.py new file mode 100755 index 000000000..03c0a5e51 --- /dev/null +++ b/bin/salomeConsole.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +import os +import sys +#------------------------------- +# Python completion and others if you want +# You should have set PYTHONSTARTUP env variable +# or import user should try to import $HOME/.pythonrc.py +#------------------------------- +import user + +#------------------------------- +# Get major CORBA objects +#------------------------------- +import CORBA +import CosNaming +# There are cyclic dependencies between Engines, SALOME and SALOMEDS. +# import first Engines, then SALOME and then SALOMEDS +# Or use reload(Engines) to be safe. +import Engines +import SALOME +import SALOMEDS +import SALOME_ModuleCatalog +reload(Engines) +reload(SALOME) +reload(SALOMEDS) + +import LifeCycleCORBA +import orbmodule +from runSalome import * + +import Utils_Identity +files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),".omni*.cfg")) + +filename="" +if len(files)==1: + filename=files[0] +else: + print "You have %d sessions running" % len(files) + for f in files: + print "Session:",f + rep= raw_input("Do you want to connect to this session [y|n]") + if rep == "y": + filename=f + break + +if filename != "": + os.environ['OMNIORB_CONFIG']=filename +else: + rep= raw_input("Do you want to try a local session on port 2810 ? [y|n]") + if rep == "y": + # Try a local session running on port 2810 + sys.argv=sys.argv+['-ORBInitRef','NameService=corbaname::localhost:2810'] + else: + sys.exit(1) + +print sys.argv + +#direct adress from clt.orb.object_to_string(clt.rootContext) +#sys.argv=sys.argv+['-ORBInitRef','NameService=IOR:010000000100000000000000010000000000000023000000010100000a0000006c6f63616c686f737400fa0a0b0000004e616d6553657276696365'] + +class client(orbmodule.client): + def initNS(self): + # Obtain a reference to the root naming context + obj = self.orb.resolve_initial_references("NameService") + try: + self.rootContext = obj._narrow(CosNaming.NamingContext) + return + except CORBA.COMM_FAILURE: + print "It's not a valid naming service" + self.rootContext = None + raise + +clt=client() +print "Naming Service address: ",clt.orb.object_to_string(clt.rootContext) + +clt.showNS() + +session=clt.waitNS("/Kernel/Session") +catalog=clt.waitNS("/Kernel/ModulCatalog") +studyMgr=clt.waitNS("/myStudyManager") +lcc = LifeCycleCORBA.LifeCycleCORBA(clt.orb) + +#c=lcc.FindOrStartContainer("","totoPy") +#print c diff --git a/resources/KERNELCatalog.xml b/resources/KERNELCatalog.xml index c0d046504..1edacc08b 100644 --- a/resources/KERNELCatalog.xml +++ b/resources/KERNELCatalog.xml @@ -22,5 +22,25 @@ hostname = muna + + SalomeTestComponent + SalomeTestComponent + Other + NRI + 2.1.0 + GUI Neutral Context + 1 + 'linux' ~ OS + + + SALOME_TestComponentPy + SALOME_TestComponentPy + Other + NRI + 2.1.0 + GUI Neutral Context + 1 + 'linux' ~ OS + diff --git a/src/LifeCycleCORBA/Launchers.cxx b/src/LifeCycleCORBA/Launchers.cxx new file mode 100644 index 000000000..f3622465d --- /dev/null +++ b/src/LifeCycleCORBA/Launchers.cxx @@ -0,0 +1,79 @@ +#include + +static int Launchers_IsLoaded = 0; +static PyObject * Launchers_module = 0; +static char * Launchers_name="Launchers"; + +PyThreadState *mainThreadState=0; + +PyThreadState * getMainThreadState(){ + PyInterpreterState *interp; + PyThreadState *p; + interp=PyInterpreterState_Head(); + for (interp = PyInterpreterState_Head(); interp != NULL; ){ + if(interp->next == NULL)break; + interp=interp->next; + } + for (p = interp->tstate_head; p != NULL; ){ + if(p->next == NULL)break; + p=p->next; + } + return p; +} +PyThreadState *acquireMainThread(){ + PyEval_AcquireLock(); + return PyThreadState_Swap(mainThreadState); +} +void releaseMainThread(PyThreadState *tstate){ + PyThreadState_Swap(tstate); + PyEval_ReleaseLock(); +} + +void Launchers_assertInitialized() { + PyThreadState *_save; + if( !Py_IsInitialized() ) { + Py_Initialize(); + PyEval_InitThreads(); + PyEval_SaveThread(); + } + if( !Launchers_IsLoaded ) { + mainThreadState=getMainThreadState(); + _save=acquireMainThread(); + Launchers_module=PyImport_ImportModule(Launchers_name); + if(!Launchers_module){ + PyErr_Print(); + return; + } + Py_INCREF(Launchers_module); + Launchers_IsLoaded = 1; + releaseMainThread(_save); + } +} + +PyObject * getLauncher(char* arg0) { + PyObject *v; + PyThreadState *_save; + Launchers_assertInitialized(); + _save=acquireMainThread(); + v=PyObject_CallMethod(Launchers_module,"getLauncher","s",arg0); + if(!v){ + PyErr_Print(); + return NULL; + } + releaseMainThread(_save); + Py_INCREF(v); + return v; +} + +void Launcher_Slaunch(PyObject *self, char* arg1, char* arg2) { + PyObject *v; + PyThreadState *_save; + Launchers_assertInitialized(); + _save=acquireMainThread(); + v=PyObject_CallMethod(self,"Slaunch","ss",arg1,arg2); + if(!v){ + PyErr_Print(); + } + releaseMainThread(_save); +} + diff --git a/src/LifeCycleCORBA/Launchers.hxx b/src/LifeCycleCORBA/Launchers.hxx new file mode 100644 index 000000000..f402b7e51 --- /dev/null +++ b/src/LifeCycleCORBA/Launchers.hxx @@ -0,0 +1,6 @@ +#include +#define elDEBUG 0 + +PyObject * getLauncher(char* ); +void Launcher_Slaunch(PyObject * objId, char*, char* ); + diff --git a/src/LifeCycleCORBA/Launchers.py b/src/LifeCycleCORBA/Launchers.py new file mode 100644 index 000000000..0888b17b9 --- /dev/null +++ b/src/LifeCycleCORBA/Launchers.py @@ -0,0 +1,82 @@ +import os +import Utils_Identity +import ConfigParser + +class Config: + defaults={'user':'','opt':''} + def __init__(self): + self.config=ConfigParser.ConfigParser(self.defaults) + self.config.read(os.path.join(os.environ['HOME'],Utils_Identity.getapplipath(),'launchers.cfg')) + def computers(self): + return self.config.sections() + def options(self,computer): + if not self.config.has_section(computer): + return self.defaults + else: + d={} + for opt in self.config.options(computer): + d[opt]=self.config.get(computer,opt) + return d + +config=None +def setConfig(conf): + global config + config=conf + +setConfig(Config()) + +class Launcher: + def Slaunch(self,computer,CMD): + self.launch(computer,CMD.split()) + +class SSH(Launcher): + def launch(self,computer,CMD): + print computer,CMD + conf=config.options(computer) + print conf + command=["ssh",computer] + if conf["user"]: + command=command+['-l',conf["user"]] + if conf["opt"]: + command=command+string.split(conf["opt"]) + command=command+[os.path.join(Utils_Identity.getapplipath(),'runSession')]+CMD + command=command+['>', "/tmp/" + CMD[1] + "_" + computer + ".log 2>&1"] + + print command + pid = os.spawnvp(os.P_NOWAIT, command[0], command) + +launcher=None +_register={} + +def register(name,klass): + _register[name]=klass + +register("ssh",SSH()) + +def setLauncher(name): + global launcher + launcher=_register[name] + +def getLauncher(computer): + """ + Return a launcher for the computer + If can be specified in config + If not return the default one : launcher + """ + conf=config.options(computer) + name=conf.get("launcher","") + return _register.get(name,launcher) + +setLauncher("ssh") + +try: + # Try to import a plugin module (user defined) that + # can define other launchers and register them + # 1- define a class + # 2- register it with : Launchers.register(name,klass) + # 3- eventually set it as default one : Launchers.setLauncher(name) + import pluginLauncher +except: + import traceback + traceback.print_exc() + pass diff --git a/src/LifeCycleCORBA/LifeCycleCORBA.py b/src/LifeCycleCORBA/LifeCycleCORBA.py index c6106dcb2..79fa9cb7e 100644 --- a/src/LifeCycleCORBA/LifeCycleCORBA.py +++ b/src/LifeCycleCORBA/LifeCycleCORBA.py @@ -175,6 +175,8 @@ class LifeCycleCORBA: def FindOrStartContainer(self, theComputer , theContainer ): """Find or Start a container on theComputer machine with theContainer name """ + if theComputer in ("","localhost"): + theComputer=getShortHostName() MESSAGE( "FindOrStartContainer: " + theComputer + theContainer ) aContainer = self.FindContainer( theComputer + "/" + theContainer ) if aContainer is None : diff --git a/src/LifeCycleCORBA/Makefile.in b/src/LifeCycleCORBA/Makefile.in index 8926e289a..c75b9d1f2 100644 --- a/src/LifeCycleCORBA/Makefile.in +++ b/src/LifeCycleCORBA/Makefile.in @@ -34,14 +34,14 @@ VPATH=.:@srcdir@:@top_srcdir@/idl @COMMENCE@ -EXPORT_HEADERS = SALOME_LifeCycleCORBA.hxx +EXPORT_HEADERS = SALOME_LifeCycleCORBA.hxx EXPORT_PYSCRIPTS = LifeCycleCORBA.py Launchers.py # Libraries targets LIB = libSalomeLifeCycleCORBA.la -LIB_SRC = SALOME_LifeCycleCORBA.cxx +LIB_SRC = SALOME_LifeCycleCORBA.cxx Launchers.cxx LIB_CLIENT_IDL = SALOME_Component.idl SALOME_TestComponent.idl \ SALOME_ModuleCatalog.idl @@ -51,7 +51,9 @@ BIN_SRC = BIN_CLIENT_IDL = SALOME_Component.idl SALOME_TestComponent.idl \ SALOME_ModuleCatalog.idl -LDFLAGS += -lSalomeNS -lOpUtil -lSALOMELocalTrace +CPPFLAGS+= $(PYTHON_INCLUDES) +LDFLAGS += -lSalomeNS -lOpUtil -lSALOMELocalTrace -lSALOMETraceCollector +LIBS += $(PYTHON_LIBS) @CONCLUDE@ diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx index 7908d60ae..05a4afcfa 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx @@ -33,6 +33,7 @@ #include "OpUtil.hxx" #include "utilities.h" +#include "Launchers.hxx" #include @@ -139,117 +140,52 @@ Engines::Container_var SALOME_LifeCycleCORBA::FindOrStartContainer( const string aComputerContainer , const string theComputer , const string theContainer ) { - Engines::Container_var aContainer = FindContainer( aComputerContainer.c_str() ) ; - Engines::Container_var aFactoryServer ; SCRUTE( aComputerContainer ) ; SCRUTE( theComputer ) ; SCRUTE( theContainer ) ; + + Engines::Container_var aContainer = FindContainer( aComputerContainer.c_str() ) ; + + if ( !CORBA::is_nil( aContainer ) ) { + return aContainer ; + } + + Engines::Container_var aFactoryServer ; + bool pyCont = false ; int len = theContainer.length() ; if ( !strcmp( &theContainer.c_str()[len-2] , "Py" ) ) { pyCont = true ; } - if ( !CORBA::is_nil( aContainer ) ) { - return aContainer ; + + string addr=_NS->getIORaddr(); + string CMD="SALOME_Container"; + if ( pyCont ) { + CMD="SALOME_ContainerPy.py"; } - else { - string FactoryServer = theComputer ; - if ( pyCont ) { - FactoryServer += "/FactoryServerPy" ; - } - else { - FactoryServer += "/FactoryServer" ; - } - aFactoryServer = FindContainer( FactoryServer.c_str() ) ; - if ( CORBA::is_nil( aFactoryServer ) ) { -// rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 & - string rsh( "" ) ; - if ( theComputer!= GetHostname() ) { - rsh += "rsh -n " ; - rsh += theComputer ; - rsh += " " ; - } - string path = ComputerPath( theComputer.c_str() ) ; - SCRUTE( path ) ; - if ( path[0] != '\0' ) { - rsh += path ; - rsh += "/../bin/" ; - } - rsh += "runSession " ; - if ( pyCont ) { - rsh += "SALOME_ContainerPy.py " ; - rsh += "FactoryServerPy -" ; - } - else { - rsh += "SALOME_Container " ; - rsh += "FactoryServer -" ; - } - string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ; - ifstream omniORBfile( omniORBcfg.c_str() ) ; - char ORBInitRef[12] ; - char nameservice[132] ; - omniORBfile >> ORBInitRef ; - rsh += ORBInitRef ; - rsh += " " ; - omniORBfile >> nameservice ; - omniORBfile.close() ; - char * bsn = strchr( nameservice , '\n' ) ; - if ( bsn ) { - bsn[ 0 ] = '\0' ; - } - rsh += nameservice ; - if ( pyCont ) { - rsh += " > /tmp/FactoryServerPy_" ; - } - else { - rsh += " > /tmp/FactoryServer_" ; - } - rsh += theComputer ; - rsh += ".log 2>&1 &" ; - SCRUTE( rsh ); - int status = system( rsh.c_str() ) ; - if (status == -1) { - MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ; - } - else if (status == 217) { - MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ; - } - else { - int count = 21 ; - while ( CORBA::is_nil( aFactoryServer ) && count ) { - sleep( 1 ) ; - count-- ; - if ( count != 10 ) + CMD=CMD + " " + theContainer; + CMD=CMD + " -ORBInitRef NameService="+addr; + + /* + * Get the appropriate launcher and ask to launch + */ + PyObject * launcher=getLauncher((char *)theComputer.c_str()); + Launcher_Slaunch(launcher,(char *)theComputer.c_str(),(char *)CMD.c_str()); + /* + * Wait until the container is registered in Naming Service + */ + int count = 5 ; + while ( CORBA::is_nil( aFactoryServer ) && count ) { + sleep( 1 ) ; + count-- ; + if ( count != 10 ) MESSAGE( count << ". Waiting for FactoryServer on " << theComputer) - aFactoryServer = FindContainer( FactoryServer.c_str() ) ; - } - if ( CORBA::is_nil( aFactoryServer ) ) { - MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ; - } - else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) { - _FactoryServer = aFactoryServer ; - } - } - } - if ( !CORBA::is_nil( aFactoryServer ) ) { - if ( strcmp( theContainer.c_str() , "FactoryServer" ) || - strcmp( theContainer.c_str() , "FactoryServerPy" ) ) { - MESSAGE("Container not found ! trying to start " << aComputerContainer); - Engines::Container_var myContainer = aFactoryServer->start_impl( theContainer.c_str() ) ; - if ( !CORBA::is_nil( myContainer ) ) { - MESSAGE("Container " << aComputerContainer << " started"); - return myContainer ; - } - else { - MESSAGE("Container " << aComputerContainer << " NOT started"); - } - } - else { - MESSAGE("Container " << aComputerContainer << " started"); - return aFactoryServer ; - } - } + aFactoryServer = FindContainer( aComputerContainer.c_str() ) ; + } + if ( !CORBA::is_nil( aFactoryServer ) ) { + return aFactoryServer; } + MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ; return Engines::Container::_nil(); } diff --git a/src/LifeCycleCORBA/TestLifeCycleCORBA.cxx b/src/LifeCycleCORBA/TestLifeCycleCORBA.cxx index 603c32814..49e3b28b0 100644 --- a/src/LifeCycleCORBA/TestLifeCycleCORBA.cxx +++ b/src/LifeCycleCORBA/TestLifeCycleCORBA.cxx @@ -26,7 +26,6 @@ // Module : SALOME // $Header$ -#include "utilities.h" #include #include #include @@ -34,7 +33,10 @@ #include CORBA_CLIENT_HEADER(SALOME_TestComponent) #include "SALOME_NamingService.hxx" #include "SALOME_LifeCycleCORBA.hxx" +#include "utilities.h" +#include "SALOMETraceCollector.hxx" #include + using namespace std; int main (int argc, char * argv[]) @@ -44,6 +46,7 @@ int main (int argc, char * argv[]) { // Initializing omniORB CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); + SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb); // Obtain a reference to the root POA CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ; @@ -58,6 +61,10 @@ int main (int argc, char * argv[]) string containerName = GetHostname(); + cout << containerName << endl; + cout << "FindOrLoadComponent " + containerName + "/" + "SalomeTestComponent" << endl; + MESSAGE("FindOrLoadComponent " + containerName + "/" + "SalomeTestComponent" ); + Engines::Component_var mycompo = _LCC.FindOrLoad_Component(containerName.c_str(),"SalomeTestComponent"); @@ -87,7 +94,22 @@ int main (int argc, char * argv[]) ASSERT(!CORBA::is_nil(m2)); SCRUTE(m2->instanceName()); + cout << m2->instanceName() << endl; MESSAGE("Coucou " << m2->Coucou(1L)); + + Engines::Component_var mycompo3 = _LCC.FindOrLoad_Component("totoPy","SALOME_TestComponentPy"); + ASSERT(!CORBA::is_nil(mycompo3)); + Engines::TestComponent_var m3 = Engines::TestComponent::_narrow(mycompo3); + ASSERT(!CORBA::is_nil(m3)); + cout << m3->instanceName() << endl; + + string containerName4 = containerName + "/titiPy"; + Engines::Component_var mycompo4 = _LCC.FindOrLoad_Component(containerName4.c_str(),"SALOME_TestComponentPy"); + ASSERT(!CORBA::is_nil(mycompo4)); + Engines::TestComponent_var m4 = Engines::TestComponent::_narrow(mycompo4); + ASSERT(!CORBA::is_nil(m4)); + cout << m4->instanceName() << endl; + } catch(CORBA::COMM_FAILURE& ex) { diff --git a/src/NamingService/SALOME_NamingService.cxx b/src/NamingService/SALOME_NamingService.cxx index 07b0e9342..ffcb52224 100644 --- a/src/NamingService/SALOME_NamingService.cxx +++ b/src/NamingService/SALOME_NamingService.cxx @@ -1311,3 +1311,7 @@ SALOME_NamingService::_current_directory(char** result_path, _current_context = _ref_context ; } //---------------------------------------------------------------------- + +char * SALOME_NamingService::getIORaddr(){ + return _orb->object_to_string(_root_context); +} diff --git a/src/NamingService/SALOME_NamingService.hxx b/src/NamingService/SALOME_NamingService.hxx index 4504e6ec7..57f9e4b10 100644 --- a/src/NamingService/SALOME_NamingService.hxx +++ b/src/NamingService/SALOME_NamingService.hxx @@ -93,6 +93,9 @@ public: virtual void Destroy_Directory(const char* Path) throw(ServiceUnreachable); + //! get IORstring naming service address + char * getIORaddr(); + protected: CORBA::ORB_ptr _orb; CosNaming::NamingContext_var _root_context, _current_context;