Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b1'.
[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 runSession" )
87             return container
88         #shstr = os.getenv( "PWD" ) + "/"
89         #shstr += "runSession ./SALOME_ContainerPy.py "
90         shstr = "runSession SALOME_ContainerPy.py "
91         shstr += ContainerName
92
93         # mpv: fix for SAL4731 - allways create new file to write log of server
94         num = 1
95         fileName = ""
96         while 1:
97             fileName = "/tmp/"+ContainerName+"_%i.log"%num
98             if not os.path.exists(fileName):
99                 break
100             num += 1
101             pass
102         
103         shstr += " > "
104         shstr += fileName
105         shstr += " 2>&1 &"
106         
107         #shstr += " > /tmp/"
108         #shstr += ContainerName
109         #shstr += ".log 2>&1 &"
110         
111         MESSAGE(  "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
112         os.system( shstr )
113         count = 21
114         while container is None :
115             time.sleep(1)
116             count = count - 1
117             MESSAGE(  str(count) + ". Waiting for " + str(theContainer) )
118             try :
119                 obj = self._naming_service.Resolve(theContainer)
120             except :
121                 obj = None
122             if obj is None:
123                 container = None
124             else:
125                 container = obj._narrow(Engines.Container)
126                 if container is None:
127                     MESSAGE(  str(containerName) + ".object exists but is not a Container" )
128                 return container
129             if count == 0 :
130                 return container
131
132     #-------------------------------------------------------------------------
133
134     def load_impl(self, nameToRegister, componentName):
135         MESSAGE(  "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
136         self._numInstance = self._numInstance +1
137         instanceName = nameToRegister + "_inst_" + `self._numInstance`
138         interfaceName = nameToRegister
139         the_command = "import " + nameToRegister + "\n"
140         the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
141         the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
142         MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
143         exec the_command
144         comp_o = comp_i._this()
145         return comp_o
146     
147     #-------------------------------------------------------------------------
148
149     def remove_impl(self, component):
150         MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
151
152     #-------------------------------------------------------------------------
153
154     def finalize_removal(self):
155         MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
156
157     #-------------------------------------------------------------------------
158
159     def ping(self):
160         MESSAGE( "SALOME_ContainerPy_i::ping" )
161
162     #-------------------------------------------------------------------------
163
164     def _get_name(self):
165         MESSAGE( "SALOME_ContainerPy_i::_get_name" )
166
167     #-------------------------------------------------------------------------
168
169     def _get_machineName(self):
170         MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
171         self._machineName = "localhost"
172         return self._machineName
173
174 #=============================================================================
175
176 #initialise the ORB and find the root POA
177 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
178 poa = orb.resolve_initial_references("RootPOA")
179
180 #create an instance of SALOME_ContainerPy_i and a Container reference
181 #containerName = "FactoryServerPy"
182 MESSAGE( str(sys.argv) )
183 containerName = sys.argv[1]
184 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
185 cpy_o = cpy_i._this()
186
187 #activate the POA
188 poaManager = poa._get_the_POAManager()
189 poaManager.activate()
190
191 #Block for ever
192 orb.run()
193
194
195         
196             
197
198