home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / GIOPConnectionHandler.py < prev    next >
Text File  |  1999-06-28  |  6KB  |  193 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/GIOPConnectionHandler.py,v $
  29. # Version:      @(#)$RCSfile: GIOPConnectionHandler.py,v $ $Revision: 1.5 $
  30. #
  31. #############################################################################
  32. """ GIOPConnectionHandler class. """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import string
  37.  
  38. # Fnorb modules.
  39. import CORBA, OctetStream
  40.  
  41.  
  42. class GIOPConnectionHandlerListener:
  43.     """ Listener interface for the GIOPConnectionHandler class. """
  44.  
  45.     def message_received(self, reply_header, cursor):
  46.     """ Called when a complete GIOP message has been received. """
  47.  
  48.     pass
  49.  
  50.     def message_sent(self):
  51.     """ Called when a complete GIOP message has been sent. """
  52.     
  53.     pass
  54.  
  55.  
  56. class GIOPConnectionHandler:
  57.     """  GIOPConnectionHandler class. """
  58.     
  59.     # Maximum size of the buffer used for reading from connections (we
  60.     # deliberately do *not* make this figure a power of two to make sure that
  61.     # memory allocations will be as efficient as possible).
  62.     BUFSIZE = 8000
  63.  
  64.     def __init__(self, listener, connection):
  65.     """ Provide an interface to a remote object!
  66.  
  67.     'listener'   is the listener that is using this handler.
  68.     'connection' is the connection to handle!
  69.  
  70.     """
  71.     self.__listener = listener
  72.     self.__connection = connection
  73.  
  74.     # Details of the current incoming GIOP message.
  75.     self.__incoming = []
  76.     self.__incoming_size = 0
  77.     self.__message_size = 0
  78.  
  79.     # Details of the current outgoing GIOP message.
  80.     self.__outgoing = None
  81.     self.__outgoing_size = 0
  82.  
  83.     return
  84.  
  85.     def pseudo__del__(self):
  86.     """ Pseudo destructor to remove circular references. """
  87.  
  88.     # Clean up the reference to the listener.
  89.     del self.__listener
  90.  
  91.     return
  92.  
  93.     #########################################################################
  94.     # GIOPConnectionHandler interface.
  95.     #########################################################################
  96.  
  97.     def send(self, message):
  98.     """ Perform a single 'send' operation on the connection. """
  99.  
  100.     if self.__outgoing is None:
  101.         # Extract the string of bytes from the message.
  102.         self.__outgoing = message
  103.  
  104.     # Extract the string of bytes from the message.
  105.     data = self.__outgoing.data()
  106.  
  107.     # Starting a new message.
  108.     if self.__outgoing_size == 0:
  109.         n = self.__connection.send(data)
  110.         self.__outgoing_size = self.__outgoing_size + n
  111.  
  112.     # Continuing transmission of a previous message.
  113.         elif self.__outgoing_size > 0 and self.__outgoing_size < len(data):
  114.         n = self.__connection.send(data[self.__outgoing_size:])
  115.         self.__outgoing_size = self.__outgoing_size + n
  116.  
  117.     # If the entire message has been sent.
  118.     if self.__outgoing_size > 0 and self.__outgoing_size == len(data):
  119.         # Get ready to start the next message.
  120.         self.__outgoing = None
  121.         self.__outgoing_size = 0
  122.  
  123.         # Message sent!
  124.         self.__listener.message_sent()
  125.  
  126.     return n
  127.  
  128.     def recv(self):
  129.     """ Perform a single 'recv' operation on the connection. """
  130.     
  131.     # Read the GIOP message header.
  132.     if self.__incoming_size < 12:
  133.         # Get the next chunk of the GIOP header.
  134.         chunk = self.__connection.recv(12 - self.__incoming_size)
  135.         # Zero length reads not allowed!
  136.         if len(chunk) == 0:
  137.         raise CORBA.COMM_FAILURE(0, CORBA.COMPLETED_MAYBE)
  138.  
  139.         # Add the chunk to the incoming message.
  140.         self.__incoming_size = self.__incoming_size + len(chunk)
  141.         self.__incoming.append(chunk)
  142.  
  143.         # Have we got the entire header now?
  144.         if self.__incoming_size == 12:
  145.         # Join the chunks together.
  146.         data = string.join(self.__incoming, '')
  147.  
  148.         # Unmarshal the header.
  149.         octet_stream = OctetStream.GIOPMessage(data)
  150.  
  151.         # Get the size of the message body.
  152.         self.__message_size = octet_stream.header().message_size
  153.  
  154.     # Read the message body.
  155.     elif self.__incoming_size >= 12 and \
  156.          self.__incoming_size < self.__message_size + 12:
  157.  
  158.         # Work out the number of bytes remaining.
  159.         remaining = (self.__message_size + 12) - self.__incoming_size
  160.  
  161.         # Calculate the size of the buffer.
  162.         bufsize = min(remaining, GIOPConnectionHandler.BUFSIZE)
  163.  
  164.         # Get the next chunk of the message.
  165.         chunk = self.__connection.recv(bufsize)
  166.         if len(chunk) == 0:
  167.         raise CORBA.COMM_FAILURE(0, CORBA.COMPLETED_MAYBE)
  168.             
  169.         # Add the chunk to the incoming message.
  170.         self.__incoming_size = self.__incoming_size + len(chunk)
  171.         self.__incoming.append(chunk)
  172.  
  173.     # Has the entire message been read?
  174.     if self.__incoming_size >= 12 and \
  175.        self.__incoming_size == self.__message_size + 12:
  176.  
  177.         # Join the chunks together.
  178.         data = string.join(self.__incoming, '')
  179.  
  180.         # Reset ready for the next message.
  181.         self.__incoming = []
  182.         self.__incoming_size = 0
  183.         self.__message_size = 0
  184.  
  185.         # Message received!
  186.         self.__listener.message_received(OctetStream.GIOPMessage(data))
  187.  
  188.     return
  189.  
  190. #############################################################################
  191.