Salome HOME
[EDF27816] : WIP
[modules/kernel.git] / src / Container / SALOME_Container_i.hxx
1 // Copyright (C) 2007-2023  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
51 class SALOME_NamingService_Container_Abstract;
52
53 class CONTAINER_EXPORT Abstract_Engines_Container_i : public virtual POA_Engines::Container,
54                                                       public virtual PortableServer::ServantBase
55 {
56 public:
57   Abstract_Engines_Container_i();
58   Abstract_Engines_Container_i(CORBA::ORB_ptr orb,
59                                PortableServer::POA_ptr poa,
60                                char *containerName,
61                                int argc, char *argv[],
62                                SALOME_NamingService_Container_Abstract *ns = nullptr,
63                                bool isServantAloneInProcess = true);
64   virtual ~Abstract_Engines_Container_i();
65
66   virtual bool isSSLMode() const = 0;
67
68   // --- CORBA methods
69
70   virtual bool load_component_Library(const char *componentName, CORBA::String_out reason);
71
72   virtual Engines::EngineComponent_ptr
73   create_component_instance(const char *componentName);
74
75   void override_environment_python( const Engines::FieldsDict& env ) override;
76
77   void override_environment( const Engines::FieldsDict& env ) override;
78
79   Engines::FieldsDict *get_os_environment() override;
80
81   virtual Engines::EngineComponent_ptr
82   create_component_instance_env(const char *componentName,
83                                 const Engines::FieldsDict &env,
84                                 CORBA::String_out reason);
85
86   virtual char *
87   create_python_service_instance(const char *CompName,
88                                  CORBA::String_out reason);
89
90   Engines::EngineComponent_ptr
91   find_component_instance(const char *registeredName);
92
93   Engines::EngineComponent_ptr
94   load_impl(const char *nameToRegister,
95             const char *componentName);
96
97   bool is_SSL_mode() override { return this->isSSLMode(); }
98
99   Engines::EmbeddedNamingService_ptr get_embedded_NS_if_ssl() override;
100
101   SALOME_NamingService_Container_Abstract *getNS() const { return _NS; }
102
103   void remove_impl(Engines::EngineComponent_ptr component_i);
104   void finalize_removal();
105
106   virtual void ping();
107   CORBA::Long getNumberOfCPUCores();
108   Engines::vectorOfDouble* loadOfCPUCores();
109   void setPyScriptForCPULoad(const char *script);
110   void resetScriptForCPULoad();
111   CORBA::Long getTotalPhysicalMemory();
112   CORBA::Long getTotalPhysicalMemoryInUse();
113   CORBA::Long getTotalPhysicalMemoryInUseByMe();
114   char *name();
115   char *workingdir();
116   char *logfilename();
117   void logfilename(const char *name);
118
119   virtual void Shutdown();
120   char *getHostName();
121   CORBA::Long getPID();
122   //! Kill current container
123   bool Kill_impl();
124
125   Engines::fileRef_ptr createFileRef(const char *origFileName);
126   Engines::fileTransfer_ptr getFileTransfer();
127
128   virtual Engines::Salome_file_ptr createSalome_file(const char *origFileName);
129   void copyFile(Engines::Container_ptr container, const char *remoteFile, const char *localFile);
130   Engines::PyNode_ptr createPyNode(const char *nodeName, const char *code);
131   Engines::PyNode_ptr getDefaultPyNode(const char *nodeName);
132   Engines::PyScriptNode_ptr createPyScriptNode(const char *nodeName, const char *code);
133   void removePyScriptNode(const char *nodeName) override;
134   void cleanAllPyScripts() override;
135   Engines::PyScriptNode_ptr getDefaultPyScriptNode(const char *nodeName);
136   // --- local C++ methods
137
138   Engines::EngineComponent_ptr
139   find_or_create_instance(std::string genericRegisterName,
140                           std::string componentLibraryName);
141
142   bool load_component_CppImplementation(const char *componentName, std::string &reason);
143   bool load_component_PythonImplementation(const char *componentName, std::string &reason);
144   bool load_component_ExecutableImplementation(const char *componentName, std::string &reason);
145
146   Engines::EngineComponent_ptr createPythonInstance(std::string CompName, std::string &error);
147   Engines::EngineComponent_ptr createExecutableInstance(std::string CompName, const Engines::FieldsDict &env, std::string &error);
148   Engines::EngineComponent_ptr createInstance(std::string genericRegisterName, void *handle, std::string &error);
149
150   static bool isPythonContainer(const char *ContainerName);
151   static void decInstanceCnt(std::string genericRegisterName);
152   //??? char* machineName();
153
154   // --- needed for parallel components, Numerical Platon
155
156   int getArgc() { return _argc; }
157   char **getArgv() { return _argv; }
158
159   void registerTemporaryFile(const std::string &fileName);
160   void unregisterTemporaryFile(const std::string &fileName);
161   void clearTemporaryFiles();
162   PortableServer::ObjectId *getCORBAId() const { return _id; }
163
164 protected:
165   static std::map<std::string, int> _cntInstances_map;
166   static std::map<std::string, void *> _library_map;  // library names, loaded
167   static std::map<std::string, void *> _toRemove_map; // library names to remove
168   static omni_mutex _numInstanceMutex;                // lib and instance protection
169
170   bool _isSupervContainer;
171
172   SALOME_NamingService_Container_Abstract *_NS;
173   std::string _library_path;
174   std::string _containerName;
175   std::string _logfilename;
176   std::string _load_script;
177   CORBA::ORB_var _orb;
178   PortableServer::POA_var _poa;
179   PortableServer::ObjectId *_id;
180   int _numInstance;
181   std::map<std::string, Engines::EngineComponent_var> _listInstances_map;
182   std::map<std::string, Engines::fileRef_var> _fileRef_map;
183   std::map<std::string, Engines::Salome_file_var> _Salome_file_map;
184   std::map<std::string, Engines::PyScriptNode_var> _dftPyScriptNode;
185   std::map<std::string, Engines::PyNode_var> _dftPyNode;
186   Utils_Mutex _mutexForDftPy;
187   std::list<std::string> _tmp_files;
188   Engines::fileTransfer_var _fileTransfer;
189
190   int _argc;
191   char **_argv;
192   long _pid;
193   bool _isServantAloneInProcess;
194 };
195
196 class CONTAINER_EXPORT Engines_Container_i : public Abstract_Engines_Container_i
197 {
198 public:
199   Engines_Container_i() { }
200   Engines_Container_i(CORBA::ORB_ptr orb,
201                       PortableServer::POA_ptr poa,
202                       char *containerName,
203                       int argc, char *argv[],
204                       SALOME_NamingService_Container_Abstract *ns = nullptr,
205                       bool isServantAloneInProcess = true) :
206                       Abstract_Engines_Container_i(orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
207   bool isSSLMode() const override { return false; }
208 };
209
210 class CONTAINER_EXPORT Engines_Container_SSL_i : public Abstract_Engines_Container_i
211 {
212 public:
213   Engines_Container_SSL_i();
214   Engines_Container_SSL_i(CORBA::ORB_ptr orb,
215                           PortableServer::POA_ptr poa,
216                           char *containerName,
217                           int argc, char *argv[],
218                           SALOME_NamingService_Container_Abstract *ns = nullptr,
219                           bool isServantAloneInProcess = true) :
220                           Abstract_Engines_Container_i(orb, poa, containerName, argc, argv, ns, isServantAloneInProcess) {}
221     bool isSSLMode() const override { return true; }
222 };
223
224 /*!
225  * Methods to be used in SSL mode to skip NS.
226  */
227 namespace KERNEL
228 {
229   CONTAINER_EXPORT Engines_Container_SSL_i *getContainerSA();
230   CONTAINER_EXPORT Engines::Container_var getContainerRefSA();
231 } // namespace KERNEL
232
233 #endif