home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / orb / DII.py < prev    next >
Text File  |  1999-06-28  |  10KB  |  341 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/DII.py,v $
  29. # Version:      @(#)$RCSfile: DII.py,v $ $Revision: 1.14 $
  30. #
  31. #############################################################################
  32. """ Implementation of the Dynamic Invocation Interface (DII). """
  33.  
  34.  
  35. class Request:
  36.     """ Implementation of the PIDL Request interface.
  37.  
  38.     Request objects are not thread-safe. This is not a problem as it really
  39.     only makes sense to handle requests from within a single thread. If you
  40.     are trying to do otherwise I suggest you take a step back and have a good,
  41.     long, hard look at yourself ;^)
  42.  
  43.     """
  44.     def __init__(self, object, operation, inputs, outputs, exceptions, **kw):
  45.     """ Constructor. """
  46.  
  47.     self.__object = object
  48.     self.__operation = operation
  49.     self.__inputs = inputs
  50.     self.__outputs = outputs
  51.     self.__exceptions = exceptions
  52.  
  53.     # Keyword arguments (as specified in the language mapping document).
  54.     #
  55.     # The 'flags' keyword argument is currently ignored as it is only
  56.     # used in compiled languages to specify memory management policy.
  57.     try:
  58.         self.__flags = kw['flags']
  59.  
  60.     except KeyError:
  61.         self.__flags = 0
  62.  
  63.     try:
  64.         self.__context = kw['context']
  65.  
  66.     except KeyError:
  67.         self.__context = []
  68.  
  69.     # Request arguments can be specified either at invocation time, or via
  70.     # a call to the 'arguments' method.
  71.     self.__arguments = ()
  72.  
  73.     return
  74.  
  75.     #########################################################################
  76.     # CORBA Request interface.
  77.     #########################################################################
  78.  
  79.     def object(self):
  80.     """ Return the object reference that we invoke the request on. """
  81.     
  82.     return self.__object
  83.  
  84.     def operation(self):
  85.     """ Return the operation name. """
  86.  
  87.     return self.__operation
  88.     
  89.     def inputs(self):
  90.     """ Return the input typecodes. """
  91.  
  92.     return self.__inputs
  93.  
  94.     def outputs(self):
  95.     """ Return the output typecodes. """
  96.  
  97.     return self.__outputs
  98.  
  99.     def exceptions(self):
  100.     """ Return the exception typecodes. """
  101.  
  102.     return self.__exceptions
  103.  
  104.     def flags(self):
  105.     """ Return the request flags.
  106.  
  107.     Request flags are currently ignored in Fnorb!
  108.  
  109.     """
  110.     return self.__flags
  111.  
  112.     def context(self):
  113.     """ Return the request context.
  114.  
  115.     Contexts are currently ignored in Fnorb!
  116.  
  117.     """
  118.     return self.__context
  119.  
  120.     def arguments(self, *args):
  121.     """ Initialise the request arguments. """
  122.  
  123.     self.__arguments = args
  124.     return
  125.  
  126.     def results(self):
  127.     """ Return the results of the previous invocation. """
  128.  
  129.     # fixme: Raise an exception here if we have not been invoked!!!!!!!!!
  130.     return self.__results
  131.  
  132. ##     def invoke(self, *args, **kw):
  133. ##     """ Invoke the request (synchronous). """
  134.  
  135. ##     apply(self.send, args, kw)
  136. ##     return self.get_response()
  137.  
  138.  
  139.     def invoke(self, *args, **kw):
  140.     """ Invoke the request (synchronous). """
  141.  
  142.     # Request arguments can be specified either at invocation time, or via
  143.     # a call to the 'arguments' method.
  144.     if len(args) == 0:
  145.         args = self.__arguments
  146.  
  147.     else:
  148.         self.__arguments = args
  149.  
  150.     # Make sure we have the right number of arguments!
  151.     if len(args) != len(self.__inputs):
  152.         raise TypeError, 'Wrong number of arguments'
  153.  
  154.     # Get the object's GIOP client.
  155.     client = self.__object._fnorb_client()
  156.  
  157.     # Ask the client to make the request, and to wait for the reply.
  158.     (forwarded, self.__results) = client.synchronous(self, args)
  159.  
  160.     # If we have been forwarded/unforwarded then try again.
  161.     if forwarded:
  162.         # Try again.
  163.         self.__results = self.invoke()
  164.  
  165.     # Return the results for convenience.
  166.     return self.__results
  167.  
  168.     def send(self, *args, **kw):
  169.     """ Invoke the request (deferred synchronous). """
  170.  
  171.     # Request arguments can be specified either at invocation time, or via
  172.     # a call to the 'arguments' method.
  173.     if len(args) == 0:
  174.         args = self.__arguments
  175.  
  176.     else:
  177.         self.__arguments = args
  178.  
  179.     # Make sure we have the right number of arguments!
  180.     if len(args) != len(self.__inputs):
  181.         raise TypeError, 'Wrong number of arguments'
  182.  
  183.     # Get the object's GIOP client.
  184.     client = self.__object._fnorb_client()
  185.  
  186.     # Ask the client to make the request.
  187.     self.__request_id = client.deferred(self, args)
  188.  
  189.     return
  190.  
  191.     def get_response(self):
  192.     """ Get the response for a deferred request. """
  193.  
  194.     # Get the object's GIOP client.
  195.     client = self.__object._fnorb_client()
  196.  
  197.     # Get the reply to the last operation request.
  198.     (forwarded, self.__results) = client.reply(self)
  199.  
  200.     # If we have been forwarded/unforwarded then try again.
  201.     if forwarded:
  202.         # Try again.
  203.         self.__results = self.invoke()
  204.  
  205.     # Return the results for convenience.
  206.     return self.__results
  207.  
  208.     def poll_response(self):
  209.     """ Has a reply been received? """
  210.  
  211.     # Get the object's GIOP client.
  212.     client = self.__object._fnorb_client()
  213.  
  214.     # Poll for the reply to the last operation request.
  215.     (forwarded, result) = client.poll(self)
  216.  
  217.     # If we have been forwarded/unforwarded then try again.
  218.     if forwarded:
  219.         # Try again.
  220.         self.send()
  221.  
  222.     return result
  223.  
  224.     def invoke_oneway(self, *args, **kw):
  225.     """ Invoke the request as 'oneway'. """
  226.  
  227.     # Request arguments can be specified either at invocation time, or via
  228.     # a call to the 'arguments' method.
  229.     if len(args) == 0:
  230.         args = self.__arguments
  231.  
  232.     else:
  233.         self.__arguments = args
  234.  
  235.     # Make sure we have the right number of arguments!
  236.     if len(args) != len(self.__inputs):
  237.         raise TypeError, 'Wrong number of arguments'
  238.  
  239.     # Get the object's GIOP client.
  240.     client = self.__object._fnorb_client()
  241.  
  242.     # Ask the client to make the request.
  243.     client.oneway(self, args)
  244.  
  245.     return
  246.  
  247.     #########################################################################
  248.     # Fnorb-specific interface.
  249.     #########################################################################
  250.  
  251.     def _fnorb_request_id(self):
  252.     """ Return the request id of the current request. """
  253.  
  254.     return self.__request_id
  255.  
  256.  
  257. class LocalRequest(Request):
  258.     """ Request interface for local requests (ie. in same address space). """
  259.  
  260.     #########################################################################
  261.     # CORBA Request interface.
  262.     #########################################################################
  263.  
  264.     def invoke(self, *args):
  265.     """ Invoke the request (synchronous). """
  266.  
  267.     # Request arguments can be specified either at invocation time, or via
  268.     # a call to the 'arguments' method.
  269.     if len(args) == 0:
  270.         args = self.__arguments
  271.  
  272.     # Make sure we have the right number of arguments!
  273.     if len(args) != len(self.__inputs):
  274.         raise TypeError, 'Wrong number of arguments'
  275.     
  276.     # Find the method on the implementation (via the 'LocalObject'
  277.     # instance).
  278.     method = getattr(self.__object, self.__operation)
  279.  
  280.     # Invoke the operation!
  281.     self.__results = apply(method, args)
  282.  
  283.     # Return the results for convenience!
  284.     return self.__results
  285.  
  286.     def invoke_oneway(self, *args):
  287.     """ Invoke the request as 'oneway'. """
  288.  
  289.     # Request arguments can be specified either at invocation time, or via
  290.     # a call to the 'arguments' method.
  291.     if len(args) == 0:
  292.         args = self.__arguments
  293.  
  294.     # Make sure we have the right number of arguments!
  295.     if len(args) != len(self.__inputs):
  296.         raise TypeError
  297.  
  298.     # Find the method on the implementation (via the 'LocalObject'
  299.     # instance).
  300.     method = getattr(self.__object, self.__operation)
  301.  
  302.     # Invoke the operation and throw away any results!
  303.     apply(method, args)
  304.  
  305.     return
  306.  
  307.     def send(self, *args):
  308.     """ Invoke the request (deferred synchronous). """
  309.  
  310.     # Request arguments can be specified either at invocation time, or via
  311.     # a call to the 'arguments' method.
  312.     if len(args) == 0:
  313.         args = self.__arguments
  314.  
  315.     # Make sure we have the right number of arguments!
  316.     if len(args) != len(self.__inputs):
  317.         raise TypeError
  318.  
  319.     # Find the method on the implementation (via the 'LocalObject'
  320.     # instance).
  321.     method = getattr(self.__object, self.__operation)
  322.  
  323.     # Invoke the operation!
  324.     self.__results = apply(method, args)
  325.  
  326.     return
  327.  
  328.     def poll_response(self):
  329.     """ Has a reply been received? """
  330.  
  331.     return 1
  332.  
  333.     def get_response(self):
  334.     """ Get the response for a deferred request. """
  335.  
  336.     return self.__results
  337.  
  338. #############################################################################
  339.