home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / GIOPServerWorkerThreaded.py < prev    next >
Text File  |  1999-06-28  |  5KB  |  197 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/GIOPServerWorkerThreaded.py,v $
  29. # Version:      @(#)$RCSfile: GIOPServerWorkerThreaded.py,v $ $Revision: 1.5 $
  30. #
  31. #############################################################################
  32. """ GIOPServerWorkerThreaded class. """
  33.  
  34.  
  35. # For obvious reasons we import the *real* thread module, not 'fnorb_thread'!
  36. import thread
  37.  
  38. # Fnorb modules.
  39. import CORBA, GIOPConnectionHandler, GIOPServerWorker
  40.  
  41.  
  42. class GIOPServerWorkerThreaded(GIOPServerWorker.GIOPServerWorker):
  43.     """ IIOP Worker class. """
  44.  
  45.     def __init__(self, giop_server, connection):
  46.     """ Constructor.
  47.  
  48.     'giop_server' is the GIOPServer server that is creating the worker.
  49.     'connection'  is a connection to the 'client' ORB.
  50.  
  51.     """
  52.     self.__giop_server = giop_server
  53.     self.__connection = connection
  54.  
  55.     # Set the connection to BLOCKING mode.
  56.     self.__connection.blocking(1)
  57.     
  58.     # Create a handler to look after the connection.
  59.     self.__handler = GIOPConnectionHandler.GIOPConnectionHandler \
  60.              (self, self.__connection)
  61.  
  62.     # Flag used to detect closure in the worker thread.
  63.     self.__closed = 0
  64.  
  65.     # Mutex for the '__closed' flag.
  66.     self.__lk = thread.allocate_lock()
  67.  
  68.     # Start the worker thread.
  69.     thread.start_new_thread(self.__worker_thread, ())
  70.  
  71.     return
  72.  
  73.     def pseudo__del__(self):
  74.     """ Pseudo destructor to remove circular references.
  75.  
  76.     This method is called from '__close' only.
  77.  
  78.     """
  79.     # The GIOP server holds a (circular) reference to this instance so we
  80.     # have to explicitly clean it up.
  81.     self.__giop_server.pseudo__del__()
  82.  
  83.     # Clean up *our* reference to *it*!
  84.     del self.__giop_server
  85.  
  86.     # The handler also holds a (circular) reference to this instance so we
  87.     # have to explicitly clean it up.
  88.     self.__handler.pseudo__del__()
  89.  
  90.     # Clean up *our* reference to *it*!
  91.     del self.__handler
  92.  
  93.     return
  94.  
  95.     #########################################################################
  96.     # GIOPServerWorker interface.
  97.     #########################################################################
  98.  
  99.     def send(self, message):
  100.     """ Send a message. """
  101.  
  102.     self.__lk.acquire()
  103.     try:
  104.         # Send the entire message.
  105.         n = 0
  106.         while n < len(message):
  107.         n = n + self.__handler.send(message)
  108.  
  109.     # We can simply ignore any exceptions when sending a message as the
  110.     # worker thread will detect the error and close down the connection.
  111.         except CORBA.SystemException:
  112.         pass
  113.  
  114.     self.__lk.release()
  115.     return
  116.  
  117.     def close_connection(self):
  118.     """ Close down the server.
  119.  
  120.     Currently, Fnorb does not close down servers, so this is not used.
  121.  
  122.     """
  123.     # Close down the worker.
  124.     self.__close()
  125.  
  126.     return
  127.  
  128.     #########################################################################
  129.     # GIOPConnectionHandlerListener interface.
  130.     #########################################################################
  131.  
  132.     def message_received(self, message):
  133.     """ Called when a complete GIOP message has been received. """
  134.  
  135.     # Pass the message up to the GIOP layer.
  136.     self.__giop_server.process_giop_message(message)
  137.  
  138.     return
  139.  
  140.     def message_sent(self):
  141.     """ Called when a complete GIOP message has been sent. """
  142.  
  143.     pass
  144.  
  145.     #########################################################################
  146.     # Private interface.
  147.     #########################################################################
  148.  
  149.     def __worker_thread(self):
  150.     """ Read operation requests from the client. """
  151.  
  152.     try:
  153.         while not self.__is_closed():
  154.         # Blocking receive.
  155.         self.__handler.recv()
  156.  
  157.     except CORBA.SystemException:
  158.         pass
  159.  
  160.     # Close down the worker.
  161.     self.__close()
  162.  
  163.     # Explicitly exit the thread!
  164.     thread.exit()
  165.  
  166.     def __is_closed(self):
  167.     """ Are we closing down? """
  168.  
  169.     self.__lk.acquire()
  170.     closed = self.__closed
  171.     self.__lk.release()
  172.  
  173.     return closed
  174.  
  175.     def __close(self):
  176.     """ Close down the worker. """
  177.  
  178.      # Notify the worker thread that we are closing down.
  179.     self.__lk.acquire()
  180.     if not self.__closed:
  181.         # Close down the connection (via the handler)
  182.         self.__connection.disconnect()
  183.  
  184.         # All done!
  185.         self.__closed = 1
  186.  
  187.         # Remove circular reference.
  188.         self.pseudo__del__()
  189.  
  190.     self.__lk.release()
  191.  
  192.     return
  193.  
  194. #############################################################################
  195.