# CORBA python client utilities # Victor Kress, Seattle 6/18/04 # $Id: dPhaseClientUtils.py,v 1.9 2007/08/25 02:33:45 kress Exp $ import os import sys #set platform-specific variables if os.environ.has_key('PHASEROOT'): path_to_phases_module = os.environ['PHASEROOT']+'/dPhases' else: print 'PHASEROOT environment variable not set. Cannot run client.' if os.environ.has_key('OMNIROOT'): path_to_omni_idl = os.environ['OMNIROOT']+'/share/idl/omniORB/' else: print 'OMNIROOT environment variable not set. Cannot run client.' from omniORB import CORBA,importIDL import CosNaming # Either of the following options work. The first is faster. # The second option is more flexible. Paths defined above. if 1: # access compiled python phases module sys.path.append(path_to_phases_module) print path_to_phases_module import phases print 'phases module imported' import rock print 'rock module imported' else: #this doesn't work, and it's not clear why # compile module straight from idl m=importIDL(path_to_phases_module+'/phases.idl', ' -I '+path_to_omni_idl) phases=sys.modules[m[0]] m=importIDL(path_to_phases_module+'/rock.idl', ' -I '+path_to_omni_idl) rock=sys.modules[m[0]] print 'dPhaseClientUtils.py is deprecated' print 'this file will remain temporarily, but client apps should be' print 'rewritten to use class Connection.py' def getORB(): "Simple function to initialize and return client ORB" orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID) return orb def getObjectReference(orb,name): "Contacts name service and obtains a reference to registered object." "orb is valid initialized ORB (see getORB)" if not orb: print 'getPhaseFactory not given valid ORB' sys.exit(1) # Initialize and get naming service ############################ # Obtain a reference to the root naming context # print 'obtaining root naming context' obj = orb.resolve_initial_references("NameService") rootContext = obj._narrow(CosNaming.NamingContext) if rootContext is None: print "Failed to narrow the root naming context" sys.exit(1) # Get PhaseFactory object ##################################### # Resolve the name try: obj = rootContext.resolve(name) except CosNaming.NamingContext.NotFound, ex: print "Name not found" sys.exit(1) return obj def getdPhaseFactory(orb): name = [CosNaming.NameComponent("phases", "context"), CosNaming.NameComponent("PhaseFactory", "object")] obj = getObjectReference(orb,name) # Narrow the object to an phases::PhaseFactory # print 'narrowing phase factory object' pFactory = obj._narrow(phases.PhaseFactory) if (pFactory is None): print "Object reference is not a phases::PhaseFactory" sys.exit(1) return pFactory def getdPhaseManager(orb): name = [CosNaming.NameComponent("phases", "context"), CosNaming.NameComponent("PhaseManager", "object")] obj = getObjectReference(orb,name) # Narrow the object to an phases::PhaseFactory # print 'narrowing phase manager object' pManager = obj._narrow(phases.PhaseManager) if (pManager is None): print "Object reference is not a phases::PhaseManager" sys.exit(1) return pManager def getdRockFactory(orb): name = [CosNaming.NameComponent("rock", "context"), CosNaming.NameComponent("RockFactory", "object")] obj = getObjectReference(orb,name) # Narrow the object to an phases::PhaseFactory # print 'narrowing phase factory object' rFactory = obj._narrow(rock.RockFactory) if (rFactory is None): print "Object reference is not a rock::RockFactory" sys.exit(1) return rFactory def getClientData(): from locale import getdefaultlocale from time import daylight,tzname from socket import gethostname,gethostbyname language = "Python" lang_vendor = sys.version.split('\n')[1] lang_version = sys.version.split('\n')[0] corba_vendor = 'omniORB' corba_version = 'omniORBpy-2.6' corba_spec = '2.6' os_name = sys.platform os_arch = os.name os_version = '' user_name = os.environ.get('USER') locale = '%s:%s'%tuple(getdefaultlocale()) if daylight: time_zone = tzname[1] else: time_zone = tzname[0] canonical_hostname = gethostname() host_address = gethostbyname(canonical_hostname) host_name = os.environ.get('HOSTNAME') cd=phases.ClientData(language,lang_vendor,lang_version, corba_vendor,corba_version,corba_spec, os_name,os_arch,os_version,user_name, locale,time_zone,canonical_hostname, host_address,host_name) return cd