connect.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Implements _both_ a connectable client, and a connectable server.
  2. #
  3. # Note that we cheat just a little - the Server in this demo is not created
  4. # via Normal COM - this means we can avoid registering the server.
  5. # However, the server _is_ accessed as a COM object - just the creation
  6. # is cheated on - so this is still working as a fully-fledged server.
  7. import pythoncom
  8. import win32com.server.util
  9. import win32com.server.connect
  10. from win32com.server.exception import Exception
  11. from pywin32_testutil import str2bytes
  12. # This is the IID of the Events interface both Client and Server support.
  13. IID_IConnectDemoEvents = pythoncom.MakeIID("{A4988850-49C3-11d0-AE5D-52342E000000}")
  14. # The server which implements
  15. # Create a connectable class, that has a single public method
  16. # 'DoIt', which echos to a single sink 'DoneIt'
  17. class ConnectableServer(win32com.server.connect.ConnectableServer):
  18. _public_methods_ = ["DoIt"] + win32com.server.connect.ConnectableServer._public_methods_
  19. _connect_interfaces_ = [IID_IConnectDemoEvents]
  20. # The single public method that the client can call on us
  21. # (ie, as a normal COM server, this exposes just this single method.
  22. def DoIt(self,arg):
  23. # Simply broadcast a notification.
  24. self._BroadcastNotify(self.NotifyDoneIt, (arg,))
  25. def NotifyDoneIt(self, interface, arg):
  26. interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)
  27. # Here is the client side of the connection world.
  28. # Define a COM object which implements the methods defined by the
  29. # IConnectDemoEvents interface.
  30. class ConnectableClient:
  31. # This is another cheat - I _know_ the server defines the "DoneIt" event
  32. # as DISPID==1000 - I also know from the implementation details of COM
  33. # that the first method in _public_methods_ gets 1000.
  34. # Normally some explicit DISPID->Method mapping is required.
  35. _public_methods_ = ["OnDoneIt"]
  36. def __init__(self):
  37. self.last_event_arg = None
  38. # A client must implement QI, and respond to a query for the Event interface.
  39. # In addition, it must provide a COM object (which server.util.wrap) does.
  40. def _query_interface_(self, iid):
  41. import win32com.server.util
  42. # Note that this seems like a necessary hack. I am responding to IID_IConnectDemoEvents
  43. # but only creating an IDispatch gateway object.
  44. if iid==IID_IConnectDemoEvents: return win32com.server.util.wrap(self)
  45. # And here is our event method which gets called.
  46. def OnDoneIt(self, arg):
  47. self.last_event_arg = arg
  48. def CheckEvent(server, client, val, verbose):
  49. client.last_event_arg = None
  50. server.DoIt(val)
  51. if client.last_event_arg != val:
  52. raise RuntimeError("Sent %r, but got back %r" % (val, client.last_event_arg))
  53. if verbose:
  54. print("Sent and received %r" % val)
  55. # A simple test script for all this.
  56. # In the real world, it is likely that the code controlling the server
  57. # will be in the same class as that getting the notifications.
  58. def test(verbose=0):
  59. import win32com.client.dynamic, win32com.client.connect
  60. import win32com.server.policy
  61. server = win32com.client.dynamic.Dispatch(win32com.server.util.wrap(ConnectableServer()))
  62. connection = win32com.client.connect.SimpleConnection()
  63. client = ConnectableClient()
  64. connection.Connect(server, client, IID_IConnectDemoEvents)
  65. CheckEvent(server, client, "Hello", verbose)
  66. CheckEvent(server, client, str2bytes("Here is a null>\x00<"), verbose)
  67. CheckEvent(server, client, "Here is a null>\x00<", verbose)
  68. val = "test-\xe0\xf2" # 2 extended characters.
  69. CheckEvent(server, client, val, verbose)
  70. if verbose:
  71. print("Everything seemed to work!")
  72. # Aggressive memory leak checking (ie, do nothing!) :-) All should cleanup OK???
  73. if __name__=='__main__':
  74. test(1)