Salome HOME
08c174254c09cff57123be2289ca9f97d7fc380f
[modules/kernel.git] / src / Container / SALOME_Container_i.hxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Container : implementation of container and engine for Kernel
24 //  File   : SALOME_Container_i.hxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28 //
29 #ifndef _SALOME_CONTAINER_I_HXX_
30 #define _SALOME_CONTAINER_I_HXX_
31
32 #include "SALOME_Container.hxx"
33 #include "Utils_Mutex.hxx"
34
35 #include <SALOMEconfig.h>
36 #include CORBA_SERVER_HEADER(SALOME_Component)
37 #include CORBA_SERVER_HEADER(SALOME_PyNode)
38
39 #include <iostream>
40 #include <signal.h>
41 #include <stdlib.h>
42 #ifndef WIN32
43 #include <unistd.h>
44 #endif
45 #include <sys/types.h>
46 #include <omnithread.h>
47 #include <map>
48 #include <list>
49 #include <string>
50 #include <vector>
51
52 class SALOME_NamingService_Container_Abstract;
53
54 class CONTAINER_EXPORT Abstract_Engines_Container_i : public virtual POA_Engines::Container,
55                                                       public virtual PortableServer::ServantBase
56 {
57 public:
58   Abstract_Engines_Container_i();
59   Abstract_Engines_Container_i(const std::string& pyContainerClsName,
60                                CORBA::ORB_ptr orb,
61                                PortableServer::POA_ptr poa,
62                                char *containerName,
63                                int argc, char *argv[],
64                                SALOME_NamingService_Container_Abstract *ns = nullptr,
65                                bool isServantAloneInProcess = true);
66   virtual ~Abstract_Engines_Container_i();
67
68   virtual bool isSSLMode() const = 0;
69
70  std::string getPyContainerClassName() const { return _py_container_name; }
71
72   // --- CORBA methods
73
74   virtual bool load_component_Library(const char *componentName, CORBA::String_out reason);
75
76   virtual Engines::EngineComponent_ptr
77   create_component_instance(const char *componentName);
78
79   void override_environment_python( const Engines::FieldsDict& env ) override;
80
81   void override_environment( const Engines::FieldsDict& env ) override;
82
83   Engines::FieldsDict *get_os_environment() override;
84
85   void set_big_obj_on_disk_threshold(CORBA::Long thresholdInByte) override;
86
87   void set_big_obj_on_disk_directory(const char *directory) override;
88
89   void set_number_of_retry(CORBA::Long nbRetry) override;
90
91   void addLogFileNameGroup(const Engines::vectorOfString& groupOfLogFileNames) override;
92     
93   Engines::vectorOfVectorOfString *getAllLogFileNameGroups() override;
94   
95   void execute_python_code(const char *code) override;
96
97   virtual Engines::EngineComponent_ptr
98   create_component_instance_env(const char *componentName,
99                                 const Engines::FieldsDict &env,
100                                 CORBA::String_out reason);
101
102   virtual char *
103   create_python_service_instance(const char *CompName,
104                                  CORBA::String_out reason);
105
106   Engines::EngineComponent_ptr
107   find_component_instance(const char *registeredName);
108
109   Engines::EngineComponent_ptr
110   load_impl(const char *nameToRegister,
111             const char *componentName);
112
113   bool is_SSL_mode() override { return this->isSSLMode(); }
114
115   Engines::EmbeddedNamingService_ptr get_embedded_NS_if_ssl() override;
116
117   SALOME_NamingService_Container_Abstract *getNS() const { return _NS; }
118
119   void remove_impl(Engines::EngineComponent_ptr component_i);
120   void finalize_removal();
121
122   virtual void ping();
123   CORBA::Long getNumberOfCPUCores();
124   Engines::vectorOfDouble* loadOfCPUCores();
125   void setPyScriptForCPULoad(const char *script);
126   void resetScriptForCPULoad();
127   CORBA::Long getTotalPhysicalMemory();
128   CORBA::Long getTotalPhysicalMemoryInUse();
129   CORBA::Long getTotalPhysicalMemoryInUseByMe();
130   char *name();
131   char *workingdir();
132   char *logfilename() override;
133   void logfilename(const char *name) override;
134   char *locallogfilename() override;
135   void locallogfilename(const char *name) override;
136   CORBA::Long monitoringtimeresms() override;
137   void monitoringtimeresms(CORBA::Long intervalInMs) override;
138   void verbosity(bool& activated, CORBA::String_out level) override;
139   void setVerbosity(bool activated, const char *level) override;
140
141   void Shutdown() override;
142   void ShutdownNow() override;
143   char *getHostName();
144   CORBA::Long getPID();
145   //! Kill current container
146   bool Kill_impl();
147
148   Engines::fileRef_ptr createFileRef(const char *origFileName);
149   Engines::fileTransfer_ptr getFileTransfer();
150
151   virtual Engines::Salome_file_ptr createSalome_file(const char *origFileName);
152   void copyFile(Engines::Container_ptr container, const char *remoteFile, const char *localFile);
153   Engines::PyNode_ptr createPyNode(const char *nodeName, const char *code);
154   Engines::PyNode_ptr getDefaultPyNode(const char *nodeName);
155   Engines::PyScriptNode_ptr createPyScriptNode(const char *nodeName, const char *code);
156   void removePyScriptNode(const char *nodeName) override;
157   void cleanAllPyScripts() override;
158   Engines::PyScriptNode_ptr getDefaultPyScriptNode(const char *nodeName);
159   // --- local C++ methods
160
161   Engines::EngineComponent_ptr
162   find_or_create_instance(std::string genericRegisterName,
163                           std::string componentLibraryName);
164
165   bool load_component_CppImplementation(const char *componentName, std::string &reason);
166   bool load_component_PythonImplementation(const char *componentName, std::string &reason);
167   bool load_component_ExecutableImplementation(const char *componentName, std::string &reason);
168
169   Engines::EngineComponent_ptr createPythonInstance(std::string CompName, std::string &error);
170   Engines::EngineComponent_ptr createExecutableInstance(std::string CompName, const Engines::FieldsDict &env, std::string &error);
171   Engines::EngineComponent_ptr createInstance(std::string genericRegisterName, void *handle, std::string &error);
172
173   static bool isPythonContainer(const char *ContainerName);
174   static void decInstanceCnt(std::string genericRegisterName);
175   //??? char* machineName();
176
177   // --- needed for parallel components, Numerical Platon
178
179   int getArgc() { return _argc; }
180   char **getArgv() { return _argv; }
181
182   void registerTemporaryFile(const std::string &fileName);
183   void unregisterTemporaryFile(const std::string &fileName);
184   void clearTemporaryFiles();
185   PortableServer::ObjectId *getCORBAId() const { return _id; }
186 public:
187   static const int DFT_TIME_INTERVAL_BTW_MEASURE;
188 private:
189   void ShutdownCommonPart();
190 protected:
191   static std::map<std::string, int> _cntInstances_map;
192   static std::map<std::string, void *> _library_map;  // library names, loaded
193   static std::map<std::string, void *> _toRemove_map; // library names to remove
194   static omni_mutex _numInstanceMutex;                // lib and instance protection
195
196   bool _isSupervContainer;
197
198   SALOME_NamingService_Container_Abstract *_NS;
199   std::string _library_path;
200   std::string _containerName;
201   std::string _logfilename;
202   std::string _localfilename;
203   std::string _load_script;
204   std::string _py_container_name;
205   CORBA::ORB_var _orb;
206   PortableServer::POA_var _poa;
207   PortableServer::ObjectId *_id;
208   int _numInstance;
209   std::map<std::string, Engines::EngineComponent_var> _listInstances_map;
210   std::map<std::string, Engines::fileRef_var> _fileRef_map;
211   std::map<std::string, Engines::Salome_file_var> _Salome_file_map;
212   std::map<std::string, Engines::PyScriptNode_var> _dftPyScriptNode;
213   std::map<std::string, Engines::PyNode_var> _dftPyNode;
214   Utils_Mutex _mutexForDftPy;
215   std::list<std::string> _tmp_files;
216   Engines::fileTransfer_var _fileTransfer;
217   std::vector< std::vector<std::string> > _groups_of_log_files;
218
219   int _argc;
220   char **_argv;
221   long _pid;
222   bool _isServantAloneInProcess;
223 };
224
225 constexpr char PY_CONTAINER_CLS_NAME_IN_PROCESS[] = "SALOME_Container_i";
226 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY[] = "SALOME_Container_OutOfProcess_i";
227 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY[] = "SALOME_Container_OutOfProcess_Replay_i";
228 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY_FT[] = "SALOME_Container_OutOfProcess_FT_i";
229 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY_FT[] = "SALOME_Container_OutOfProcess_Replay_FT_i";
230
231 class CONTAINER_EXPORT Engines_Container_i : public Abstract_Engines_Container_i
232 {
233 public:
234   Engines_Container_i() { }
235   Engines_Container_i(CORBA::ORB_ptr orb,
236                       PortableServer::POA_ptr poa,
237                       char *containerName,
238                       int argc, char *argv[],
239                       SALOME_NamingService_Container_Abstract *ns = nullptr,
240                       bool isServantAloneInProcess = true) :
241                       Abstract_Engines_Container_i(PY_CONTAINER_CLS_NAME_IN_PROCESS, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
242   bool isSSLMode() const override { return false; }
243 };
244
245 class CONTAINER_EXPORT Abstract_Engines_Container_SSL_i : public Abstract_Engines_Container_i
246 {
247 public:
248   Abstract_Engines_Container_SSL_i(const std::string& pyContainerClsName,
249                           CORBA::ORB_ptr orb,
250                           PortableServer::POA_ptr poa,
251                           char *containerName,
252                           int argc, char *argv[],
253                           SALOME_NamingService_Container_Abstract *ns = nullptr,
254                           bool isServantAloneInProcess = true) :
255                           Abstract_Engines_Container_i(pyContainerClsName, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
256     bool isSSLMode() const override { return true; }
257 };
258
259 class CONTAINER_EXPORT Engines_Container_SSL_i : public Abstract_Engines_Container_SSL_i
260 {
261 public:
262   Engines_Container_SSL_i(CORBA::ORB_ptr orb,
263                           PortableServer::POA_ptr poa,
264                           char *containerName,
265                           int argc, char *argv[],
266                           SALOME_NamingService_Container_Abstract *ns = nullptr,
267                           bool isServantAloneInProcess = true) :
268                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_IN_PROCESS, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
269 };
270
271 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_i : public Abstract_Engines_Container_SSL_i
272 {
273 public:
274   Engines_Container_SSL_OutOfProcess_i(CORBA::ORB_ptr orb,
275                           PortableServer::POA_ptr poa,
276                           char *containerName,
277                           int argc, char *argv[],
278                           SALOME_NamingService_Container_Abstract *ns = nullptr,
279                           bool isServantAloneInProcess = true) :
280                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
281 };
282
283 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_Replay_i : public Abstract_Engines_Container_SSL_i
284 {
285 public:
286   Engines_Container_SSL_OutOfProcess_Replay_i(CORBA::ORB_ptr orb,
287                           PortableServer::POA_ptr poa,
288                           char *containerName,
289                           int argc, char *argv[],
290                           SALOME_NamingService_Container_Abstract *ns = nullptr,
291                           bool isServantAloneInProcess = true) :
292                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
293 };
294
295 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_FT_i : public Abstract_Engines_Container_SSL_i
296 {
297 public:
298   Engines_Container_SSL_OutOfProcess_FT_i(CORBA::ORB_ptr orb,
299                           PortableServer::POA_ptr poa,
300                           char *containerName,
301                           int argc, char *argv[],
302                           SALOME_NamingService_Container_Abstract *ns = nullptr,
303                           bool isServantAloneInProcess = true) :
304                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY_FT, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
305 };
306
307 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_Replay_FT_i : public Abstract_Engines_Container_SSL_i
308 {
309 public:
310   Engines_Container_SSL_OutOfProcess_Replay_FT_i(CORBA::ORB_ptr orb,
311                           PortableServer::POA_ptr poa,
312                           char *containerName,
313                           int argc, char *argv[],
314                           SALOME_NamingService_Container_Abstract *ns = nullptr,
315                           bool isServantAloneInProcess = true) :
316                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY_FT, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
317 };
318
319 /*!
320  * Methods to be used in SSL mode to skip NS.
321  */
322 namespace KERNEL
323 {
324   CONTAINER_EXPORT Abstract_Engines_Container_SSL_i *getContainerSA();
325   CONTAINER_EXPORT Engines::Container_var getContainerRefSA();
326 } // namespace KERNEL
327
328 #endif