#include "MULTIPR_API.hxx"
#include "MULTIPR_Exceptions.hxx"
+#include "MULTIPR_Utils.hxx"
#include <SALOMEDS_Tool.hxx>
// Class MULTIPR_Gen_i implementation
//*****************************************************************************
-MULTIPR_Gen_i::MULTIPR_Gen_i(
- CORBA::ORB_ptr orb,
+MULTIPR_Gen_i::MULTIPR_Gen_i
+ (CORBA::ORB_ptr orb,
PortableServer::POA_ptr poa,
PortableServer::ObjectId* contId,
const char* instanceName,
throw (SALOME::SALOME_Exception)
{
MULTIPR_Obj_i* obj = new MULTIPR_Obj_i(medFilename);
- //MULTIPR_ORB::MULTIPR_Gen_ptr engine = _this();
- //obj->setEngine(engine);
obj->setEngine(this);
// Dump Python
- //MULTIPR::TPythonDump(engine) << obj << " = " << this << ".getObject(\"" << medFilename << "\")";
MULTIPR::TPythonDump(this) << obj << " = " << this << ".getObject(\"" << medFilename << "\")";
return obj->POA_MULTIPR_ORB::MULTIPR_Obj::_this();
}
-MULTIPR_Obj_i::MULTIPR_Obj_i (const char* medFilename)
- throw (SALOME::SALOME_Exception)
+MULTIPR_Obj_i::MULTIPR_Obj_i (const char* medFilename, bool isPersistence, bool isMultifile)
+ throw (SALOME::SALOME_Exception)
+ : mBoxing(100),
+ _engine(NULL),
+ mIsTmp(isPersistence && !isMultifile)
{
- mObj = new multipr::Obj();
- mBoxing = 100;
-
+ mObj = new multipr::Obj();
+
try
{
cout << "Load " << medFilename << endl;
- mObj->create(medFilename);
+ if (isPersistence)
+ mObj->restorePersistent(medFilename);
+ else
+ mObj->create(medFilename);
cout << endl;
}
catch (multipr::RuntimeException& e)
MULTIPR_Obj_i::~MULTIPR_Obj_i()
{
- if (mObj != NULL)
+ if (mObj != NULL)
+ {
+ if (mIsTmp)
{
- cout << "MULTIPR: Destructor: remove mObj" << endl;
- delete mObj;
- mObj = NULL;
+ // Remove temporary files, created during study loading, together with directory
+ std::string strFile = mObj->getMEDFilename();
+ std::string strPath = multipr::getPath(strFile.c_str());
+#ifdef WNT
+ //std::string cmd_rm ("del /F \"");
+#else
+ std::string cmd_rm ("rm -rf \"");
+#endif
+ cmd_rm += strPath + "\"";
+ system(cmd_rm.c_str());
}
+
+ cout << "MULTIPR: Destructor: remove mObj" << endl;
+ delete mObj;
+ mObj = NULL;
+ }
}
}
}
+std::string MULTIPR_Obj_i::getMeshName() const
+ throw (SALOME::SALOME_Exception)
+{
+ if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
+
+ return mObj->getMeshName();
+}
+
void MULTIPR_Obj_i::setBoxing (CORBA::Long pBoxing)
throw (SALOME::SALOME_Exception)
void MULTIPR_Obj_i::save(const char* pPath)
throw (SALOME::SALOME_Exception)
{
- if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
+ if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
- try
- {
- mObj->save(pPath);
- }
- catch (multipr::RuntimeException& e)
+ try
+ {
+ std::string strFile = mObj->getMEDFilename();
+
+ // Do Save
+ mObj->save(pPath);
+
+ // Remove temporary files, if needed
+ if (mIsTmp)
{
- e.dump(cout);
- THROW_SALOME_CORBA_EXCEPTION("Unable to save MED file", SALOME::INTERNAL_ERROR);
+ mIsTmp = false;
+
+ // Remove temporary files, created during study loading, together with directory
+ std::string strPath = multipr::getPath(strFile.c_str());
+#ifdef WNT
+ //std::string cmd_rm ("del /F \"");
+#else
+ std::string cmd_rm ("rm -rf \"");
+#endif
+ cmd_rm += strPath + "\"";
+ system(cmd_rm.c_str());
}
+ }
+ catch (multipr::RuntimeException& e)
+ {
+ e.dump(cout);
+ THROW_SALOME_CORBA_EXCEPTION("Unable to save MED file", SALOME::INTERNAL_ERROR);
+ }
- // Dump Python
- MULTIPR::TPythonDump(_engine) << this << ".save(\"" << pPath << "\")";
+ // Dump Python
+ MULTIPR::TPythonDump(_engine) << this << ".save(\"" << pPath << "\")";
}
-//void MULTIPR_Obj_i::setEngine (MULTIPR_ORB::MULTIPR_Gen_ptr theEngine)
+//-----------------------------------------------------------------------------
+// savePersistent and setEngine (for Persistence & Dump Python)
+//-----------------------------------------------------------------------------
+
+void MULTIPR_Obj_i::savePersistent (const char* pPath)
+ throw (SALOME::SALOME_Exception)
+{
+ if (mObj == NULL) THROW_SALOME_CORBA_EXCEPTION("No associated MED file", SALOME::INTERNAL_ERROR);
+
+ try
+ {
+ mObj->savePersistent(pPath);
+ }
+ catch (multipr::RuntimeException& e)
+ {
+ e.dump(cout);
+ THROW_SALOME_CORBA_EXCEPTION("Unable to save MED file", SALOME::INTERNAL_ERROR);
+ }
+}
+
void MULTIPR_Obj_i::setEngine (MULTIPR_Gen_i* theEngine)
{
- //_engine = MULTIPR_ORB::MULTIPR_Gen::_duplicate(theEngine);
_engine = theEngine;
}
// SALOMEDS::Driver methods (Persistence & Dump Python)
//-----------------------------------------------------------------------------
+/*! Get component data type
+ */
+char* MULTIPR_Gen_i::ComponentDataType()
+{
+ if(MYDEBUG) MESSAGE( "MULTIPR_Gen_i::ComponentDataType" );
+ return CORBA::string_dup( "MULTIPR" );
+}
+
+/*! Clears study-connected data when it is closed
+ */
+void MULTIPR_Gen_i::Close (SALOMEDS::SComponent_ptr theComponent)
+{
+ if(MYDEBUG) MESSAGE( "MULTIPR_Gen_i::Close" );
+
+ // Clear study contexts data
+ //_impl->Close(theComponent->GetStudy()->StudyId());
+
+ // Destroy all objects
+ SALOMEDS::Study_ptr aStudy = theComponent->GetStudy();
+ SALOMEDS::ChildIterator_ptr obj_it (aStudy->NewChildIterator(theComponent));
+ for (; obj_it->More(); obj_it->Next())
+ {
+ SALOMEDS::SObject_ptr aSObj = obj_it->Value();
+ CORBA::Object_var anObj = aSObj->GetObject();
+ MULTIPR_ORB::MULTIPR_Obj_ptr obj = MULTIPR_ORB::MULTIPR_Obj::_narrow(anObj);
+ if (!CORBA::is_nil(obj))
+ {
+ //obj->Destroy();
+ CORBA::Object_Helper::release(obj);
+ }
+ }
+}
+
/*! Save MULTIPR module's data
*/
SALOMEDS::TMPFile* MULTIPR_Gen_i::Save (SALOMEDS::SComponent_ptr theComponent,
// Store study contents as a set of python commands
SavePython(myCurrentStudy);
+ // Study name (for tmp directory and tmp files naming)
+ std::string aStudyName;
+ if (isMultiFile)
+ aStudyName = SALOMEDS_Tool::GetNameFromPath(myCurrentStudy->URL());
+
// Declare a byte stream
SALOMEDS::TMPFile_var aStreamFile;
if (CORBA::is_nil(obj))
return aStreamFile._retn();
+ // Obtain a temporary directory
+ std::string tmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
+
+ // Directory for MED data to be stored in a distributed MED file
+ std::string subDir = aStudyName + "_MULTIPR_MED";
+ std::string meshDir = tmpDir + subDir;
+
+ // Create a new dir <meshDir> to save the sequential/distributed file in.
+ // Remove all the files in <meshDir> if they exist.
+#ifdef WNT
+ std::string dirSeparator = "\\";
+ //std::string cmd_mk ("mkdir /F \"");
+ //std::string cmd_rm ("del /F \"");
+#else
+ std::string dirSeparator = "/";
+ std::string cmd_mk ("mkdir \"");
+ std::string cmd_rm ("rm -rf \"");
+#endif
+ cmd_mk += meshDir + "\"";
+ cmd_rm += meshDir + dirSeparator + "\"*";
+ system(cmd_mk.c_str());
+ system(cmd_rm.c_str());
+
+ // Create a sequence of files processed
+ SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
+
if (obj->isValidSequentialMEDFile())
{
- // TODO
+ std::string aFile = obj->getFilename();
+ std::string aFileName = multipr::getFilenameWithoutPath(aFile.c_str());
+
+ // Copy initial sequential MED file to <meshDir>
+#ifdef WNT
+ std::string dirSeparator = "\\";
+ //std::string cmd_cp ("copy \"");
+#else
+ std::string dirSeparator = "/";
+ std::string cmd_cp ("cp \"");
+#endif
+ cmd_cp += aFile + "\" \"" + meshDir + dirSeparator + "\"";
+ system(cmd_cp.c_str());
+
+ // Set names of temporary files
+ std::string aFileNameRel = subDir + dirSeparator + aFileName;
+ aFileSeq->length(1);
+ aFileSeq[0] = CORBA::string_dup(aFileNameRel.c_str()); // sequential MED file
}
else if (obj->isValidDistributedMEDFile())
{
- /*
- // Obtain a temporary dir
- std::string tmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
+ // Save distributed MED file to the <meshDir>
+ PortableServer::Servant aServant = _poa->reference_to_servant(obj);
+ MULTIPR_Obj_i* objServant = dynamic_cast<MULTIPR_Obj_i*>(aServant);
+ if (!objServant)
+ {
+ // TODO: exception
+ return aStreamFile._retn();
+ }
+ objServant->savePersistent(meshDir.c_str());
+
+ // ASCII master file name
+ std::string aMaitreFile = obj->getFilename();
+ std::string aMaitreFileName = multipr::getFilenameWithoutPath(aMaitreFile.c_str());
+ // just after partitionneDomaine() the state is MULTIPR_OBJ_STATE_DIS_MEM,
+ // and getFilename() returns name of sequential file
+ // (because distributed file is not created on disk yet). So, build the name:
+ if (aMaitreFile == obj->getSeqFilename())
+ {
+ std::string strExtension (".med");
+ std::string strNamePrefix =
+ multipr::removeExtension(aMaitreFileName.c_str(), strExtension.c_str());
+ aMaitreFileName = strNamePrefix + "_grains_maitre" + strExtension;
+ }
+ //std::string aMaitreFileName = SALOMEDS_Tool::GetNameFromPath(aMaitreFile);
+ std::string aMaitreFileNameRel = subDir + dirSeparator + aMaitreFileName;
- // Create a sequence of files processed
- SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
+ // Set names of temporary files
+ MULTIPR_ORB::string_array* listParts = obj->getParts();
+ unsigned int nbParts = listParts->length();
+ aFileSeq->length(nbParts + 1);
- std::string aStudyName;
- if (isMultiFile)
- aStudyName = SALOMEDS_Tool::GetNameFromPath(myCurrentStudy->URL());
+ char lMeshName[256];
+ int lId;
+ char lPartName[256];
+ char lPath[256];
+ char lMEDFileName[256];
- // ?? create a new dir in <tmpDir> for all parts of distributed file
- // or it will be created automatically ??
- obj->save(tmpDir.c_str());
+ for (unsigned int i = 0; i < nbParts; i++) // each part MED files
+ {
+ const char* strPartName = (*listParts)[i];
+ char* strPartInfo = obj->getPartInfo(strPartName);
- // TODO: set real length
- aFileSeq->length(1); // nb.distributed files + 1 (_maitre.med)
+ // parse infos
+ int ret = sscanf(strPartInfo, "%s %d %s %s %s",
+ lMeshName, &lId, lPartName, lPath, lMEDFileName);
- // Set names of temporary files
- std::string meshdir =
- aStudyName + "_MULTIPR_Mesh"; // for MED data to be stored in distributed MED file
- //std::string filename = aStudyName + "_MULTIPR.hdf"; // for MULTIPR data itself
-
- aFileSeq[0] = CORBA::string_dup(meshdir.c_str());
- //aFileSeq[1] = CORBA::string_dup(meshfile.ToCString());
- meshdir = tmpDir + meshfile;
- //filename = tmpDir + filename;
- //*/
-
- //Remove the files if they exist
- //#ifndef WNT
- // TCollection_AsciiString cmd ("rm -f \"");
- //#else
- // TCollection_AsciiString cmd ("del /F \"");
- //#endif
-
- //cmd+=filename;
- //cmd+="\" \"";
- //cmd+=meshfile;
- //cmd+="\"";
- //system(cmd.ToCString());
-
- // Convert temporary files to stream
- //aStreamFile = SALOMEDS_Tool::PutFilesToStream(tmpDir.ToCString(), aFileSeq.in(), isMultiFile);
-
- // Remove temporary files and directory
- //if (!isMultiFile)
- // SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(), true);
+ if (ret != 5)
+ {
+ // TODO: exception
+ return aStreamFile._retn();
+ }
+
+ std::string aPartFileName = multipr::getFilenameWithoutPath(lMEDFileName);
+ std::string aPartFileNameRel = subDir + dirSeparator + aPartFileName;
+
+ aFileSeq[i] = CORBA::string_dup(aPartFileNameRel.c_str()); // part MED file
+ }
+ aFileSeq[nbParts] = CORBA::string_dup(aMaitreFileNameRel.c_str()); // ASCII master file
}
else
{
- // ??
+ // TODO: exception
+ return aStreamFile._retn();
+ }
+
+ // Convert temporary files to stream
+ aStreamFile = SALOMEDS_Tool::PutFilesToStream(tmpDir, aFileSeq.in(), isMultiFile);
+
+ // Remove temporary files and directory
+ if (!isMultiFile)
+ {
+ //SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir, aFileSeq.in(), true);
+ // remove with shell command, because SALOMEDS_Tool::RemoveTemporaryFiles does not remove sub-folders
+#ifdef WNT
+ //std::string cmd_rm ("del /F \"");
+#else
+ std::string cmd_rm ("rm -rf \"");
+#endif
+ cmd_rm += tmpDir + "\"";
+ system(cmd_rm.c_str());
}
INFOS("MULTIPR_Gen_i::Save() completed");
SetCurrentStudy(theComponent->GetStudy());
// Get temporary files location
- //TCollection_AsciiString tmpDir =
- // isMultiFile ? TCollection_AsciiString((char*)theURL) : (char*)SALOMEDS_Tool::GetTmpDir().c_str();
+ std::string tmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
INFOS("THE URL++++++++++++++");
INFOS(theURL);
- //INFOS("THE TMP PATH+++++++++");
- //INFOS(tmpDir);
+ INFOS("THE TMP PATH+++++++++");
+ INFOS(tmpDir.c_str());
+
+ // For LocalPersistentIDToIOR():
+ myTmpDir = tmpDir;
+
+ // Create a new dir <meshDir> to restore the distributed/sequential
+ // MED file in. It is needed only if not multifile, because in
+ // multifile study all required files are already on disk.
+ if (!isMultiFile)
+ {
+ std::string subDir = "_MULTIPR_MED";
+ std::string meshDir = tmpDir + subDir;
+#ifdef WNT
+ //std::string cmd_mk ("mkdir /F \"");
+#else
+ std::string cmd_mk ("mkdir \"");
+#endif
+ cmd_mk += meshDir + "\"";
+ system(cmd_mk.c_str());
+ }
// Convert the stream into sequence of files to process
- //SALOMEDS::ListOfFileNames_var aFileSeq =
- // SALOMEDS_Tool::PutStreamToFiles(theStream, tmpDir.ToCString(), isMultiFile);
+ SALOMEDS::ListOfFileNames_var aFileSeq =
+ SALOMEDS_Tool::PutStreamToFiles(theStream, tmpDir.c_str(), isMultiFile);
//TCollection_AsciiString aStudyName;
//if (isMultiFile)
return Load(theComponent, theStream, theURL, isMultiFile);
}
-/*! Clears study-connected data when it is closed
- */
-void MULTIPR_Gen_i::Close (SALOMEDS::SComponent_ptr theComponent)
-{
- if(MYDEBUG) MESSAGE( "MULTIPR_Gen_i::Close" );
-
- // Clear study contexts data
- //_impl->Close(theComponent->GetStudy()->StudyId());
-}
-
-/*! Get component data type
- */
-char* MULTIPR_Gen_i::ComponentDataType()
-{
- if(MYDEBUG) MESSAGE( "MULTIPR_Gen_i::ComponentDataType" );
- return CORBA::string_dup( "MULTIPR" );
-}
-
/*! Transform data from transient form to persistent
*/
char* MULTIPR_Gen_i::IORToLocalPersistentID (SALOMEDS::SObject_ptr /*theSObject*/,
const char* IORString,
- CORBA::Boolean /*isMultiFile*/,
+ CORBA::Boolean isMultiFile,
CORBA::Boolean /*isASCII*/ )
{
if(MYDEBUG) MESSAGE("MULTIPR_Gen_i::IORToLocalPersistentID");
- MULTIPR_ORB::MULTIPR_Obj_ptr anObject =
+ MULTIPR_ORB::MULTIPR_Obj_ptr obj =
MULTIPR_ORB::MULTIPR_Obj::_narrow(_orb->string_to_object(IORString));
- if (!CORBA::is_nil(anObject))
+ if (!CORBA::is_nil(obj))
{
- if(MYDEBUG) MESSAGE("MULTIPR_Gen_i::IORToLocalPersistentID: id = " << anObject->getFilename());
- return CORBA::string_dup(anObject->getFilename());
+ std::string aStudyName;
+ if (isMultiFile)
+ aStudyName = SALOMEDS_Tool::GetNameFromPath(myCurrentStudy->URL());
+
+ std::string subDir = aStudyName + "_MULTIPR_MED";
+
+ std::string aFile = obj->getFilename();
+ std::string aFileName = multipr::getFilenameWithoutPath(aFile.c_str());
+
+ // Get servant (to call methods, not present in IDL interface
+ PortableServer::Servant aServant = _poa->reference_to_servant(obj);
+ MULTIPR_Obj_i* objServant = dynamic_cast<MULTIPR_Obj_i*>(aServant);
+ if (!objServant)
+ {
+ // TODO: exception
+ return CORBA::string_dup("");
+ }
+
+ std::string strSeparator ("|");
+ if (obj->isValidSequentialMEDFile())
+ {
+ // Save Boxing
+ char strBoxing[32];
+ sprintf(strBoxing, "%d", objServant->getBoxing());
+ aFileName += strSeparator + strBoxing;
+
+ // Save Mesh Name
+ std::string aMeshName = objServant->getMeshName();
+ if (!aMeshName.empty())
+ {
+ aFileName += strSeparator + aMeshName;
+ }
+ }
+ else if (obj->isValidDistributedMEDFile())
+ {
+ // just after partitionneDomaine() the state is MULTIPR_OBJ_STATE_DIS_MEM,
+ // and getFilename() returns name of sequential file
+ // (because distributed file is not created on disk yet). So, build the name:
+ if (aFile == obj->getSeqFilename())
+ {
+ std::string strExtension (".med");
+ std::string strNamePrefix =
+ multipr::removeExtension(aFileName.c_str(), strExtension.c_str());
+ aFileName = strNamePrefix + "_grains_maitre" + strExtension;
+ }
+
+ // Save Boxing
+ char strBoxing[32];
+ sprintf(strBoxing, "%d", objServant->getBoxing());
+ aFileName += strSeparator + strBoxing;
+ }
+ else
+ {
+ // TODO: exception
+ return CORBA::string_dup("");
+ }
+
+ // PersistentID will be a relative path to MED file (relatively tmp dir)
+ // plus additianal parameters, separated by '|' (see above)
+#ifdef WNT
+ std::string dirSeparator = "\\";
+#else
+ std::string dirSeparator = "/";
+#endif
+ aFileName = subDir + dirSeparator + aFileName;
+
+ if(MYDEBUG) MESSAGE("MULTIPR_Gen_i::IORToLocalPersistentID: id = " << aFileName.c_str());
+
+ return CORBA::string_dup(aFileName.c_str());
}
return CORBA::string_dup("");
}
*/
char* MULTIPR_Gen_i::LocalPersistentIDToIOR (SALOMEDS::SObject_ptr /*theSObject*/,
const char* aLocalPersistentID,
- CORBA::Boolean /*isMultiFile*/,
+ CORBA::Boolean isMultiFile,
CORBA::Boolean /*isASCII*/)
{
if(MYDEBUG) MESSAGE("MULTIPR_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID);
if (strlen(aLocalPersistentID) > 0)
{
- //string medFilename (myTmpDir);
- //medFilename += "/";
- string medFilename ("/tmp/");
- medFilename += aLocalPersistentID;
- MULTIPR_ORB::MULTIPR_Obj_ptr anObj = getObject(medFilename.c_str());
+ // Extract relative path
+ std::string strLocalPersistentID (aLocalPersistentID);
+ int nb = strLocalPersistentID.find("|");
+ if (nb < 1) nb = strLocalPersistentID.size();
+ if (nb < 1)
+ {
+ // TODO: exception
+ return CORBA::string_dup("");
+ }
+ std::string strRelativePath = strLocalPersistentID.substr(0, nb);
+ strLocalPersistentID.erase(0, nb); // erase <strRelativePath>
+ if (strLocalPersistentID.size() > 0) strLocalPersistentID.erase(0, 1); // erase "|"
+
+ // the only kind of available objects is a MULTIPR_ORB::MULTIPR_Obj,
+ // representing a sequential or a distributed MED file.
+ std::string medFilename = myTmpDir + strRelativePath; // myTmpDir already contains dir separator
+
+ // create MULTIPR_Obj from file
+ //MULTIPR_ORB::MULTIPR_Obj_ptr obj = getObject(medFilename.c_str());
+ MULTIPR_Obj_i* obj_i = new MULTIPR_Obj_i (medFilename.c_str(),
+ /*isPersistence = */true,
+ isMultiFile);
+ obj_i->setEngine(this);
+ MULTIPR_ORB::MULTIPR_Obj_ptr obj = obj_i->POA_MULTIPR_ORB::MULTIPR_Obj::_this();
+
+ // Set boxing and mesh name, if provided
+ nb = strLocalPersistentID.find("|");
+ if (nb < 1) nb = strLocalPersistentID.size();
+ if (nb > 0)
+ {
+ std::string strBoxing = strLocalPersistentID.substr(0, nb);
+ strLocalPersistentID.erase(0, nb); // erase <strBoxing>
+ if (strLocalPersistentID.size() > 0) strLocalPersistentID.erase(0, 1); // erase "|"
+ int aBoxing = atoi(strBoxing.c_str());
+ obj->setBoxing(aBoxing);
+ }
+
+ if (obj->isValidSequentialMEDFile())
+ {
+ nb = strLocalPersistentID.size();
+ if (nb > 0)
+ {
+ std::string strMeshName = strLocalPersistentID.substr(0, nb);
+ obj->setMesh(strMeshName.c_str());
+ // we do not cut <strLocalPersistentID> here,
+ // because we do not expect to find in it anything
+ }
+ }
- CORBA::String_var anIORString = _orb->object_to_string(anObj);
+ // get IOR string
+ CORBA::String_var anIORString = _orb->object_to_string(obj);
return CORBA::string_dup(anIORString);
}
- return CORBA::string_dup( "" );
+ return CORBA::string_dup("");
}
/*! Transform data from persistent form to transient