home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / IIOPConnection.py < prev    next >
Text File  |  1999-06-28  |  5KB  |  175 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/IIOPConnection.py,v $
  29. # Version:      @(#)$RCSfile: IIOPConnection.py,v $ $Revision: 1.8 $
  30. #
  31. #############################################################################
  32. """ IIOPConnection class. """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import select, socket, struct
  37.  
  38. # Fnorb modules.
  39. import CORBA, Connection
  40.  
  41.  
  42. class IIOPConnection(Connection.Connection):
  43.     """ IIOPConnection class. """
  44.  
  45.     def __init__(self, _socket=None):
  46.     """ Provide an IIOP connection to a remote object!
  47.  
  48.     '_socket' is a socket that is *already* connected.
  49.  
  50.     """
  51.     # If we are given a "here's one I prepared earlier" socket, then we
  52.     # use that.
  53.     #
  54.     if _socket is not None:
  55.         self.__socket = _socket
  56.         self.__connected = 1
  57.  
  58.         # The 'TCP_NODELAY' option disables the TCP Nagle algorithm which
  59.         # prevents the transmission of small packets (like for example
  60.         # 'CloseConnection' messages).
  61.         #
  62.         # fixme: We don't use symbolic constants from the 'socket' module
  63.         # as they don't appear to be present on all platforms (eg. Linux!).
  64.         # Is this just an ommision or is there a reason?!?
  65.         self.__socket.setsockopt(6, # IPPROTO_TCP
  66.                      1, # TCP_NODELAY
  67.                      1) # Option on!
  68.  
  69.     # Otherwise, we will create a socket when the 'connect' method is
  70.     # called.
  71.     else:
  72.         self.__connected = 0
  73.  
  74.     return
  75.  
  76.     #########################################################################
  77.     # Connection interface.
  78.     #########################################################################
  79.  
  80.     def connect(self, address):
  81.     """ Connect to the remote object. """
  82.  
  83.     # fixme: Need a better address abstraction than just tuples.
  84.     (host, port) = address
  85.  
  86.     # Create a socket and connect to the remote object.
  87.     try:
  88.         self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  89.         self.__socket.connect(host, port)
  90.  
  91.         # Set the socket by default to NON-blocking mode.
  92.         self.__socket.setblocking(0)
  93.  
  94.         # Connected!
  95.         self.__connected = 1
  96.  
  97.     except socket.error:
  98.         raise CORBA.COMM_FAILURE() # System exception.
  99.  
  100.     return
  101.  
  102.     def disconnect(self):
  103.     """ Disconnect from the remote server. """
  104.  
  105.     # Make sure that we are actually connected!
  106.     if self.__connected:
  107.         try:
  108.         # Close the connection.
  109.         self.__socket.close()
  110.  
  111.         except socket.error:
  112.         pass
  113.  
  114.         # Disconnected.
  115.         self.__connected = 0
  116.  
  117.     return
  118.  
  119.     def send(self, data, block=0):
  120.     """ Send as much of the data as we can. """
  121.  
  122.     try:
  123.         n = self.__socket.send(data)
  124.  
  125.     except socket.error:
  126.         raise CORBA.COMM_FAILURE(0, CORBA.COMPLETED_MAYBE)
  127.  
  128.     return n
  129.  
  130.     def recv(self, n):
  131.     """ Receive at most 'n' bytes of data. """
  132.     
  133.     try:
  134.         # Block until there is some data present on the socket.
  135.         #
  136.         # We do a 'select' instead of a blocking read, because 'select'
  137.         # can be interrupted by closing the socket. This is useful in
  138.         # the presence of threads when we need to shut down the read
  139.         # thread.
  140.         (iwtd, owtd, ewtd) = select.select([self.__socket], [], [])
  141.  
  142.         # Read the data.
  143.         data = self.__socket.recv(n)
  144.  
  145.     # fixme: The 'AttributeError' is caught because it seems that on
  146.     # Windows platforms this exception is raised when the socket has been
  147.     # closed by the peer?
  148.     except (select.error, socket.error, ValueError, AttributeError):
  149.         raise CORBA.COMM_FAILURE(0, CORBA.COMPLETED_MAYBE)
  150.     
  151.     return data
  152.  
  153.     def is_connected(self):
  154.     """ Are we connected to the remote address? """
  155.  
  156.     return self.__connected
  157.  
  158.     def blocking(self, blocking):
  159.     """ Set the connection to blocking (1) or non-blocking (0). """
  160.  
  161.     self.__socket.setblocking(blocking)
  162.     return
  163.  
  164.     def handle(self):
  165.     """ Return my underlying I/O handle.
  166.  
  167.     In this case, my I/O handle is the file descriptor of my socket. 
  168.  
  169.     """
  170.     return self.__socket.fileno()
  171.  
  172. #############################################################################
  173.