Salome HOME
[EDF30062] [EDF29150]: Additional fault tolerant mecanism
[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 static bool DEFAULT_SSL_MODE = true;
23 static bool GUI_MODE = false;
24
25 // IOR of SALOME_Embedded_NamingService servant
26 static std::string IOR_OF_EMBEDDED_NS;
27
28 bool getSSLMode()
29 {
30   return DEFAULT_SSL_MODE;
31 }
32
33 void setSSLMode(bool sslMode)
34 {
35   DEFAULT_SSL_MODE = sslMode;
36 }
37
38 bool getGUIMode()
39 {
40   return GUI_MODE;
41 }
42
43 void setGUIMode(bool guiMode)
44 {
45   GUI_MODE = guiMode;
46 }
47
48 std::string getIOROfEmbeddedNS()
49 {
50   return IOR_OF_EMBEDDED_NS;
51 }
52
53 void setIOROfEmbeddedNS(const std::string& ior)
54 {
55   IOR_OF_EMBEDDED_NS = ior;
56 }
57
58 #include <iostream>
59
60 /*!
61  * Callable from Python in case if sys.stdout is not connected to tty
62  */
63 void WriteInStdout(const std::string& msg)
64 {
65   std::cout << msg << std::endl << std::flush;
66 }
67
68 /*!
69  * Callable from Python in case if sys.stdout is not connected to tty
70  */
71 void WriteInStderr(const std::string& msg)
72 {
73   std::cerr << msg << std::endl << std::flush;
74 }
75
76 namespace SALOME
77 {
78   static constexpr char IN_PROCESS_VALUE = 0;
79   static constexpr char IN_PROCESS_VALUE_STR[] = "InProcess";
80   static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE = 1;
81   static constexpr char OUT_OF_PROCESS_NO_REPLAY_VALUE_STR[] = "OutOfProcessNoReplay";
82   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE = 2;
83   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR[] = "OutOfProcessWithReplay";
84   static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE = 3;
85   static constexpr char OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR[] = "OutOfProcessNoReplayFT";
86   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE = 4;
87   static constexpr char OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR[] = "OutOfProcessWithReplayFT";
88
89   static PyExecutionMode FromIntToPyExecutionMode(char value)
90   {
91     switch(value)
92     {
93       case IN_PROCESS_VALUE:
94         return PyExecutionMode::InProcess;
95       case OUT_OF_PROCESS_NO_REPLAY_VALUE:
96         return PyExecutionMode::OutOfProcessNoReplay;
97       case OUT_OF_PROCESS_WITH_REPLAY_VALUE:
98         return PyExecutionMode::OutOfProcessWithReplay;
99       case OUT_OF_PROCESS_NO_REPLAY_FT_VALUE:
100         return PyExecutionMode::OutOfProcessNoReplayFT;
101       case OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE:
102         return PyExecutionMode::OutOfProcessWithReplayFT;
103     }
104     throw std::range_error("FromIntToPyExecutionMode : Invalid value for Py Execution Mode ! Must be in 0 (InProcess), 1 (OutOfProcessNoReplay) or 2 (OutOfProcessWithReplay) !");
105   }
106   
107   static PyExecutionMode FromStrToPyExecutionMode(const std::string& value)
108   {
109     if(value == IN_PROCESS_VALUE_STR)
110       return PyExecutionMode::InProcess;
111     if(value == OUT_OF_PROCESS_NO_REPLAY_VALUE_STR)
112       return PyExecutionMode::OutOfProcessNoReplay;
113     if(value == OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR)
114       return PyExecutionMode::OutOfProcessWithReplay;
115     if(value == OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR)
116       return PyExecutionMode::OutOfProcessNoReplayFT;
117     if(value == OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR)
118       return PyExecutionMode::OutOfProcessWithReplayFT;
119     throw std::range_error("FromStrToPyExecutionMode : Invalid str value for py execution mode !");
120   }
121
122   static std::string FromExecutionModeToStr(PyExecutionMode execMode)
123   {
124     switch(execMode)
125     {
126       case PyExecutionMode::InProcess:
127         return IN_PROCESS_VALUE_STR;
128       case PyExecutionMode::OutOfProcessNoReplay:
129         return OUT_OF_PROCESS_NO_REPLAY_VALUE_STR;
130       case PyExecutionMode::OutOfProcessWithReplay:
131         return OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR;
132       case PyExecutionMode::OutOfProcessNoReplayFT:
133         return OUT_OF_PROCESS_NO_REPLAY_FT_VALUE_STR;
134       case PyExecutionMode::OutOfProcessWithReplayFT:
135         return OUT_OF_PROCESS_WITH_REPLAY_FT_VALUE_STR;
136       default:
137         throw std::range_error("FromExecutionModeToStr : Invalid str value for py execution mode !");
138     }
139   }
140 }
141
142 constexpr int SALOME_BIG_OBJ_ON_DISK_THRES_DFT = 50000000;
143
144 static int SALOME_BIG_OBJ_ON_DISK_THRES = SALOME_BIG_OBJ_ON_DISK_THRES_DFT;
145
146 int SALOME::GetBigObjOnDiskThreshold()
147 {
148   return SALOME_BIG_OBJ_ON_DISK_THRES;
149 }
150
151 void SALOME::SetBigObjOnDiskThreshold(int newThresholdInByte)
152 {
153   SALOME_BIG_OBJ_ON_DISK_THRES = newThresholdInByte;
154 }
155
156 static std::string SALOME_FILE_BIG_OBJ_DIR;
157
158 constexpr int DFT_SALOME_NB_RETRY = 1;
159
160 static int SALOME_NB_RETRY = DFT_SALOME_NB_RETRY;
161
162 std::string SALOME::GetBigObjOnDiskDirectory()
163 {
164   return SALOME_FILE_BIG_OBJ_DIR;
165 }
166
167 void SALOME::SetBigObjOnDiskDirectory(const std::string& directory)
168 {
169   SALOME_FILE_BIG_OBJ_DIR = directory;
170 }
171
172 bool SALOME::BigObjOnDiskDirectoryDefined()
173 {
174   return ! SALOME_FILE_BIG_OBJ_DIR.empty();
175 }
176
177 void SALOME::SetNumberOfRetry(int nbRetry)
178 {
179   SALOME_NB_RETRY = nbRetry;
180 }
181
182 int SALOME::GetNumberOfRetry()
183 {
184   return SALOME_NB_RETRY;
185 }
186
187 static SALOME::PyExecutionMode DefaultPyExecMode = SALOME::PyExecutionMode::NotSet;
188
189 void SALOME::SetPyExecutionMode(PyExecutionMode mode)
190 {
191   DefaultPyExecMode = mode;
192 }
193
194 void SALOME::SetPyExecutionModeStr(const std::string& mode)
195 {
196   SALOME::SetPyExecutionMode( SALOME::FromStrToPyExecutionMode(mode) );
197 }
198
199 std::vector<std::string> SALOME::GetAllPyExecutionModes()
200 {
201   return {IN_PROCESS_VALUE_STR,OUT_OF_PROCESS_NO_REPLAY_VALUE_STR,OUT_OF_PROCESS_WITH_REPLAY_VALUE_STR};
202 }
203
204 std::string SALOME::GetPyExecutionModeStr()
205 {
206   return SALOME::FromExecutionModeToStr( SALOME::GetPyExecutionMode() );
207 }
208
209 SALOME::PyExecutionMode SALOME::GetPyExecutionMode()
210 {
211   auto isEnvVarSet = []() -> SALOME::PyExecutionMode
212   {
213     const char *envVar = std::getenv("SALOME_PY_EXECUTION_MODE");
214     if (envVar && (envVar[0] != '\0'))
215     {
216       const int numValue = std::stoi(envVar);
217       return SALOME::FromIntToPyExecutionMode( static_cast<char>(numValue) );
218     }
219     return SALOME::PyExecutionMode::InProcess;
220   };
221   if(DefaultPyExecMode == SALOME::PyExecutionMode::NotSet)
222     DefaultPyExecMode = isEnvVarSet();
223   return DefaultPyExecMode;
224 }