+#endif
+
+//=============================================================================
+//! Get or create a file reference object associated to a local file (to transfer it)
+/*!
+* CORBA method: get or create a fileRef object associated to a local file
+* (a file on the computer on which runs the container server), which stores
+* a list of (machine, localFileName) corresponding to copies already done.
+*
+* \param origFileName absolute path for a local file to copy on other
+* computers
+* \return a fileRef object associated to the file.
+*/
+//=============================================================================
+Engines::fileRef_ptr
+Engines_Container_i::createFileRef(const char* origFileName)
+{
+ std::string origName(origFileName);
+ Engines::fileRef_var theFileRef = Engines::fileRef::_nil();
+
+ if (origName[0] != '/')
+ {
+ INFOS("path of file to copy must be an absolute path beginning with '/'");
+ return Engines::fileRef::_nil();
+ }
+
+ if (CORBA::is_nil(_fileRef_map[origName]))
+ {
+ CORBA::Object_var obj=_poa->id_to_reference(*_id);
+ Engines::Container_var pCont = Engines::Container::_narrow(obj);
+ fileRef_i* aFileRef = new fileRef_i(pCont, origFileName);
+ theFileRef = Engines::fileRef::_narrow(aFileRef->_this());
+ _numInstanceMutex.lock() ; // lock to be alone (stl container write)
+ _fileRef_map[origName] = theFileRef;
+ _numInstanceMutex.unlock() ;
+ }
+
+ theFileRef = Engines::fileRef::_duplicate(_fileRef_map[origName]);
+ ASSERT(! CORBA::is_nil(theFileRef));
+ return theFileRef._retn();
+}
+
+//=============================================================================
+//! Get a fileTransfer reference
+/*!
+* CORBA method:
+* \return a reference to the fileTransfer object
+*/
+//=============================================================================
+Engines::fileTransfer_ptr
+Engines_Container_i::getFileTransfer()
+{
+ Engines::fileTransfer_var aFileTransfer
+ = Engines::fileTransfer::_duplicate(_fileTransfer);
+ return aFileTransfer._retn();
+}
+
+//=============================================================================
+//! Create a Salome file
+//=============================================================================
+Engines::Salome_file_ptr
+Engines_Container_i::createSalome_file(const char* origFileName)
+{
+ std::string origName(origFileName);
+ if (CORBA::is_nil(_Salome_file_map[origName]))
+ {
+ Salome_file_i* aSalome_file = new Salome_file_i();
+ aSalome_file->setContainer(Engines::Container::_duplicate(this->_this()));
+ try
+ {
+ aSalome_file->setLocalFile(origFileName);
+ aSalome_file->recvFiles();
+ }
+ catch (const SALOME::SALOME_Exception& /*e*/) //!< TODO: unused variable
+ {
+ return Engines::Salome_file::_nil();
+ }
+
+ Engines::Salome_file_var theSalome_file = Engines::Salome_file::_nil();
+ theSalome_file = Engines::Salome_file::_narrow(aSalome_file->_this());
+ _numInstanceMutex.lock() ; // lock to be alone (stl container write)
+ _Salome_file_map[origName] = theSalome_file;
+ _numInstanceMutex.unlock() ;
+ }
+
+ Engines::Salome_file_ptr theSalome_file =
+ Engines::Salome_file::_duplicate(_Salome_file_map[origName]);
+ ASSERT(!CORBA::is_nil(theSalome_file));
+ return theSalome_file;
+}
+
+//=============================================================================
+/*! \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");
+ }
+}
+
+//=============================================================================
+/*! \brief create a PyNode object to execute remote python code
+ * \param nodeName the name of the node
+ * \param code the python code to load
+ * \return the PyNode
+ */
+//=============================================================================
+Engines::PyNode_ptr Engines_Container_i::createPyNode(const char* nodeName, const char* code)
+{
+ Engines::PyNode_var node= Engines::PyNode::_nil();
+
+ PyGILState_STATE gstate = PyGILState_Ensure();
+ PyObject *res = PyObject_CallMethod(_pyCont,
+ (char*)"create_pynode",
+ (char*)"ss",
+ nodeName,
+ code);
+ if(res==NULL)
+ {
+ //internal error
+ PyErr_Print();
+ PyGILState_Release(gstate);
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "can not create a python node";
+ throw SALOME::SALOME_Exception(es);
+ }
+ long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
+ PyObject* result=PyTuple_GetItem(res,1);
+ std::string astr=PyUnicode_AsUTF8(result);
+ Py_DECREF(res);
+ PyGILState_Release(gstate);
+ if(ierr==0)
+ {
+ Utils_Locker lck(&_mutexForDftPy);
+ CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
+ node=Engines::PyNode::_narrow(obj);
+ std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
+ if(it==_dftPyNode.end())
+ {
+ _dftPyNode[nodeName]=node;
+ }
+ else
+ {
+ Engines::PyNode_var oldNode((*it).second);
+ if(!CORBA::is_nil(oldNode))
+ oldNode->UnRegister();
+ (*it).second=node;
+ }
+ if(!CORBA::is_nil(node))
+ node->Register();
+ return node._retn();
+ }
+ else
+ {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = astr.c_str();
+ throw SALOME::SALOME_Exception(es);
+ }
+}
+
+//=============================================================================
+/*! \brief Retrieves the last created PyNode instance with createPyNode.
+ *
+ */
+//=============================================================================
+Engines::PyNode_ptr Engines_Container_i::getDefaultPyNode(const char *nodeName)
+{
+ Utils_Locker lck(&_mutexForDftPy);
+ std::map<std::string,Engines::PyNode_var>::iterator it(_dftPyNode.find(nodeName));
+ if(it==_dftPyNode.end())
+ return Engines::PyNode::_nil();
+ else
+ {
+ Engines::PyNode_var tmpVar((*it).second);
+ if(!CORBA::is_nil(tmpVar))
+ return Engines::PyNode::_duplicate(tmpVar);
+ else
+ return Engines::PyNode::_nil();
+ }
+}
+
+//=============================================================================
+/*! \brief create a PyScriptNode object to execute remote python code
+ * \param nodeName the name of the node
+ * \param code the python code to load
+ * \return the PyScriptNode
+ */
+//=============================================================================
+Engines::PyScriptNode_ptr Engines_Container_i::createPyScriptNode(const char* nodeName, const char* code)
+{
+ Engines::PyScriptNode_var node= Engines::PyScriptNode::_nil();
+
+ PyGILState_STATE gstate = PyGILState_Ensure();
+ PyObject *res = PyObject_CallMethod(_pyCont,
+ (char*)"create_pyscriptnode",
+ (char*)"ss",
+ nodeName,
+ code);
+ if(res==NULL)
+ {
+ //internal error
+ PyErr_Print();
+ PyGILState_Release(gstate);
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "can not create a python node";
+ throw SALOME::SALOME_Exception(es);
+ }
+ long ierr=PyLong_AsLong(PyTuple_GetItem(res,0));
+ PyObject* result=PyTuple_GetItem(res,1);
+ std::string astr=PyUnicode_AsUTF8(result);
+ Py_DECREF(res);
+ PyGILState_Release(gstate);
+
+ if(ierr==0)
+ {
+ Utils_Locker lck(&_mutexForDftPy);
+ CORBA::Object_var obj=_orb->string_to_object(astr.c_str());
+ node=Engines::PyScriptNode::_narrow(obj);
+ std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
+ if(it==_dftPyScriptNode.end())
+ {
+ _dftPyScriptNode[nodeName]=node;
+ }
+ else
+ {
+ Engines::PyScriptNode_var oldNode((*it).second);
+ if(!CORBA::is_nil(oldNode))
+ oldNode->UnRegister();
+ (*it).second=node;
+ }
+ return node._retn();
+ }
+ else
+ {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = astr.c_str();
+ throw SALOME::SALOME_Exception(es);
+ }
+}
+
+void Engines_Container_i::removePyScriptNode(const char *nodeName)
+{
+ std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
+ if(it==_dftPyScriptNode.end())
+ {
+ std::ostringstream oss; oss << "Engines_Container_i::removePyScriptNode : node \"" << nodeName << "\" is not map !";
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = oss.str().c_str();
+ throw SALOME::SALOME_Exception(es);
+ }
+ (*it).second->UnRegister();
+ _dftPyScriptNode.erase(it);
+}
+
+void Engines_Container_i::cleanAllPyScripts()
+{
+ for(std::map<std::string,Engines::PyNode_var>::iterator it=_dftPyNode.begin();it!=_dftPyNode.end();it++)
+ {
+ Engines::PyNode_var tmpVar((*it).second);
+ if(!CORBA::is_nil(tmpVar))
+ tmpVar->UnRegister();
+ }
+ _dftPyNode.clear();
+ for(std::map<std::string,Engines::PyScriptNode_var>::iterator it=_dftPyScriptNode.begin();it!=_dftPyScriptNode.end();it++)
+ {
+ Engines::PyScriptNode_var tmpVar((*it).second);
+ if(!CORBA::is_nil(tmpVar))
+ tmpVar->UnRegister();
+ }
+ _dftPyScriptNode.clear();
+}
+
+//=============================================================================
+/*! \brief Retrieves the last created PyScriptNode instance with createPyScriptNode.
+ *
+ */
+//=============================================================================
+Engines::PyScriptNode_ptr Engines_Container_i::getDefaultPyScriptNode(const char *nodeName)
+{
+ Utils_Locker lck(&_mutexForDftPy);
+ std::map<std::string,Engines::PyScriptNode_var>::iterator it(_dftPyScriptNode.find(nodeName));
+ if(it==_dftPyScriptNode.end())
+ return Engines::PyScriptNode::_nil();
+ else
+ {
+ Engines::PyScriptNode_var tmpVar((*it).second);
+ if(!CORBA::is_nil(tmpVar))
+ return Engines::PyScriptNode::_duplicate(tmpVar);
+ else
+ return Engines::PyScriptNode::_nil();
+ }
+}
+
+//=============================================================================
+/* int checkifexecutable(const char *filename)
+*
+* Return non-zero if the name is an executable file, and
+* zero if it is not executable, or if it does not exist.
+*/
+//=============================================================================
+int checkifexecutable(const std::string& filename)
+{
+ int result;
+ struct stat statinfo;