Salome HOME
DCQ : Merge with Ecole_ete_a6.
[modules/kernel.git] / src / Container / SALOME_ContainerPy.py
1 #! /usr/bin/env python
2 #
3 #  SALOME Container : implementation of container and engine for Kernel
4 #
5 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
7
8 #  This library is free software; you can redistribute it and/or 
9 #  modify it under the terms of the GNU Lesser General Public 
10 #  License as published by the Free Software Foundation; either 
11 #  version 2.1 of the License. 
12
13 #  This library is distributed in the hope that it will be useful, 
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
16 #  Lesser General Public License for more details. 
17
18 #  You should have received a copy of the GNU Lesser General Public 
19 #  License along with this library; if not, write to the Free Software 
20 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
21
22 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
23 #
24 #
25 #
26 #  File   : SALOME_ContainerPy.py
27 #  Author : Paul RASCLE, EDF
28 #  Module : SALOME
29 #  $Header$
30
31 import os
32 import sys
33 import string
34 from omniORB import CORBA, PortableServer
35 # import SALOMEDS before other SALOME modules
36 # (if not, incomplete import done by SALOME module: no load of SALOMEDS_attributes)
37 import SALOMEDS 
38 import Engines, Engines__POA
39 from SALOME_NamingServicePy import *
40 from SALOME_ComponentPy import *
41
42 from SALOME_utilities import *
43
44 #=============================================================================
45
46 #define an implementation of the container interface
47
48 class SALOME_ContainerPy_i (Engines__POA.Container):
49     _orb = None
50     _poa = None
51     _numInstance = 0
52
53     #-------------------------------------------------------------------------
54
55     def __init__(self, orb, poa, containerName):
56         MESSAGE( "SALOME_ContainerPy_i::__init__" )
57         self._orb = orb
58         self._poa = poa
59         self._containerName = containerName
60
61         myMachine=string.split(os.getenv( "HOSTNAME" ),'.')
62         naming_service = SALOME_NamingServicePy_i(self._orb)
63         self._naming_service = naming_service
64         Container_path = "/Containers/" + myMachine[0] + "/" + self._containerName
65         MESSAGE( str(Container_path) )
66         naming_service.Register(self._this(), Container_path)
67             
68     #-------------------------------------------------------------------------
69
70     def start_impl(self, ContainerName):
71         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
72         myMachine=string.split(os.getenv( "HOSTNAME" ),'.')
73         theContainer = "/Containers/" + myMachine[0] + "/" + ContainerName
74         try:
75             obj = self._naming_service.Resolve(theContainer)
76         except :
77             obj = None
78             MESSAGE(  "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
79         if obj is None:
80             container = None
81         else:
82             container = obj._narrow(Engines.Container)
83             if container is None:
84                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
85             else :
86                 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
87             return container
88         #shstr = os.getenv( "PWD" ) + "/"
89         #shstr += "runSession ./SALOME_ContainerPy.py "
90         shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
91         #shstr = "runSession SALOME_ContainerPy.py "
92         shstr += ContainerName
93
94         # mpv: fix for SAL4731 - allways create new file to write log of server
95         num = 1
96         fileName = ""
97         while 1:
98             fileName = "/tmp/"+ContainerName+"_%i.log"%num
99             if not os.path.exists(fileName):
100                 break
101             num += 1
102             pass
103         
104         shstr += " > "
105         shstr += fileName
106         shstr += " 2>&1 &"
107         
108         #shstr += " > /tmp/"
109         #shstr += ContainerName
110         #shstr += ".log 2>&1 &"
111         
112         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
113         os.system( shstr )
114         count = 21
115         while container is None :
116             time.sleep(1)
117             count = count - 1
118             MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
119             try :
120                 obj = self._naming_service.Resolve(theContainer)
121             except :
122                 obj = None
123             if obj is None:
124                 container = None
125             else:
126                 container = obj._narrow(Engines.Container)
127                 if container is None:
128                     MESSAGE(  str(containerName) + ".object exists but is not a Container" )
129                 return container
130             if count == 0 :
131                 return container
132
133     #-------------------------------------------------------------------------
134
135     def load_impl(self, nameToRegister, componentName):
136         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
137         self._numInstance = self._numInstance +1
138         instanceName = nameToRegister + "_inst_" + `self._numInstance`
139         interfaceName = nameToRegister
140         the_command = "import " + nameToRegister + "\n"
141         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
142         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
143         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
144         exec the_command
145         comp_o = comp_i._this()
146         return comp_o
147     
148     #-------------------------------------------------------------------------
149
150     def remove_impl(self, component):
151         MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
152
153     #-------------------------------------------------------------------------
154
155     def finalize_removal(self):
156         MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
157
158     #-------------------------------------------------------------------------
159
160     def ping(self):
161         MESSAGE( "SALOME_ContainerPy_i::ping" )
162
163     #-------------------------------------------------------------------------
164
165     def _get_name(self):
166         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
167
168     #-------------------------------------------------------------------------
169
170     def _get_machineName(self):
171         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
172         self._machineName = "localhost"
173         return self._machineName
174
175 #=============================================================================
176
177 #initialise the ORB and find the root POA
178 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
179 poa = orb.resolve_initial_references("RootPOA")
180
181 #create an instance of SALOME_ContainerPy_i and a Container reference
182 #containerName = "FactoryServerPy"
183 MESSAGE( str(sys.argv) )
184 containerName = sys.argv[1]
185 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
186 cpy_o = cpy_i._this()
187
188 #activate the POA
189 poaManager = poa._get_the_POAManager()
190 poaManager.activate()
191
192 #Block for ever
193 orb.run()
194
195
196         
197             
198
199