home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / Nudger.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/Nudger.py,v $
  29. # Version:      @(#)$RCSfile: Nudger.py,v $ $Revision: 1.7 $
  30. #
  31. #############################################################################
  32. """ Nudger (part of the 'Reactor' pattern). """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import socket
  37.  
  38. # Fnorb modules.
  39. import CORBA, EventHandler, Reactor
  40.  
  41.  
  42. class Nudger(EventHandler.EventHandler):
  43.     """ Nudger (part of the 'Reactor' pattern). """
  44.  
  45.     def __init__(self, host='', port=0):
  46.     """ Constructor.
  47.  
  48.     'host'    is the hostname to use when creating my socket.
  49.     'port'    is the port number to use when creating my socket.
  50.  
  51.     """
  52.     try:
  53.         # Create a socket on which to listen for connection requests.
  54.         listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  55.         listen_socket.bind(host, port)
  56.         listen_socket.listen(1)
  57.  
  58.         # Get the host name, the IP address and the port number of the
  59.         # socket.
  60.         listen_host = socket.gethostname()
  61.         (listen_ip_address, listen_port) = listen_socket.getsockname()
  62.  
  63.         # Under OSF/1 if you specify '' as the host address then the IP
  64.         # address returned by 'getsockname' will be '0.0.0.0'!
  65.         if listen_ip_address == '0.0.0.0':
  66.         listen_ip_address = socket.gethostbyname(listen_host)
  67.  
  68.         # Now create a client socket and connect it to the listening
  69.         # socket.  This uses the little-known semantics of the 'connect'
  70.         # function i.e. the call will succeed even *before* the 'accept'
  71.         # has been performed on the listening socket!  If you don't believe
  72.         # me read Stevens' Network Programming ;^)
  73.         self.__client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  74.         self.__client.connect(listen_host, listen_port)
  75.  
  76.         # And finally, we do an 'accept' to complete the connection.
  77.         (self.__server, address) = listen_socket.accept()
  78.  
  79.         # Set the server socket to NON-blocking mode.
  80.         self.__server.setblocking(0)
  81.  
  82.     except socket.error:
  83.         raise CORBA.COMM_FAILURE() # System exception.
  84.  
  85.     # Get a reference to the active reactor.
  86.     self.__reactor = Reactor.Reactor_init()
  87.  
  88.     # Register our interest in 'read' events with the Reactor.
  89.     self.__reactor.register_handler(self, Reactor.READ)
  90.  
  91.     return
  92.  
  93.     def __del__(self):
  94.     """ Destructor. """
  95.  
  96.     self.handle_close()
  97.     return
  98.  
  99.     #########################################################################
  100.     # Nudger interface.
  101.     #########################################################################
  102.  
  103.     def nudge(self):
  104.     """ Nudge myself! """
  105.  
  106.     self.__client.send('nudge!')
  107.     return
  108.  
  109.     #########################################################################
  110.     # EventHandler interface.
  111.     #########################################################################
  112.  
  113.     def handle_event(self, mask):
  114.     """ Callback method to handle all events except close events. """
  115.  
  116.     # Read event.
  117.     if mask & Reactor.READ:
  118.         self.__read_event()
  119.  
  120.     # Exception event.
  121.     if mask & Reactor.EXCEPTION:
  122.         self.__exception_event()
  123.  
  124.     return
  125.  
  126.     def handle_close(self):
  127.     """ Callback method to handle close events. """
  128.  
  129.     # Withdraw my Reactor registration.
  130.     self.__reactor.unregister_handler(self, Reactor.READ)
  131.  
  132.     # Clean up my sockets.
  133.     try:
  134.         self.__client.close()
  135.         self.__server.close()
  136.  
  137.     except socket.error:
  138.         pass
  139.  
  140.     del self.__client
  141.     del self.__server
  142.  
  143.     return
  144.  
  145.     def handle(self):
  146.     """ Return my underlying I/O handle.
  147.  
  148.     In this case, my I/O handle is the file descriptor of my socket. 
  149.  
  150.     """
  151.     return self.__server.fileno()
  152.  
  153.     #########################################################################
  154.     # Private interface.
  155.     #########################################################################
  156.  
  157.     def __read_event(self):
  158.     """ Handle a read event. """
  159.  
  160.     # We do nothing!  The sole purpose of the nudger is to trip the
  161.     # reactor out of a blocking select!
  162.     self.__server.recv(10)
  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.