home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / IIOPAcceptor.py < prev    next >
Text File  |  1999-06-28  |  5KB  |  177 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/IIOPAcceptor.py,v $
  29. # Version:      @(#)$RCSfile: IIOPAcceptor.py,v $ $Revision: 1.6 $
  30. #
  31. #############################################################################
  32. """ IIOPAcceptor (part of the 'Reactor' pattern). """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import socket
  37.  
  38. # Fnorb modules.
  39. import Acceptor, CORBA, GIOPServer, EventHandler, Reactor
  40.  
  41. # IIOP protocol modules.
  42. import IIOPConnection
  43.  
  44.  
  45. class IIOPAcceptor(Acceptor.Acceptor, EventHandler.EventHandler):
  46.     """ Acceptor (part of the 'Reactor' pattern). """
  47.  
  48.     def __init__(self, address=None):
  49.     """ Constructor.
  50.  
  51.     'address'  is the address to use when creating my endpoint.
  52.  
  53.     """
  54.     # Unpack the address.
  55.     #
  56.     # fixme: address abstraction!
  57.     if address is None:
  58.         (host, port) = ('', 0)
  59.  
  60.     else:
  61.         (host, port) = address
  62.  
  63.     try:
  64.         # Create a socket on which to listen for connection requests.
  65.         self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  66.         self.__socket.bind(host, port)
  67.         self.__socket.listen(5)
  68.  
  69.         # Get the host name, the IP address and the port number of the
  70.         # socket.
  71.         self.__host = socket.gethostname()
  72.         (self.__ip_address, self.__port) = self.__socket.getsockname()
  73.  
  74. ##        # Under OSF/1 if you specify '' as the host address then the IP
  75. ##        # address returned by 'getsockname' will be '0.0.0.0'!
  76. ##        if self.__ip_address == '0.0.0.0':
  77. ##        self.__ip_address = socket.gethostbyname(self.__host)
  78.  
  79.     except socket.error:
  80.         raise CORBA.COMM_FAILURE() # System exception.
  81.  
  82.     # Get a reference to the active reactor.
  83.     self.__reactor = Reactor.Reactor_init()
  84.  
  85.     # Register our interest in read events (ie. connection requests) with
  86.     # the Reactor.
  87.     self.__reactor.register_handler(self, Reactor.READ)
  88.  
  89.     return
  90.  
  91.     #########################################################################
  92.     # Acceptor interface.
  93.     #########################################################################
  94.  
  95.     def address(self):
  96.     """ Return the address of the acceptor's endpoint. """
  97.  
  98.     # fixme: Address abstraction!
  99.     return (self.__host, self.__port)
  100.  
  101.     #########################################################################
  102.     # EventHandler interface.
  103.     #########################################################################
  104.  
  105.     def handle_event(self, mask):
  106.     """ Callback method to handle all events except close events. """
  107.  
  108.     # Read event.
  109.     if mask & Reactor.READ:
  110.         self.__read_event()
  111.  
  112.     # Exception event.
  113.     elif mask & Reactor.EXCEPTION:
  114.         self.__exception_event()
  115.  
  116.     return
  117.  
  118.     def handle_close(self):
  119.     """ Callback method to handle close events. """
  120.  
  121.     # Withdraw my Reactor registration.
  122.     self.__reactor.unregister_handler(self, Reactor.READ)
  123.  
  124.     # Clean up my socket.
  125.     try:
  126.         self.__socket.close()
  127.  
  128.     except socket.error:
  129.         pass
  130.  
  131.     del self.__socket
  132.  
  133.     return
  134.  
  135.     def handle(self):
  136.     """ Return my underlying I/O handle.
  137.  
  138.     In this case, my I/O handle is the file descriptor of my socket. 
  139.  
  140.     """
  141.     return self.__socket.fileno()
  142.  
  143.     #########################################################################
  144.     # Private interface.
  145.     #########################################################################
  146.  
  147.     def __read_event(self):
  148.     """ Handle a read event. """
  149.  
  150.     try:
  151.         # 'Accept' the connection (in TCP/IP there is, in fact, no way to
  152.         # reject it!).
  153.         (client_socket, address) = self.__socket.accept()
  154.  
  155.         # Create a 'Connection' instance to look after the socket.
  156.         connection = IIOPConnection.IIOPConnection(client_socket)
  157.  
  158.         # Create a server to handle client operation requests.
  159.         GIOPServer.GIOPServer(connection)
  160.  
  161.     except socket.error:
  162.         self.__exception_event()
  163.  
  164.     return
  165.  
  166.     def __exception_event(self):
  167.     """ Handle an exception event. """
  168.  
  169.     # Clean up.
  170.     self.handle_close()
  171.  
  172.     return
  173.     
  174. #############################################################################
  175.