1 // Copyright (C) 2021-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "KernelBasis.hxx"
25 static bool DEFAULT_SSL_MODE = true;
26 static bool GUI_MODE = false;
28 // IOR of SALOME_Embedded_NamingService servant
29 static std::string IOR_OF_EMBEDDED_NS;
33 return DEFAULT_SSL_MODE;
36 void setSSLMode(bool sslMode)
38 DEFAULT_SSL_MODE = sslMode;
46 void setGUIMode(bool guiMode)
51 std::string getIOROfEmbeddedNS()
53 return IOR_OF_EMBEDDED_NS;
56 void setIOROfEmbeddedNS(const std::string& ior)
58 IOR_OF_EMBEDDED_NS = ior;
64 * Callable from Python in case if sys.stdout is not connected to tty
66 void WriteInStdout(const std::string& msg)
68 std::cout << msg << std::endl << std::flush;
72 * Callable from Python in case if sys.stdout is not connected to tty
74 void WriteInStderr(const std::string& msg)
76 std::cerr << msg << std::endl << std::flush;
81 static constexpr char IN_PROCESS_VALUE = 0;
82 static constexpr char IN_PROCESS_VALUE_STR[] = "InProcess";
83 static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE = 1;
84 static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE_STR[] = "OutOfProcessNoReplay";
85 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE = 2;
86 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR[] = "OutOfProcessWithReplay";
87 static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE = 3;
88 static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR[] = "OutOfProcessNoReplayFT";
89 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE = 4;
90 static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR[] = "OutOfProcessWithReplayFT";
92 static PyExecutionMode FromIntToPyExecutionMode(char value)
96 case IN_PROCESS_VALUE:
97 return PyExecutionMode::InProcess;
98 case OUT_OF_PROCESS_NO_REPLAY_VALUE:
99 return PyExecutionMode::OutOfProcessNoReplay;
100 case OUT_OF_PROCESS_WITH_REPLAY_VALUE:
101 return PyExecutionMode::OutOfProcessWithReplay;
102 case OUT_OF_PROCESS_NO_REPLAY_FT_VALUE:
103 return PyExecutionMode::OutOfProcessNoReplayFT;
104 case OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE:
105 return PyExecutionMode::OutOfProcessWithReplayFT;
107 throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
110 static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
112 if(value == IN_PROCESS_VALUE_STR)
113 return PyExecutionMode::InProcess;
114 if(value == OUT_OF_PROCESS_NO_REPLAY_VALUE_STR)
115 return PyExecutionMode::OutOfProcessNoReplay;
116 if(value == OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR)
117 return PyExecutionMode::OutOfProcessWithReplay;
118 if(value == OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR)
119 return PyExecutionMode::OutOfProcessNoReplayFT;
120 if(value == OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR)
121 return PyExecutionMode::OutOfProcessWithReplayFT;
122 throw std::range_error("FromStrToPyExecutionMode : Invalid str value for py execution mode !");
125 static std::string FromExecutionModeToStr(PyExecutionMode execMode)
129 case PyExecutionMode::InProcess:
130 return IN_PROCESS_VALUE_STR;
131 case PyExecutionMode::OutOfProcessNoReplay:
132 return OUT_OF_PROCESS_NO_REPLAY_VALUE_STR;
133 case PyExecutionMode::OutOfProcessWithReplay:
134 return OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR;
135 case PyExecutionMode::OutOfProcessNoReplayFT:
136 return OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR;
137 case PyExecutionMode::OutOfProcessWithReplayFT:
138 return OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR;
140 throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
145 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
147 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
149 std::string SALOME_REPLAY_FILES_DIR;
151 int SALOME::GetBigObjOnDiskThreshold()
153 return SALOME_BIG_OBJ_ON_DISK_THRES;
156 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
158 SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
161 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
163 static std::string SALOME_FILE_BIG_OBJ_DIR;
165 constexpr int DFT_SALOME_NB_RETRY = 1;
167 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
169 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
173 case SALOME::SHARED_FILE_SYSTEM_PROTOCOL:
174 return SALOME::BigObjTransferProtocol::SharedFileSystem;
175 case SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL:
176 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
178 throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
182 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
184 if( protocol == SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR )
185 return SALOME::BigObjTransferProtocol::SharedFileSystem;
186 if( protocol == SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR )
187 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
188 throw std::runtime_error("BigObjOnDiskProtocolFromStr unrecognized protocol !");
191 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
195 case SALOME::BigObjTransferProtocol::SharedFileSystem:
196 return SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR;
197 case SALOME::BigObjTransferProtocol::SSDCopyFileSystem:
198 return SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR;
200 throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
205 * This method returns the protocol of proxy transfert and the directory
207 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
209 if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
211 directory = SALOME_FILE_BIG_OBJ_DIR;
212 return SALOME::BigObjTransferProtocol::SharedFileSystem;
214 std::string protocol = SALOME_FILE_BIG_OBJ_DIR.substr(0,3);
215 directory = SALOME_FILE_BIG_OBJ_DIR.substr(3);
216 if( protocol[0]!=SALOME_FILE_BIG_OBJ_DIR_SEP || protocol[2]!=SALOME_FILE_BIG_OBJ_DIR_SEP)
218 directory = SALOME_FILE_BIG_OBJ_DIR;
219 return SALOME::BigObjTransferProtocol::SharedFileSystem;
221 std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
222 short iproxyprot = 0;
224 return FromIntToBigObjOnDiskProtocol( iproxyprot );
227 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
229 return SALOME_FILE_BIG_OBJ_DIR;
232 std::string SALOME::GetDirectoryForReplayFiles()
234 return SALOME_REPLAY_FILES_DIR;
237 void SALOME::SetDirectoryForReplayFiles(const std::string& directory)
239 SALOME_REPLAY_FILES_DIR = directory;
242 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
244 SALOME_FILE_BIG_OBJ_DIR = directory;
247 bool SALOME::BigObjOnDiskDirectoryDefined()
249 return ! SALOME_FILE_BIG_OBJ_DIR.empty();
252 void SALOME::SetNumberOfRetry(int nbRetry)
254 SALOME_NB_RETRY = nbRetry;
257 int SALOME::GetNumberOfRetry()
259 return SALOME_NB_RETRY;
262 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
264 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
266 DefaultPyExecMode = mode;
269 void SALOME::SetPyExecutionModeStr(const std::string& mode)
271 SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
274 std::vector<std::string> SALOME::GetAllPyExecutionModes()
276 return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
279 std::string SALOME::GetPyExecutionModeStr()
281 return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
284 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
286 auto isEnvVarSet = []() -> SALOME::PyExecutionMode
288 const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
289 if (envVar && (envVar[0] != '\0'))
291 const int numValue = std::stoi(envVar);
292 return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
294 return SALOME::PyExecutionMode::InProcess;
296 if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
297 DefaultPyExecMode = isEnvVarSet();
298 return DefaultPyExecMode;