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 int SALOME::GetBigObjOnDiskThreshold()
151 return SALOME_BIG_OBJ_ON_DISK_THRES;
154 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
156 SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
159 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
161 static std::string SALOME_FILE_BIG_OBJ_DIR;
163 constexpr int DFT_SALOME_NB_RETRY = 1;
165 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
167 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
171 case SALOME::SHARED_FILE_SYSTEM_PROTOCOL:
172 return SALOME::BigObjTransferProtocol::SharedFileSystem;
173 case SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL:
174 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
176 throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
180 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
182 if( protocol == SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR )
183 return SALOME::BigObjTransferProtocol::SharedFileSystem;
184 if( protocol == SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR )
185 return SALOME::BigObjTransferProtocol::SSDCopyFileSystem;
186 throw std::runtime_error("BigObjOnDiskProtocolFromStr unrecognized protocol !");
189 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
193 case SALOME::BigObjTransferProtocol::SharedFileSystem:
194 return SALOME::SHARED_FILE_SYSTEM_PROTOCOL_STR;
195 case SALOME::BigObjTransferProtocol::SSDCopyFileSystem:
196 return SALOME::SSD_COPY_FILE_SYSTEM_PROTOCOL_STR;
198 throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
203 * This method returns the protocol of proxy transfert and the directory
205 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
207 if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
209 directory = SALOME_FILE_BIG_OBJ_DIR;
210 return SALOME::BigObjTransferProtocol::SharedFileSystem;
212 std::string protocol = SALOME_FILE_BIG_OBJ_DIR.substr(0,3);
213 directory = SALOME_FILE_BIG_OBJ_DIR.substr(3);
214 if( protocol[0]!=SALOME_FILE_BIG_OBJ_DIR_SEP || protocol[2]!=SALOME_FILE_BIG_OBJ_DIR_SEP)
216 directory = SALOME_FILE_BIG_OBJ_DIR;
217 return SALOME::BigObjTransferProtocol::SharedFileSystem;
219 std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
220 short iproxyprot = 0;
222 return FromIntToBigObjOnDiskProtocol( iproxyprot );
225 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
227 return SALOME_FILE_BIG_OBJ_DIR;
230 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
232 SALOME_FILE_BIG_OBJ_DIR = directory;
235 bool SALOME::BigObjOnDiskDirectoryDefined()
237 return ! SALOME_FILE_BIG_OBJ_DIR.empty();
240 void SALOME::SetNumberOfRetry(int nbRetry)
242 SALOME_NB_RETRY = nbRetry;
245 int SALOME::GetNumberOfRetry()
247 return SALOME_NB_RETRY;
250 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
252 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
254 DefaultPyExecMode = mode;
257 void SALOME::SetPyExecutionModeStr(const std::string& mode)
259 SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
262 std::vector<std::string> SALOME::GetAllPyExecutionModes()
264 return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
267 std::string SALOME::GetPyExecutionModeStr()
269 return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
272 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
274 auto isEnvVarSet = []() -> SALOME::PyExecutionMode
276 const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
277 if (envVar && (envVar[0] != '\0'))
279 const int numValue = std::stoi(envVar);
280 return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
282 return SALOME::PyExecutionMode::InProcess;
284 if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
285 DefaultPyExecMode = isEnvVarSet();
286 return DefaultPyExecMode;