Salome HOME
[EDF30399] : Steer directory hosting replay files
[modules/kernel.git] / src / Basics / KernelBasis.cxx
1 // Copyright (C) 2021-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "KernelBasis.hxx"
21
22 #include <sstream>
23 #include <stdexcept>
24
25 static bool DEFAULT_SSL_MODE = true;
26 static bool GUI_MODE = false;
27
28 // IOR of SALOME_Embedded_NamingService servant
29 static std::string IOR_OF_EMBEDDED_NS;
30
31 bool getSSLMode()
32 {
33   return DEFAULT_SSL_MODE;
34 }
35
36 void setSSLMode(bool sslMode)
37 {
38   DEFAULT_SSL_MODE = sslMode;
39 }
40
41 bool getGUIMode()
42 {
43   return GUI_MODE;
44 }
45
46 void setGUIMode(bool guiMode)
47 {
48   GUI_MODE = guiMode;
49 }
50
51 std::string getIOROfEmbeddedNS()
52 {
53   return IOR_OF_EMBEDDED_NS;
54 }
55
56 void setIOROfEmbeddedNS(const std::string& ior)
57 {
58   IOR_OF_EMBEDDED_NS = ior;
59 }
60
61 #include <iostream>
62
63 /*!
64  * Callable from Python in case if sys.stdout is not connected to tty
65  */
66 void WriteInStdout(const std::string& msg)
67 {
68   std::cout << msg << std::endl << std::flush;
69 }
70
71 /*!
72  * Callable from Python in case if sys.stdout is not connected to tty
73  */
74 void WriteInStderr(const std::string& msg)
75 {
76   std::cerr << msg << std::endl << std::flush;
77 }
78
79 namespace SALOME
80 {
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";
91
92   static PyExecutionMode FromIntToPyExecutionMode(char value)
93   {
94     switch(value)
95     {
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;
106     }
107     throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
108   }
109   
110   static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
111   {
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 !");
123   }
124
125   static std::string FromExecutionModeToStr(PyExecutionMode execMode)
126   {
127     switch(execMode)
128     {
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;
139       default:
140         throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
141     }
142   }
143 }
144
145 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
146
147 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
148
149 std::string SALOME_REPLAY_FILES_DIR;
150
151 int SALOME::GetBigObjOnDiskThreshold()
152 {
153   return SALOME_BIG_OBJ_ON_DISK_THRES;
154 }
155
156 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
157 {
158   SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
159 }
160
161 constexpr char SALOME_FILE_BIG_OBJ_DIR_SEP = '@';
162
163 static std::string SALOME_FILE_BIG_OBJ_DIR;
164
165 constexpr int DFT_SALOME_NB_RETRY = 1;
166
167 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
168
169 SALOME::BigObjTransferProtocol SALOME::FromIntToBigObjOnDiskProtocol(char protocol)
170 {
171   switch( protocol )
172   {
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;
177     default:
178       throw std::runtime_error("FromIntToBigObjOnDiskProtocol unrecognized protocol ! should be in [0,1] !");
179   }
180 }
181
182 SALOME::BigObjTransferProtocol SALOME::BigObjOnDiskProtocolFromStr(const std::string& protocol)
183 {
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 !");
189 }
190
191 std::string SALOME::BigObjOnDiskProtocolToStr(BigObjTransferProtocol protocol)
192 {
193   switch( protocol )
194   {
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;
199     default:
200       throw std::runtime_error("BigObjOnDiskProtocolToStr unrecognized protocol ! should be in [0,1] !");
201   }
202 }
203
204 /*!
205  * This method returns the protocol of proxy transfert and the directory
206  */
207 SALOME::BigObjTransferProtocol SALOME::GetBigObjOnDiskProtocolAndDirectory(std::string& directory)
208 {
209   if(SALOME_FILE_BIG_OBJ_DIR.size() < 3)
210   {
211     directory = SALOME_FILE_BIG_OBJ_DIR;
212     return SALOME::BigObjTransferProtocol::SharedFileSystem;
213   }
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)
217   {
218     directory = SALOME_FILE_BIG_OBJ_DIR;
219     return SALOME::BigObjTransferProtocol::SharedFileSystem;
220   }
221   std::istringstream iss(protocol.substr(1,1)); iss.exceptions(std::istringstream::failbit | std::istringstream::badbit);
222   short iproxyprot = 0;
223   iss >> iproxyprot;
224   return FromIntToBigObjOnDiskProtocol( iproxyprot );
225 }
226
227 std::string SALOME::GetBigObjOnDiskDirectoryCoarse()
228 {
229   return SALOME_FILE_BIG_OBJ_DIR;
230 }
231
232 std::string SALOME::GetDirectoryForReplayFiles()
233 {
234   return SALOME_REPLAY_FILES_DIR;
235 }
236
237 void SALOME::SetDirectoryForReplayFiles(const std::string& directory)
238 {
239   SALOME_REPLAY_FILES_DIR = directory;
240 }
241
242 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
243 {
244   SALOME_FILE_BIG_OBJ_DIR = directory;
245 }
246
247 bool SALOME::BigObjOnDiskDirectoryDefined()
248 {
249   return ! SALOME_FILE_BIG_OBJ_DIR.empty();
250 }
251
252 void SALOME::SetNumberOfRetry(int nbRetry)
253 {
254   SALOME_NB_RETRY = nbRetry;
255 }
256
257 int SALOME::GetNumberOfRetry()
258 {
259   return SALOME_NB_RETRY;
260 }
261
262 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
263
264 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
265 {
266   DefaultPyExecMode = mode;
267 }
268
269 void SALOME::SetPyExecutionModeStr(const std::string& mode)
270 {
271   SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
272 }
273
274 std::vector<std::string> SALOME::GetAllPyExecutionModes()
275 {
276   return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
277 }
278
279 std::string SALOME::GetPyExecutionModeStr()
280 {
281   return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
282 }
283
284 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
285 {
286   auto isEnvVarSet = []() -> SALOME::PyExecutionMode
287   {
288     const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
289     if (envVar && (envVar[0] != '\0'))
290     {
291       const int numValue = std::stoi(envVar);
292       return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
293     }
294     return SALOME::PyExecutionMode::InProcess;
295   };
296   if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
297     DefaultPyExecMode = isEnvVarSet();
298   return DefaultPyExecMode;
299 }