home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / cos / naming / BindingIterator.py next >
Text File  |  1999-06-28  |  4KB  |  166 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/cos/naming/RCS/BindingIterator.py,v $
  29. # Version:      @(#)$RCSfile: BindingIterator.py,v $ $Revision: 1.7 $
  30. #
  31. #############################################################################
  32. """ Cos Naming Service Binding Iterators. """
  33.  
  34.  
  35. # Fnorb modules.
  36. from Fnorb.orb import fnorb_thread, BOA
  37.  
  38. # Types.
  39. import CosNaming
  40.  
  41. # Server skeleton.
  42. import CosNaming_skel
  43.  
  44.  
  45. def BindingIteratorFactory_init():
  46.     """ Initialise the Binding Iterator Factory.
  47.  
  48.     This is a factory function for the BindingIteratorFactory class (the 
  49.     factory is a singleton (ie. there can only be one instance per process)).
  50.  
  51.     """
  52.     try:
  53.     bif = BindingIteratorFactory()
  54.  
  55.     except BindingIteratorFactory, bif:
  56.     pass
  57.  
  58.     return bif
  59.  
  60.  
  61. class BindingIteratorFactory:
  62.     """ Factory for binding iterators.
  63.  
  64.     The factory is a singleton (ie. there can only be one instance per
  65.     process).
  66.  
  67.     """
  68.     # Singleton instance.
  69.     __instance = None
  70.  
  71.     def __init__(self):
  72.     """ Constructor. """
  73.  
  74.     # The factory is a singleton (ie. there can only be one instance per
  75.     # process).
  76.     if BindingIteratorFactory.__instance is not None:
  77.         raise BindingIteratorFactory.__instance
  78.  
  79.     BindingIteratorFactory.__instance = self
  80.  
  81.     return
  82.  
  83.     def create_binding_iterator(self, bindings):
  84.     """ Create a new binding iterator instance. """
  85.  
  86.     # Create the instance.
  87.     impl = BindingIterator(bindings)
  88.  
  89.     # Create an object reference with a 'random' object key.
  90.     boa = BOA.BOA_init()
  91.     obj = boa.create(str(id(impl)), BindingIterator._FNORB_ID)
  92.  
  93.     # Activate the implementation (ie. connect the generated object
  94.     # reference to the implementation).
  95.     boa.obj_is_ready(obj, impl)
  96.  
  97.     return obj
  98.  
  99.  
  100. class BindingIterator(CosNaming_skel.BindingIterator_skel):
  101.     """ Binding Iterator. """
  102.  
  103.     def __init__(self, bindings):
  104.     """ Constructor. """
  105.  
  106.     self.__bindings = bindings
  107.     self.__index = 0
  108.  
  109.     # Mutex to make access to the list of bindings safe in threaded
  110.     # environments.
  111.     self.__lk = fnorb_thread.allocate_lock()
  112.  
  113.     return
  114.  
  115.     def __del__(self):
  116.     pass
  117.  
  118.     def next_one(self):
  119.     """ Return the next binding. """
  120.  
  121.     self.__lk.acquire()
  122.     if self.__index >= len(self.__bindings):
  123.         result = 0
  124.         binding = self.__bindings[-1]
  125.  
  126.     else:
  127.         result = 1
  128.         binding = self.__bindings[self.__index]
  129.         self.__index = self.__index + 1
  130.     self.__lk.release()
  131.  
  132.     return (result, binding)
  133.  
  134.     def next_n(self, how_many):
  135.     """ Return the next 'how_many' bindings. """
  136.  
  137.     self.__lk.acquire()
  138.     if self.__index >= len(self.__bindings):
  139.         result = 0
  140.         binding_list = []
  141.  
  142.     else:
  143.         result = 1
  144.         binding_list = self.__bindings[self.__index:self.__index+how_many]
  145.         self.__index = self.__index + how_many
  146.     self.__lk.release()
  147.  
  148.     return (result, binding_list)
  149.  
  150.     def destroy(self):
  151.     """ Destroy the iterator. """
  152.  
  153.     self.__lk.acquire()
  154.     del self.__bindings
  155.     self.__lk.release()
  156.  
  157.     # Disconnect the implementation from the BOA.
  158.     boa = BOA.BOA_init()
  159.     boa.deactivate_obj(self)
  160.  
  161.     return
  162.  
  163. #############################################################################
  164.