From 1fb22af43250cee8e9f755cf4884b638d0463f37 Mon Sep 17 00:00:00 2001 From: prascle Date: Mon, 30 May 2005 12:14:43 +0000 Subject: [PATCH] PR: Swig LifeCycleCORBA --- src/LifeCycleCORBA/Makefile.in | 2 +- src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx | 22 +++-- src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx | 8 +- src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py | 41 +++++++++ src/LifeCycleCORBA_SWIG/Makefile.in | 52 +++++++++++ src/LifeCycleCORBA_SWIG/TestLifeCycleCORBA.py | 10 ++ .../libSALOME_LifeCycleCORBA.i | 91 +++++++++++++++++++ src/Makefile.in | 2 +- 8 files changed, 216 insertions(+), 12 deletions(-) create mode 100644 src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py create mode 100644 src/LifeCycleCORBA_SWIG/Makefile.in create mode 100644 src/LifeCycleCORBA_SWIG/TestLifeCycleCORBA.py create mode 100644 src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i diff --git a/src/LifeCycleCORBA/Makefile.in b/src/LifeCycleCORBA/Makefile.in index 3b825582f..b23e0d50a 100644 --- a/src/LifeCycleCORBA/Makefile.in +++ b/src/LifeCycleCORBA/Makefile.in @@ -36,7 +36,7 @@ VPATH=.:@srcdir@:@top_srcdir@/idl EXPORT_HEADERS = SALOME_LifeCycleCORBA.hxx -EXPORT_PYSCRIPTS = LifeCycleCORBA.py Launchers.py +EXPORT_PYSCRIPTS = Launchers.py # Libraries targets diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx index d6c18ede9..f8b2463f7 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx @@ -52,7 +52,15 @@ using namespace std; SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns) { - _NS = ns; + if (!ns) + { + int argc = 0; + char *xargv = ""; + char **argv = &xargv; + CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); + _NS = new SALOME_NamingService(orb); + } + else _NS = ns; //add try catch _NS->Change_Directory("/"); // mpv 250105: current directory may be not root // (in SALOMEDS for an example) @@ -188,7 +196,9 @@ SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params, //============================================================================= /*! Public - * Find and aready existing and registered component instance or load a new - * component instance on a container defined by machine parameters + * component instance on a container defined by machine parameters. + * Renamed (Else / Or) to avoid problems with Python (Swig) version and keep + * compatiblity with existing Python method FindOrLoadComponent * \param params machine parameters like type or name... * \param componentName the name of component class * \param studyId default = 0 : multistudy instance @@ -198,9 +208,9 @@ SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params, Engines::Component_ptr SALOME_LifeCycleCORBA:: -FindOrLoad_Component(const Engines::MachineParameters& params, - const char *componentName, - int studyId) +FindElseLoadComponent(const Engines::MachineParameters& params, + const char *componentName, + int studyId) { if (! isKnownComponentClass(componentName)) return Engines::Component::_nil(); @@ -275,7 +285,7 @@ SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName, params->hostname=CORBA::string_dup(stContainer); params->OS=CORBA::string_dup("LINUX"); free(stContainer); - return FindOrLoad_Component(params,componentName); + return FindElseLoadComponent(params,componentName); } } diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx index d86a7a471..d419c3596 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx @@ -42,7 +42,7 @@ class SALOME_NamingService; class SALOME_LifeCycleCORBA { public: - SALOME_LifeCycleCORBA(SALOME_NamingService *ns); + SALOME_LifeCycleCORBA(SALOME_NamingService *ns = 0); virtual ~SALOME_LifeCycleCORBA(); Engines::Container_ptr @@ -60,9 +60,9 @@ public: int studyId=0); Engines::Component_ptr - FindOrLoad_Component(const Engines::MachineParameters& params, - const char *componentName, - int studyId=0); + FindElseLoadComponent(const Engines::MachineParameters& params, + const char *componentName, + int studyId=0); Engines::Component_ptr FindOrLoad_Component(const char *containerName, diff --git a/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py b/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py new file mode 100644 index 000000000..f88ef8722 --- /dev/null +++ b/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py @@ -0,0 +1,41 @@ +# SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++ +# +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : LifeCycleCORBA.py +# Author : Paul RASCLE, EDF +# Module : SALOME +# $Header$ + + +import Engines +from libSALOME_LifeCycleCORBA import * + +class LifeCycleCORBA (SALOME_LifeCycleCORBA): + + def __init__(self, orb = None): + SALOME_LifeCycleCORBA.__init__(self) + + def FindOrLoadComponent(self, containerName, componentName): + return SALOME_LifeCycleCORBA.FindOrLoad_Component(self, + containerName, + componentName) diff --git a/src/LifeCycleCORBA_SWIG/Makefile.in b/src/LifeCycleCORBA_SWIG/Makefile.in new file mode 100644 index 000000000..c4c55c81d --- /dev/null +++ b/src/LifeCycleCORBA_SWIG/Makefile.in @@ -0,0 +1,52 @@ +# Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +# +# +# +# File : Makefile.in +# Author : Paul RASCLE, EDF +# Module : SALOME +# $Header$ + +top_srcdir=@top_srcdir@ +top_builddir=../.. +srcdir=@srcdir@ +VPATH=.:@srcdir@:@top_srcdir@/idl + + +@COMMENCE@ + +# Libraries targets + +LIB = libSALOME_LifeCycleCORBAcmodule.la +LIB_SRC = + +LIB_CLIENT_IDL = SALOME_Component.idl SALOME_ContainerManager.idl + +SWIG_DEF = libSALOME_LifeCycleCORBA.i + +EXPORT_PYSCRIPTS = libSALOME_LifeCycleCORBA.py LifeCycleCORBA.py TestLifeCycleCORBA.py + +CPPFLAGS+= $(PYTHON_INCLUDES) + +LIBS+= $(PYTHON_LIBS) -lSalomeLifeCycleCORBA + +LDFLAGS+= + +@CONCLUDE@ diff --git a/src/LifeCycleCORBA_SWIG/TestLifeCycleCORBA.py b/src/LifeCycleCORBA_SWIG/TestLifeCycleCORBA.py new file mode 100644 index 000000000..c8241085f --- /dev/null +++ b/src/LifeCycleCORBA_SWIG/TestLifeCycleCORBA.py @@ -0,0 +1,10 @@ + +import Engines +import LifeCycleCORBA + +lcc = LifeCycleCORBA.LifeCycleCORBA() + +obj=lcc.FindOrLoad_Component("FactoryServer","SalomeTestComponent") +comp=obj._narrow(Engines.TestComponent) +comp.Coucou(1) + diff --git a/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i b/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i new file mode 100644 index 000000000..07034b25f --- /dev/null +++ b/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i @@ -0,0 +1,91 @@ + + +%module libSALOME_LifeCycleCORBA + +%{ +#include "utilities.h" +#include "SALOME_LifeCycleCORBA.hxx" + + using namespace std; +%} + +%typemap(python,out) Engines::Container_ptr, Engines::Component_ptr +{ + MESSAGE("typemap out on CORBA object ptr"); + SCRUTE($1); + + // --- Get the Python orb + + PyObject* pdict = PyDict_New(); + PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins()); + PyRun_String("import CORBA", Py_single_input, pdict, pdict); + PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input, + pdict, pdict); + PyObject* orb = PyDict_GetItemString(pdict, "o"); + + // --- Get the C++ orb + + int argc = 0; + char *xargv = ""; + char **argv = &xargv; + CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv); + string s = ORB->object_to_string($1); + SCRUTE(s); + PyObject * tmp = PyString_FromString(s.c_str()); + SCRUTE(tmp); + $result = PyObject_CallMethod(orb, "string_to_object", "O", tmp); + SCRUTE($result); +} + +%typemap(python,in) const Engines::MachineParameters & +{ + MESSAGE("typemap in on Engines::MachineParameters"); + ASSERT (PyDict_Check($input)) + { + Engines::MachineParameters *param = new Engines::MachineParameters ; + PyObject *key, *value; + int pos = 0; + while (PyDict_Next($input, &pos, &key, &value)) + { + char* keystr = PyString_AsString(key); + MESSAGE("key: " << keystr); + if (strcmp(keystr,"container_name")==0) + { + param->container_name = CORBA::string_dup(PyString_AsString(value)); + } + else if (strcmp(keystr,"hostname")==0) + { + param->hostname = CORBA::string_dup(PyString_AsString(value)); + } + else if (strcmp(keystr,"OS")==0) + { + param->OS = CORBA::string_dup(PyString_AsString(value)); + } + else if (strcmp(keystr,"mem_mb")==0) + { + param->mem_mb = PyLong_AsLong(value); + } + else if (strcmp(keystr,"cpu_clock")==0) + { + param->cpu_clock = PyLong_AsLong(value); + } + else if (strcmp(keystr,"nb_proc_per_node")==0) + { + param->nb_proc_per_node = PyLong_AsLong(value); + } + else if (strcmp(keystr,"nb_node")==0) + { + param->nb_node = PyLong_AsLong(value); + } + } + $1 = param; + } +} + +%typemap(python,freearg) const Engines::MachineParameters & +{ + MESSAGE("delete $1"); + delete $1; +} + +%include "SALOME_LifeCycleCORBA.hxx" diff --git a/src/Makefile.in b/src/Makefile.in index d0daa6ea2..9912d266b 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -37,7 +37,7 @@ SUBDIRS = MSG2QM SALOMELocalTrace SALOMETraceCollector Logger Utils CASCatch Pat GenericObj MEDWrapper NamingService Registry \ ModuleCatalog DataTypeCatalog RessourcesCatalog \ ResourcesManager Notification NOTIFICATION_SWIG \ - Container TestContainer LifeCycleCORBA HDFPersist Prs \ + Container TestContainer LifeCycleCORBA LifeCycleCORBA_SWIG HDFPersist Prs \ VTKFilter OBJECT \ TOOLSDS SALOMEDS Event \ SALOMEGUI TOOLSGUI Plot2d VTKViewer OCCViewer \ -- 2.39.2