Salome HOME
CCAR: add some methods to container and lifecycle to copy files to container, CCAR_rm_start
authorcaremoli <caremoli>
Tue, 14 Apr 2009 16:41:37 +0000 (16:41 +0000)
committercaremoli <caremoli>
Tue, 14 Apr 2009 16:41:37 +0000 (16:41 +0000)
from container to another container and update SWIG interface

idl/SALOME_Component.idl
src/Container/Container_i.cxx
src/Container/SALOME_Container_i.hxx
src/Container/SALOME_FileTransfer_i.cxx
src/Container/SALOME_FileTransfer_i.hxx
src/Container/Salome_file_i.hxx
src/LifeCycleCORBA/SALOME_LifeCycleCORBA.cxx
src/LifeCycleCORBA/SALOME_LifeCycleCORBA.hxx
src/LifeCycleCORBA_SWIG/LifeCycleCORBA.py
src/LifeCycleCORBA_SWIG/libSALOME_LifeCycleCORBA.i

index e47f886d031cac89eb998532d292922aff6a2fc6..616cda33cce5af6d952ad50312d65a29333073c7 100644 (file)
@@ -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. 
index 7558ef37ef32624f1a4ed17d5ae87959dbac7d0e..065c5b4b55165398d5c8d36bbdc9f8766fad92c7 100644 (file)
@@ -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");
+    }
+}
+
index 08afa42af91766f239ebf24f3d780e192580b59f..9dae83fb6f4146eb7b878ed3de5c22878ae93e80 100644 (file)
@@ -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
index b1195972647c15a2ea2b4827737cfb35ec5b09ca..fd54215240d28b81d6eba6dc5a74813fdb43f89b 100644 (file)
@@ -97,7 +97,11 @@ void fileTransfer_i::close(CORBA::Long fileId)
     {
       INFOS(" no FILE structure associated to fileId " <<fileId);
     }
-  else fclose(fp);
+  else 
+    {
+      fclose(fp);
+      _fileAccess.erase(fileId);
+    }
 }
 
 #define FILEBLOCK_SIZE 256*1024
@@ -135,3 +139,48 @@ Engines::fileBlock* fileTransfer_i::getBlock(CORBA::Long fileId)
   return aBlock;
 }
 
+/*! \brief open the given file in write mode (for copy)
+ *
+ *  CORBA method: try to open the file. If the file is writable, 
+ *  return a positive integer else return 0;
+ *  \param  fileName path to the file to be transfered
+ *  \return fileId = positive integer > 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 " <<fileId);
+      return ;
+    }
+  int toFollow = block.length();
+  SCRUTE(toFollow);
+  const CORBA::Octet *buf = block.get_buffer();
+  fwrite(buf, sizeof(CORBA::Octet), toFollow, fp);
+}
+
+
index 5114bf7cedf0d5553bd65eb1555428cc2d58245e..dd96d400990c6f23a9795d74c03ee438599cb454 100644 (file)
@@ -47,10 +47,12 @@ public:
   void close(CORBA::Long fileId);
 
   Engines::fileBlock* getBlock(CORBA::Long fileId);
+  CORBA::Long openW(const char* fileName);
+  void putBlock(CORBA::Long fileId, const Engines::fileBlock& block);
 
 protected:
   int _fileKey;
-  std::map<int, FILE*> _fileAccess;
+  std::map<int, FILE* > _fileAccess;
   int _ctr;
 };
 
index ac72586e73e7d73629b1887144d1d8ecf54cb47e..d92ab4f4cf7b07aae60dfe93399603b5484e746e 100644 (file)
 #include CORBA_SERVER_HEADER(SALOME_Exception)
 
 #include "SALOME_Container.hxx"
+#include "SALOME_FileTransfer_i.hxx"
+
 #include <map>
 #include <cstdio>
 #include <string>
 
 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();
index a9ab219c8327350291a99f52b31182cafd8314a6..3d68dcee7fc522ea20b8420147aa3fd4f5eda4f2 100644 (file)
@@ -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);
+}
+
index dfbf3d7c9bfefad3265836c0720bc27ed6b22a8e..d7ba91303d52390585fdc3c0e8a2a3c6d94858c9 100644 (file)
@@ -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();
index 2b5670b71093cac6c89785ea4c9bf714094e8a42..d5e0c3a4d0fefb30f6e661df04d67517aef2fcf0 100644 (file)
@@ -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)
+
index d2d5fe26866fac3e40e762b5bf3724032d206e1f..4482c92e8ebc30ec3b26b4d2070942b0d73c65d8 100644 (file)
@@ -26,6 +26,7 @@
 %feature("autodoc", "1");
 
 %include <std_except.i>
+%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 <hold_lock> 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 <hold_lock> 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;