From 6b9314dbb2f5926cee20470fc18fd708513ce099 Mon Sep 17 00:00:00 2001 From: caremoli Date: Tue, 14 Apr 2009 16:41:37 +0000 Subject: [PATCH] CCAR: add some methods to container and lifecycle to copy files to container, from container to another container and update SWIG interface --- idl/SALOME_Component.idl | 21 ++ src/Container/Container_i.cxx | 44 +++++ src/Container/SALOME_Container_i.hxx | 1 + src/Container/SALOME_FileTransfer_i.cxx | 51 ++++- src/Container/SALOME_FileTransfer_i.hxx | 4 +- src/Container/Salome_file_i.hxx | 4 +- src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx | 44 ++++- src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx | 3 +- src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py | 11 ++ .../libSALOME_LifeCycleCORBA.i | 180 ++++++++---------- 10 files changed, 255 insertions(+), 108 deletions(-) diff --git a/idl/SALOME_Component.idl b/idl/SALOME_Component.idl index e47f886d0..616cda33c 100644 --- a/idl/SALOME_Component.idl +++ b/idl/SALOME_Component.idl @@ -179,6 +179,13 @@ module Engines */ fileTransfer getFileTransfer(); + //! Copy a file from a remote host (container) to a local file + /*! + \param contai the remote container + \param remoteFile the file on the remote host to copy + \param localFile the local file to create by copy + */ + void copyFile(in Container contai, in string remoteFile, in string localFile); }; /*! \brief Interface of the %component. @@ -430,6 +437,12 @@ module Engines File. */ long open(in string fileName); + //! Open the file transfer in write mode for file fileName + /*! + \param fileName the file to copy into with putBlock + \return the id to use with putBlock + */ + long openW(in string fileName); //! Close the file transfer /*! @@ -444,6 +457,14 @@ module Engines The last block is empty, and identifies the end of file. */ fileBlock getBlock(in long fileId); + + //! Put a file data block + /*! + \param fileId identification of the file obtained by openW + \param block a data block to copy into the file identified by fileId + */ + void putBlock(in long fileId, in fileBlock block); + }; //! A file managed by a Salome_file. diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 7558ef37e..065c5b4b5 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -1371,3 +1371,47 @@ void SigIntHandler( int what ) } } #endif + +/*! \brief copy a file from a remote host (container) to the local host + * \param container the remote container + * \param remoteFile the file to copy locally from the remote host into localFile + * \param localFile the local file + */ +void Engines_Container_i::copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile) +{ + Engines::fileTransfer_var fileTransfer = container->getFileTransfer(); + + FILE* fp; + if ((fp = fopen(localFile,"wb")) == NULL) + { + INFOS("file " << localFile << " cannot be open for writing"); + return; + } + + CORBA::Long fileId = fileTransfer->open(remoteFile); + if (fileId > 0) + { + Engines::fileBlock* aBlock; + int toFollow = 1; + int ctr=0; + while (toFollow) + { + ctr++; + SCRUTE(ctr); + aBlock = fileTransfer->getBlock(fileId); + toFollow = aBlock->length(); + SCRUTE(toFollow); + CORBA::Octet *buf = aBlock->get_buffer(); + fwrite(buf, sizeof(CORBA::Octet), toFollow, fp); + delete aBlock; + } + fclose(fp); + MESSAGE("end of transfer"); + fileTransfer->close(fileId); + } + else + { + INFOS("open reference file for copy impossible"); + } +} + diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index 08afa42af..9dae83fb6 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -96,6 +96,7 @@ public: Engines::fileTransfer_ptr getFileTransfer(); virtual Engines::Salome_file_ptr createSalome_file(const char* origFileName); + void copyFile(Engines::Container_ptr container, const char* remoteFile, const char* localFile); // --- local C++ methods Engines::Component_ptr diff --git a/src/Container/SALOME_FileTransfer_i.cxx b/src/Container/SALOME_FileTransfer_i.cxx index b11959726..fd5421524 100644 --- a/src/Container/SALOME_FileTransfer_i.cxx +++ b/src/Container/SALOME_FileTransfer_i.cxx @@ -97,7 +97,11 @@ void fileTransfer_i::close(CORBA::Long fileId) { INFOS(" no FILE structure associated to fileId " < 0 if open OK. + */ +CORBA::Long fileTransfer_i::openW(const char* fileName) +{ + MESSAGE(" fileTransfer_i::openW " << fileName); + int aKey = _fileKey++; + _ctr=0; + FILE* fp; + if ((fp = fopen(fileName,"wb")) == NULL) + { + INFOS("file " << fileName << " is not writable"); + return 0; + } + _fileAccess[aKey] = fp; + return aKey; +} + +/*! \brief put a data block for copy into a file + * + * CORBA method: put a block of data into the file associated to the fileId + * given at openW. + * \param fileId got in return from openW method + * \param block an octet sequence to copy into opened file + */ +void fileTransfer_i::putBlock(CORBA::Long fileId, const Engines::fileBlock& block) +{ + MESSAGE("fileTransfer_i::putBlock"); + FILE* fp; + if (! (fp = _fileAccess[fileId]) ) + { + INFOS(" no FILE structure associated to fileId " < _fileAccess; + std::map _fileAccess; int _ctr; }; diff --git a/src/Container/Salome_file_i.hxx b/src/Container/Salome_file_i.hxx index ac72586e7..d92ab4f4c 100644 --- a/src/Container/Salome_file_i.hxx +++ b/src/Container/Salome_file_i.hxx @@ -33,12 +33,14 @@ #include CORBA_SERVER_HEADER(SALOME_Exception) #include "SALOME_Container.hxx" +#include "SALOME_FileTransfer_i.hxx" + #include #include #include class CONTAINER_EXPORT Salome_file_i: - public virtual POA_Engines::Salome_file + public virtual POA_Engines::Salome_file,public virtual fileTransfer_i { public: Salome_file_i(); diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx index a9ab219c8..3d68dcee7 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx @@ -51,6 +51,7 @@ #include "SALOME_ContainerManager.hxx" #include "SALOME_Component_i.hxx" #include "SALOME_NamingService.hxx" +#include "SALOME_FileTransferCORBA.hxx" using namespace std; @@ -368,13 +369,19 @@ void SALOME_LifeCycleCORBA::preSet( Engines::MachineParameters& params) { params.container_name = ""; params.hostname = ""; + params.alias = ""; + params.protocol = ""; + params.username = ""; + params.applipath = ""; params.OS = ""; params.mem_mb = 0; params.cpu_clock = 0; params.nb_proc_per_node = 0; params.nb_node = 0; params.isMPI = false; - + params.mpiImpl=""; + params.batch=""; + params.workingdir = ""; params.parallelLib = ""; params.nb_component_nodes = 0; } @@ -707,3 +714,38 @@ SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters& return myInstance._retn(); } +/*! \brief copy a file from a source host to a destination host + * \param hostSrc the source host + * \param fileSrc the file to copy from the source host to the destination host + * \param hostDest the destination host + * \param fileDest the destination file + */ +void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest) +{ + if(strcmp(hostDest,"localhost") == 0) + { + //if localhost use a shortcut + SALOME_FileTransferCORBA transfer(hostSrc,fileSrc); + transfer.getLocalFile(fileDest); + return; + } + + Engines::ContainerManager_var contManager = getContainerManager(); + + Engines::MachineParameters params; + preSet(params); + + Engines::MachineList listOfMachines; + listOfMachines.length(1); + + params.hostname = hostDest; + listOfMachines[0] = hostDest; + Engines::Container_var containerDest = contManager->FindOrStartContainer(params, listOfMachines); + + params.hostname = hostSrc; + listOfMachines[0] = hostSrc; + Engines::Container_var containerSrc = contManager->FindOrStartContainer(params, listOfMachines); + + containerDest->copyFile(containerSrc,fileSrc,fileDest); +} + diff --git a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx index dfbf3d7c9..d7ba91303 100644 --- a/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx +++ b/src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx @@ -98,10 +98,11 @@ public: int NbProc(const Engines::MachineParameters& params); - void preSet(Engines::MachineParameters& params); + static void preSet(Engines::MachineParameters& outparams); Engines::ContainerManager_ptr getContainerManager(); Engines::ResourcesManager_ptr getResourcesManager(); + void copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest); void shutdownServers(); static void killOmniNames(); diff --git a/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py b/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py index 2b5670b71..d5e0c3a4d 100644 --- a/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py +++ b/src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py @@ -37,3 +37,14 @@ class LifeCycleCORBA (SALOME_LifeCycleCORBA): return SALOME_LifeCycleCORBA.FindOrLoad_Component(self, containerName, componentName) + +class MachineParameters (Engines.MachineParameters): + def __init__(self, container_name='', hostname='', alias='', protocol='', username='', + applipath='', componentList=[], OS='', mem_mb=0, cpu_clock=0, + nb_proc_per_node=0, nb_node=0, isMPI=False, mpiImpl='', batch='', workingdir='', + parallelLib='', nb_component_nodes=0): + Engines.MachineParameters.__init__(self,container_name, hostname, alias, protocol, username, + applipath, componentList, OS, mem_mb, cpu_clock, + nb_proc_per_node, nb_node, isMPI, mpiImpl, batch, workingdir, + parallelLib, nb_component_nodes) + diff --git a/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i b/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i index d2d5fe268..4482c92e8 100644 --- a/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i +++ b/src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i @@ -26,6 +26,7 @@ %feature("autodoc", "1"); %include +%include "std_string.i" // ---------------------------------------------------------------------------- @@ -43,19 +44,19 @@ typedef int Py_ssize_t; #define PY_SSIZE_T_MIN INT_MIN #endif - using namespace std; +using namespace std; //--- from omniORBpy.h (not present on Debian Sarge packages) struct omniORBpyAPI { PyObject* (*cxxObjRefToPyObjRef)(const CORBA::Object_ptr cxx_obj, - CORBA::Boolean hold_lock); + CORBA::Boolean hold_lock); // Convert a C++ object reference to a Python object reference. // If is true, caller holds the Python interpreter lock. CORBA::Object_ptr (*pyObjRefToCxxObjRef)(PyObject* py_obj, - CORBA::Boolean hold_lock); + CORBA::Boolean hold_lock); // Convert a Python object reference to a C++ object reference. // Raises BAD_PARAM if the Python object is not an object reference. // If is true, caller holds the Python interpreter lock. @@ -65,7 +66,7 @@ struct omniORBpyAPI { // Constructor for the singleton. Sets up the function pointers. }; - omniORBpyAPI* api; +omniORBpyAPI* api; %} @@ -80,8 +81,7 @@ struct omniORBpyAPI { PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy"); if (!omnipy) { - PyErr_SetString(PyExc_ImportError, - (char*)"Cannot import _omnipy"); + PyErr_SetString(PyExc_ImportError, (char*)"Cannot import _omnipy"); return; } PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API"); @@ -92,8 +92,11 @@ struct omniORBpyAPI { // ---------------------------------------------------------------------------- +using namespace std; -%typemap(python,out) Engines::Container_ptr, Engines::Component_ptr, Engines::fileRef_ptr + +%typemap(out) Engines::Container_ptr, Engines::Component_ptr, Engines::fileRef_ptr, + Engines::ContainerManager_ptr, Engines::ResourcesManager_ptr { MESSAGE("typemap out on CORBA object ptr"); SCRUTE($1); @@ -101,7 +104,12 @@ struct omniORBpyAPI { SCRUTE($result); } -%typemap(python,in) Engines::fileRef_ptr aFileRef +%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) Engines::fileRef_ptr +{ + $1=PyObject_HasAttrString($input, "__omni_obj"); +} + +%typemap(in) Engines::fileRef_ptr aFileRef { MESSAGE("typemap in on CORBA object ptr"); try { @@ -114,114 +122,80 @@ struct omniORBpyAPI { } } - -%typemap(python,out) std::string, - string -{ - MESSAGE("typemap out on std::string"); - SCRUTE($1); - $result = PyString_FromString($1.c_str()); -} - -%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) Engines::MachineParameters const & -{ - $1 = PyDict_Check($input)? 1 : 0; -} %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) const Engines::MachineParameters & { $1 = PyDict_Check($input)? 1 : 0; } -%typemap(typecheck) std::string, - string -{ - $1 = PyString_Check($input); -} - -%typemap(python,in) std::string, - string -{ - MESSAGE("typemap in on std::string"); - std::string str; - if (PyString_Check($input) == 1) - { - char* value = PyString_AsString($input); - str = value; - $1 = str; - } - else - { - MESSAGE("Not a string"); - PyErr_SetString(PyExc_TypeError,"Must Be a Python string"); - return NULL; - } -} - - -%typemap(python,in) const Engines::MachineParameters & +%typemap(in) const Engines::MachineParameters & { //printf("typemap in on Engines::MachineParameters\n"); MESSAGE("typemap in on Engines::MachineParameters"); if (PyDict_Check($input) == 1) { Engines::MachineParameters *param = new Engines::MachineParameters ; - param->container_name = CORBA::string_dup(""); - param->hostname = CORBA::string_dup(""); - param->OS = CORBA::string_dup(""); - param->mem_mb = 0; - param->cpu_clock = 0; - param->nb_proc_per_node = 0; - param->nb_node = 0; - param->isMPI = false; - param->parallelLib = CORBA::string_dup(""); - param->nb_component_nodes = 0; + SALOME_LifeCycleCORBA::preSet(*param); + PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next($input, &pos, &key, &value)) - { - char* keystr = PyString_AsString(key); - printf("key: %s\n", 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); - } - else if (strcmp(keystr,"isMPI")==0) - { - param->isMPI = PyLong_AsLong(value); - } - else if (strcmp(keystr,"parallelLib")==0) - { - param->parallelLib = CORBA::string_dup(PyString_AsString(value)); - } - else if (strcmp(keystr,"nb_component_nodes")==0) - { - param->nb_component_nodes = PyLong_AsLong(value); - } - } + { + char* keystr = PyString_AsString(key); + 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,"alias")==0) + param->alias = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"protocol")==0) + param->protocol = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"username")==0) + param->username = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"applipath")==0) + param->applipath = 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); + } + else if (strcmp(keystr,"isMPI")==0) + { + param->isMPI = PyLong_AsLong(value); + } + else if (strcmp(keystr,"mpiImpl")==0) + param->mpiImpl = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"batch")==0) + param->batch = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"workingdir")==0) + param->workingdir = CORBA::string_dup(PyString_AsString(value)); + else if (strcmp(keystr,"parallelLib")==0) + { + param->parallelLib = CORBA::string_dup(PyString_AsString(value)); + } + else if (strcmp(keystr,"nb_component_nodes")==0) + { + param->nb_component_nodes = PyLong_AsLong(value); + } + } $1 = param; } else @@ -233,7 +207,7 @@ struct omniORBpyAPI { } -%typemap(python,freearg) const Engines::MachineParameters & +%typemap(freearg) const Engines::MachineParameters & { MESSAGE("delete $1"); delete $1; -- 2.39.2