Fresco/Fresco-Python-demos/src berlin.py,1.13,1.14

Neil John Pilgrim <[email protected]>
Newsgroups gmane.comp.video.fresco.cvs
Message-ID <[email protected]>
Update of /cvs/fresco/Fresco/Fresco-Python-demos/src
In directory purcel:/tmp/cvs-serv11617

Modified Files:
	berlin.py 
Log Message:
Update python module to new server resolution methods.
Closes task68.


Index: berlin.py
===================================================================
RCS file: /cvs/fresco/Fresco/Fresco-Python-demos/src/berlin.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- berlin.py	28 Jan 2003 03:34:31 -0000	1.13
+++ berlin.py	7 Feb 2003 04:32:45 -0000	1.14
@@ -10,7 +10,7 @@
 instances of the Kits and Connection objects.
 """
 
-import sys, string, threading
+import sys, os, string, threading
 
 # Import omniORBpy stuff
 from omniORB import CORBA
@@ -72,99 +72,165 @@
     objects. It loads _defaultKitNames as the default set of kits, but more
     can be added using the add_kit method."""
     def __init__(self):
-	"""Initialise the Kits singleton, and insert it into the global
-	(berlin module) namespace"""
-	global kits
-	kits = self
-	self._kitNames = {}
-	self._kitNames.update(_defaultKitNames)
-	self._kitObjects = {}
+        """Initialise the Kits singleton, and insert it into the global
+        (berlin module) namespace"""
+        global kits
+        kits = self
+        self._kitNames = {}
+        self._kitNames.update(_defaultKitNames)
+        self._kitObjects = {}
 
     def add_kit(self, name, module, kit):
-	"""Adds the given kit to the list of accessable kits"""
-	self._kitNames[name] = (module, kit)
+        """Adds the given kit to the list of accessable kits"""
+        self._kitNames[name] = (module, kit)
 
     def __getattr__(self, kit):
-	"""Returns a cached kit. If the kit is not already cached, then it is
-	looked up using the list of kits that you can update with add_kit()"""
-	# Check cache
-	if self._kitObjects.has_key(kit):
-	    return self._kitObjects[kit]
-	# Try and resolve
-	if self._kitNames.has_key(kit):
-	    # Find and resolve the kit
-	    module, name = self._kitNames[kit]
-	    idl = "IDL:fresco.org/%s/%s:1.0"%self._kitNames[kit]
-	    obj = connection.context.resolve(idl, [])
-	    cls = globals()[module].__dict__[name]
-	    obj = obj._narrow(cls)
-	    if obj is not None:
-		self._kitObjects[kit] = obj
-		return obj
-	# Raise an exception to signal error
-	raise NameError, "Kit '%s' not found."%kit
+        """Returns a cached kit. If the kit is not already cached, then it is
+        looked up using the list of kits that you can update with add_kit()"""
+        # Check cache
+        if self._kitObjects.has_key(kit):
+            return self._kitObjects[kit]
+        # Try and resolve
+        if self._kitNames.has_key(kit):
+            # Find and resolve the kit
+            module, name = self._kitNames[kit]
+            idl = "IDL:fresco.org/%s/%s:1.0"%self._kitNames[kit]
+            obj = connection.context.resolve(idl, [])
+            cls = globals()[module].__dict__[name]
+            obj = obj._narrow(cls)
+            if obj is not None:
+                self._kitObjects[kit] = obj
+                return obj
+        # Raise an exception to signal error
+        raise NameError, "Kit '%s' not found."%kit
 
 class Connection:
     """Connection wrapper that does all the CORBA stuff"""
     def __init__(self, clientContext=None):
-	"""Initialises the singleton, inserts it into the global (berlin
-	module) namespace, and finds the Berlin server. You may pass your own
-	ClientContext object (not CORBA var!), else a ClientContextImpl will
-	be used."""
-	global connection
-	connection = self
+        """Initialises the singleton, inserts it into the global (berlin
+        module) namespace, and finds the Berlin server. You may pass your own
+        ClientContext object (not CORBA var!), else a ClientContextImpl will
+        be used."""
+        global connection
+        connection = self
 
-	# Initialise the ORB
-	#omniORB.maxTcpConnectionPerServer(100)
-	self.orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
+        # Initialise the ORB
+        #omniORB.maxTcpConnectionPerServer(100)
+        self.orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
 
-	# Find the POA
-	self.poa = self.orb.resolve_initial_references("RootPOA")
-	# Activate the POA
-	self.poaManager = self.poa._get_the_POAManager()
-	self.poaManager.activate()
+        # Find the POA
+        self.poa = self.orb.resolve_initial_references("RootPOA")
+        # Activate the POA
+        self.poaManager = self.poa._get_the_POAManager()
+        self.poaManager.activate()
 
-	# Create an ClientContext
-	if clientContext is None: clientContext = ClientContextImpl()
-	# Create an object reference, and implicitly activate the object
-	self.clientContext = clientContext._this()
+        # Create an ClientContext
+        if clientContext is None: clientContext = ClientContextImpl()
+        # Create an object reference, and implicitly activate the object
+        self.clientContext = clientContext._this()
 
-	# get a server context
-	object = self.orb.resolve_initial_references("NameService")
-	context = object._narrow(CosNaming.NamingContext)
-	name = [CosNaming.NameComponent("FrescoServer", "Object")]
-	object = context.resolve(name)
-	self.server = object._narrow(Fresco.Server)
-	#print "Got Server var =",self.server
-	#print "Client Context var =",self.clientContext
-	self.context  = self.server.create_server_context(self.clientContext)
+        # Default arguments
+        export_ref = ior_file_path = server_id = stringified_ior = ""
+
+        # Parse argument list
+        try:
+            index = sys.argv.index("-R") or sys.argv.index("--export-ref")
+            if index >= 1: export_ref = sys.argv[index+1]
+        except:
+            pass
+
+        try:
+            index = sys.argv.index("-I") or sys.argv.index("--ior-file-path")
+            if index >= 1: ior_file_path = sys.argv[index+1]
+        except:
+            pass
+
+        try:
+            index = sys.argv.index("-i") or sys.argv.index("--server-id")
+            if index >= 1: server_id = sys.argv[index+1]
+        except:
+            pass
+
+        # Use $FRESCO_DISPLAY if the argument list is empty
+        if export_ref == "" and ior_file_path == "" and server_id == "":
+             if os.environ.has_key("FRESCO_DISPLAY"):
+                stringified_ior = os.environ["FRESCO_DISPLAY"]
+
+        # Set defaults
+        if export_ref == "": export_ref = "ior"
+        if ior_file_path == "": ior_file_path = "/tmp/fresco"
+        if server_id == "": server_id = "FrescoServer"
+
+        # Try with $FRESCO_DISPLAY
+        if stringified_ior != "":
+            object = self.orb.string_to_object(stringified_ior)
+        else:
+            # Try with other methods
+            if export_ref == "nameserver":
+                object = self.orb.resolve_initial_references("NameService")
+                context = object._narrow(CosNaming.NamingContext)
+                try:
+                  name = [CosNaming.NameComponent(server_id, "Object")]
+                  object = context.resolve(name)
+                except:
+                  print >> sys.stderr, \
+                        "ERROR: Could not find specified server-id in" \
+                        "nameservice\n"
+                  sys.exit(1)
+            elif export_ref == "corbaloc":
+                print "Not implemented yet"
+                sys.exit(1)
+            elif export_ref == "ior":
+                ior_filename = os.path.join(ior_file_path, server_id)
+                try:
+                    ior_file = open(ior_filename)
+                    stringified_ior = ior_file.readline().rstrip()
+                except:
+                    print >> sys.stderr, "ERROR: Cannot open %s\n" \
+                      "This could signify that the server is not running\n" \
+                      "or that a server is running" \
+                      "but with a different -I option parameter.\n" \
+                      "If the latter, use the same option\n" \
+                      "with this client as with the server.\n" \
+                      "This could also signify that a server is running but\n" \
+                      "its address is being\n" \
+                      "published using another method; try the -R option." \
+                      % ior_filename
+                    sys.exit(1)
+                object = self.orb.string_to_object(stringified_ior)
+                ior_file.close()
+                del ior_file
+        self.server = object._narrow(Fresco.Server)
+        self.context  = self.server.create_server_context(self.clientContext)
+        #print "Got Server var =",self.server
+        #print "Client Context var =",self.clientContext
 
 class KBThread (threading.Thread):
     """A thread that waits for the user to hit enter, and then signals an
     application quit. This assumes there is a global var called 'app' with an
     attribute called 'quit' that is a threading.Event object"""
     def run(self):
-	"Run until enter"
-	print "Hit ENTER to quit."
-	while 1:
-	    line = sys.stdin.readline()
-	    if len(line) == 1: break
-	self.do_quit()
+        "Run until enter"
+        print "Hit ENTER to quit."
+        while 1:
+            line = sys.stdin.readline()
+            if len(line) == 1: break
+        self.do_quit()
     def do_quit(self):
-	"Override if you want to quit some other way"
-	app.quit.set()
+        "Override if you want to quit some other way"
+        app.quit.set()
 
 class App:
     "Base class for applications with some default useful functionality"
     def __init__(self):
-	"""Creates global kits and connection objects, the app.quit Event
-	object, and a KBThread to set it."""
-	global app
-	app = self
-	self.quit = threading.Event() # set quit to.. quit :)
-	self.kbthread = KBThread()
-	self.kbthread.setDaemon(1) # dont wait for kb thread :)
-	self.kbthread.start()
+        """Creates global kits and connection objects, the app.quit Event
+        object, and a KBThread to set it."""
+        global app
+        app = self
+        self.quit = threading.Event() # set quit to.. quit :)
+        self.kbthread = KBThread()
+        self.kbthread.setDaemon(1) # dont wait for kb thread :)
+        self.kbthread.start()
 
 
 def Label(string):
@@ -175,11 +241,11 @@
     """Small wrapper command which calls a python function with a single
     argument which is the Any."""
     def __init__(self, callback):
-	self.callback = callback
+        self.callback = callback
     def execute(self, any):
-	print "Callback to",self.callback,"with",any
-	if self.callback:
-	    self.callback(any)
+        print "Callback to",self.callback,"with",any
+        if self.callback:
+            self.callback(any)
 
 
 # vim: ts=8:sts=4:sw=4
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.