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