Salome HOME
81643dc17cbf12349b3d5980181d20902fd497ba
[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 addLogFileNameGroup(const Engines::vectorOfString& groupOfLogFileNames) override;
86     
87   Engines::vectorOfVectorOfString *getAllLogFileNameGroups() override;
88   
89   void execute_python_code(const char *code) override;
90
91   virtual Engines::EngineComponent_ptr
92   create_component_instance_env(const char *componentName,
93                                 const Engines::FieldsDict &env,
94                                 CORBA::String_out reason);
95
96   virtual char *
97   create_python_service_instance(const char *CompName,
98                                  CORBA::String_out reason);
99
100   Engines::EngineComponent_ptr
101   find_component_instance(const char *registeredName);
102
103   Engines::EngineComponent_ptr
104   load_impl(const char *nameToRegister,
105             const char *componentName);
106
107   bool is_SSL_mode() override { return this->isSSLMode(); }
108
109   Engines::EmbeddedNamingService_ptr get_embedded_NS_if_ssl() override;
110
111   SALOME_NamingService_Container_Abstract *getNS() const { return _NS; }
112
113   void remove_impl(Engines::EngineComponent_ptr component_i);
114   void finalize_removal();
115
116   virtual void ping();
117   CORBA::Long getNumberOfCPUCores();
118   Engines::vectorOfDouble* loadOfCPUCores();
119   void setPyScriptForCPULoad(const char *script);
120   void resetScriptForCPULoad();
121   CORBA::Long getTotalPhysicalMemory();
122   CORBA::Long getTotalPhysicalMemoryInUse();
123   CORBA::Long getTotalPhysicalMemoryInUseByMe();
124   char *name();
125   char *workingdir();
126   char *logfilename() override;
127   void logfilename(const char *name) override;
128   char *locallogfilename() override;
129   void locallogfilename(const char *name) override;
130   CORBA::Long monitoringtimeresms() override;
131   void monitoringtimeresms(CORBA::Long intervalInMs) override;
132   void verbosity(bool& activated, CORBA::String_out level) override;
133   void setVerbosity(bool activated, const char *level) override;
134
135   void Shutdown() override;
136   void ShutdownNow() override;
137   char *getHostName();
138   CORBA::Long getPID();
139   //! Kill current container
140   bool Kill_impl();
141
142   Engines::fileRef_ptr createFileRef(const char *origFileName);
143   Engines::fileTransfer_ptr getFileTransfer();
144
145   virtual Engines::Salome_file_ptr createSalome_file(const char *origFileName);
146   void copyFile(Engines::Container_ptr container, const char *remoteFile, const char *localFile);
147   Engines::PyNode_ptr createPyNode(const char *nodeName, const char *code);
148   Engines::PyNode_ptr getDefaultPyNode(const char *nodeName);
149   Engines::PyScriptNode_ptr createPyScriptNode(const char *nodeName, const char *code);
150   void removePyScriptNode(const char *nodeName) override;
151   void cleanAllPyScripts() override;
152   Engines::PyScriptNode_ptr getDefaultPyScriptNode(const char *nodeName);
153   // --- local C++ methods
154
155   Engines::EngineComponent_ptr
156   find_or_create_instance(std::string genericRegisterName,
157                           std::string componentLibraryName);
158
159   bool load_component_CppImplementation(const char *componentName, std::string &reason);
160   bool load_component_PythonImplementation(const char *componentName, std::string &reason);
161   bool load_component_ExecutableImplementation(const char *componentName, std::string &reason);
162
163   Engines::EngineComponent_ptr createPythonInstance(std::string CompName, std::string &error);
164   Engines::EngineComponent_ptr createExecutableInstance(std::string CompName, const Engines::FieldsDict &env, std::string &error);
165   Engines::EngineComponent_ptr createInstance(std::string genericRegisterName, void *handle, std::string &error);
166
167   static bool isPythonContainer(const char *ContainerName);
168   static void decInstanceCnt(std::string genericRegisterName);
169   //??? char* machineName();
170
171   // --- needed for parallel components, Numerical Platon
172
173   int getArgc() { return _argc; }
174   char **getArgv() { return _argv; }
175
176   void registerTemporaryFile(const std::string &fileName);
177   void unregisterTemporaryFile(const std::string &fileName);
178   void clearTemporaryFiles();
179   PortableServer::ObjectId *getCORBAId() const { return _id; }
180 public:
181   static const int DFT_TIME_INTERVAL_BTW_MEASURE;
182 private:
183   void ShutdownCommonPart();
184 protected:
185   static std::map<std::string, int> _cntInstances_map;
186   static std::map<std::string, void *> _library_map;  // library names, loaded
187   static std::map<std::string, void *> _toRemove_map; // library names to remove
188   static omni_mutex _numInstanceMutex;                // lib and instance protection
189
190   bool _isSupervContainer;
191
192   SALOME_NamingService_Container_Abstract *_NS;
193   std::string _library_path;
194   std::string _containerName;
195   std::string _logfilename;
196   std::string _localfilename;
197   std::string _load_script;
198   std::string _py_container_name;
199   CORBA::ORB_var _orb;
200   PortableServer::POA_var _poa;
201   PortableServer::ObjectId *_id;
202   int _numInstance;
203   std::map<std::string, Engines::EngineComponent_var> _listInstances_map;
204   std::map<std::string, Engines::fileRef_var> _fileRef_map;
205   std::map<std::string, Engines::Salome_file_var> _Salome_file_map;
206   std::map<std::string, Engines::PyScriptNode_var> _dftPyScriptNode;
207   std::map<std::string, Engines::PyNode_var> _dftPyNode;
208   Utils_Mutex _mutexForDftPy;
209   std::list<std::string> _tmp_files;
210   Engines::fileTransfer_var _fileTransfer;
211   std::vector< std::vector<std::string> > _groups_of_log_files;
212
213   int _argc;
214   char **_argv;
215   long _pid;
216   bool _isServantAloneInProcess;
217 };
218
219 constexpr char PY_CONTAINER_CLS_NAME_IN_PROCESS[] = "SALOME_Container_i";
220 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY[] = "SALOME_Container_OutOfProcess_i";
221 constexpr char PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY[] = "SALOME_Container_OutOfProcess_Replay_i";
222
223
224 class CONTAINER_EXPORT Engines_Container_i : public Abstract_Engines_Container_i
225 {
226 public:
227   Engines_Container_i() { }
228   Engines_Container_i(CORBA::ORB_ptr orb,
229                       PortableServer::POA_ptr poa,
230                       char *containerName,
231                       int argc, char *argv[],
232                       SALOME_NamingService_Container_Abstract *ns = nullptr,
233                       bool isServantAloneInProcess = true) :
234                       Abstract_Engines_Container_i(PY_CONTAINER_CLS_NAME_IN_PROCESS, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
235   bool isSSLMode() const override { return false; }
236 };
237
238 class CONTAINER_EXPORT Abstract_Engines_Container_SSL_i : public Abstract_Engines_Container_i
239 {
240 public:
241   Abstract_Engines_Container_SSL_i(const std::string& pyContainerClsName,
242                           CORBA::ORB_ptr orb,
243                           PortableServer::POA_ptr poa,
244                           char *containerName,
245                           int argc, char *argv[],
246                           SALOME_NamingService_Container_Abstract *ns = nullptr,
247                           bool isServantAloneInProcess = true) :
248                           Abstract_Engines_Container_i(pyContainerClsName, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
249     bool isSSLMode() const override { return true; }
250 };
251
252 class CONTAINER_EXPORT Engines_Container_SSL_i : public Abstract_Engines_Container_SSL_i
253 {
254 public:
255   Engines_Container_SSL_i(CORBA::ORB_ptr orb,
256                           PortableServer::POA_ptr poa,
257                           char *containerName,
258                           int argc, char *argv[],
259                           SALOME_NamingService_Container_Abstract *ns = nullptr,
260                           bool isServantAloneInProcess = true) :
261                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_IN_PROCESS, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
262 };
263
264 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_i : public Abstract_Engines_Container_SSL_i
265 {
266 public:
267   Engines_Container_SSL_OutOfProcess_i(CORBA::ORB_ptr orb,
268                           PortableServer::POA_ptr poa,
269                           char *containerName,
270                           int argc, char *argv[],
271                           SALOME_NamingService_Container_Abstract *ns = nullptr,
272                           bool isServantAloneInProcess = true) :
273                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_NO_REPLAY, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
274 };
275
276 class CONTAINER_EXPORT Engines_Container_SSL_OutOfProcess_Replay_i : public Abstract_Engines_Container_SSL_i
277 {
278 public:
279   Engines_Container_SSL_OutOfProcess_Replay_i(CORBA::ORB_ptr orb,
280                           PortableServer::POA_ptr poa,
281                           char *containerName,
282                           int argc, char *argv[],
283                           SALOME_NamingService_Container_Abstract *ns = nullptr,
284                           bool isServantAloneInProcess = true) :
285                           Abstract_Engines_Container_SSL_i(PY_CONTAINER_CLS_NAME_OUT_PROCESS_WITH_REPLAY, orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
286 };
287
288 /*!
289  * Methods to be used in SSL mode to skip NS.
290  */
291 namespace KERNEL
292 {
293   CONTAINER_EXPORT Abstract_Engines_Container_SSL_i *getContainerSA();
294   CONTAINER_EXPORT Engines::Container_var getContainerRefSA();
295 } // namespace KERNEL
296
297 #endif