home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / GIOPClientManager.py < prev    next >
Text File  |  1999-06-28  |  5KB  |  178 lines

  1. #!/usr/bin/env python
  2. #############################################################################
  3. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4. # All Rights Reserved.
  5. #
  6. # The software contained on this media is the property of the DSTC Pty
  7. # Ltd.  Use of this software is strictly in accordance with the
  8. # license agreement in the accompanying LICENSE.HTML file.  If your
  9. # distribution of this software does not contain a LICENSE.HTML file
  10. # then you have no rights to use this software in any manner and
  11. # should contact DSTC at the address below to determine an appropriate
  12. # licensing arrangement.
  13. #      DSTC Pty Ltd
  14. #      Level 7, GP South
  15. #      Staff House Road
  16. #      University of Queensland
  17. #      St Lucia, 4072
  18. #      Australia
  19. #      Tel: +61 7 3365 4310
  20. #      Fax: +61 7 3365 4311
  21. #      Email: enquiries@dstc.edu.au
  22. # This software is being provided "AS IS" without warranty of any
  23. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  24. # kind arising out of or in connection with the use or performance of
  25. # this software.
  26. #
  27. # Project:      Fnorb
  28. # File:         $Source: /units/arch/src/Fnorb/orb/RCS/GIOPClientManager.py,v $
  29. # Version:      @(#)$RCSfile: GIOPClientManager.py,v $ $Revision: 1.4 $
  30. #
  31. #############################################################################
  32. """ GIOP Client Manager module. """
  33.  
  34.  
  35. # Fnorb modules.
  36. import fnorb_thread, CORBA, GIOPClient
  37.  
  38.  
  39. def GIOPClientManager_init():
  40.     """ Initialise the GIOP Client Manager.
  41.  
  42.     This is a factory function for the GIOPClientManager class (the
  43.     GIOPClientManager is a singleton (ie. there can only be one
  44.     GIOPClientManager instance per process)).
  45.  
  46.     """
  47.     GIOPClientManager._lk.acquire()
  48.     try:
  49.     gcm = GIOPClientManager()
  50.  
  51.     except GIOPClientManager, gcm:
  52.     pass
  53.     GIOPClientManager._lk.release()
  54.  
  55.     return gcm
  56.  
  57.  
  58. class GIOPClientManager:
  59.     """ The GIOPClientManager object.
  60.  
  61.     The GIOPClientManager is a singleton (ie. there can only be one
  62.     GIOPClientManager instance per process).
  63.  
  64.     """
  65.     # Singleton instance.
  66.     __instance = None
  67.  
  68.     # Mutex to make access via the factory function thread-safe.
  69.     _lk = fnorb_thread.allocate_lock()
  70.  
  71.     def __init__(self):
  72.     """ Constructor. """
  73.  
  74.     # The GIOPClientManager is a singleton (ie. there can only be one
  75.     # GIOPClientManager instance per process).
  76.     if GIOPClientManager.__instance is not None:
  77.         raise GIOPClientManager.__instance
  78.  
  79.     GIOPClientManager.__instance = self
  80.  
  81.     # A dictionary of GIOP clients that are connected to remote objects.
  82.     self.__clients = {} # {Address: (GIOPClient, ReferenceCount)}
  83.  
  84.     # A mutex to make access to the client dictionary thread-safe.
  85.     self.__lk = fnorb_thread.allocate_lock()
  86.  
  87.     return
  88.  
  89.     #########################################################################
  90.     # GIOPClientManager interface.
  91.     #########################################################################
  92.  
  93.     def get_client(self, ior, protocol=None):
  94.     """ Get a client for the specified IOR. """
  95.  
  96.     # If no particular protocol was specified then we use the first one
  97.     # that we can get an address from.
  98.     if protocol is None:
  99.         for protocol in CORBA.ORB_init()._fnorb_protocols():
  100.         address = protocol.get_address(ior)
  101.         if address is not None:
  102.             break
  103.  
  104.         else:
  105.         address = None
  106.  
  107.     # Otherwise, we get the address from the specified protocol.
  108.     else:
  109.         address = protocol.get_address(ior)
  110.  
  111.     # If no address was found then barf!
  112.     if address is None:
  113.         raise CORBA.INV_OBJREF() # System exception.
  114.  
  115.     # The dictionary of clients is keyed on the address.
  116.     self.__lk.acquire()
  117.     try:
  118.         # If we already have a client connected to this address then use
  119.         # that.
  120.         if self.__clients.has_key(address):
  121.         (client, refcount) = self.__clients[address]
  122.         self.__clients[address] = (client, refcount + 1)
  123.  
  124.         # Else create a new client and connect it to the remote object.
  125.         else:
  126.         client = GIOPClient.GIOPClient(protocol, address)
  127.         self.__clients[address] = (client, 1)
  128.         
  129.     finally:
  130.         self.__lk.release()
  131.  
  132.     return client
  133.  
  134.     def delete_client(self, ior, protocol=None):
  135.     """ Delete the client for the specified IOR. """
  136.  
  137.     # If no particular protocol was specified then we use the first one
  138.     # that we can get an address from.
  139.     if protocol is None:
  140.         for protocol in CORBA.ORB_init()._fnorb_protocols():
  141.         address = protocol.get_address(ior)
  142.         if address is not None:
  143.             break
  144.  
  145.         else:
  146.         address = None
  147.  
  148.     # Otherwise, we get the address from the specified protocol.
  149.     else:
  150.         address = protocol.get_address(ior)
  151.  
  152.     # If no address was found then barf!
  153.     if address is None:
  154.         raise CORBA.INV_OBJREF() # System exception.
  155.  
  156.     # The dictionary of clients is keyed on the address.
  157.     self.__lk.acquire()
  158.     try:
  159.         (client, refcount) = self.__clients[address]
  160.         if refcount == 1:
  161.         # Remove the client from our table.
  162.         del self.__clients[address]
  163.  
  164.         # Call the pseudo destructor to clean up circular references.
  165.         client.pseudo__del__()
  166.  
  167.         else:
  168.         self.__clients[address] = (client, refcount - 1)
  169.         
  170.     finally:
  171.         self.__lk.release()
  172.  
  173.     return
  174.  
  175. #############################################################################
  176.