home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com_demos_connect.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.0 KB  |  68 lines

  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.  
  8. import pythoncom
  9. import win32com.server.util
  10. import win32com.server.connect
  11. from win32com.server.exception import Exception
  12.  
  13. # This is the IID of the Events interface both Client and Server support.
  14. IID_IConnectDemoEvents = pythoncom.MakeIID("{A4988850-49C3-11d0-AE5D-52342E000000}")
  15.  
  16. # The server which implements
  17. # Create a connectable class, that has a single public method
  18. # 'DoIt', which echos to a single sink 'DoneIt'
  19.  
  20. class ConnectableServer(win32com.server.connect.ConnectableServer):
  21.     _public_methods_ = ["DoIt"] + win32com.server.connect.ConnectableServer._public_methods_
  22.     _connect_interfaces_ = [IID_IConnectDemoEvents]
  23.     # The single public method that the client can call on us
  24.     # (ie, as a normal COM server, this exposes just this single method.
  25.     def DoIt(self,arg):
  26.         # Simply broadcast a notification.
  27.         self._BroadcastNotify(self.NotifyDoneIt, (arg,))
  28.  
  29.     def NotifyDoneIt(self, interface, arg):
  30.         interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)
  31.  
  32. # Here is the client side of the connection world.
  33. # Define a COM object which implements the methods defined by the
  34. # IConnectDemoEvents interface.                                
  35. class ConnectableClient:
  36.     # This is another cheat - I _know_ the server defines the "DoneIt" event
  37.     # as DISPID==1000 - I also know from the implementation details of COM
  38.     # that the first method in _public_methods_ gets 1000.
  39.     # Normally some explicit DISPID->Method mapping is required.
  40.     _public_methods_ = ["OnDoneIt"]
  41.     # A client must implement QI, and respond to a query for the Event interface.
  42.     # In addition, it must provide a COM object (which server.util.wrap) does.
  43.     def _query_interface_(self, iid):
  44.         import win32com.server.util
  45.         # Note that this seems like a necessary hack.  I am responding to IID_IConnectDemoEvents
  46.         # but only creating an IDispatch gateway object.
  47.         if iid==IID_IConnectDemoEvents: return win32com.server.util.wrap(self)
  48.     # And here is our event method which gets called.
  49.     def OnDoneIt(self, arg):
  50.         print "OnDoneIt with ", repr(arg)
  51.  
  52. # A simple test script for all this.
  53. # In the real world, it is likely that the code controlling the server
  54. # will be in the same class as that getting the notifications.
  55. def test():
  56.     import win32com.client.dynamic, win32com.client.connect
  57.     import win32com.server.policy
  58.     server = win32com.client.dynamic.Dispatch(win32com.server.util.wrap(ConnectableServer()))
  59.     connection = win32com.client.connect.SimpleConnection()
  60.     connection.Connect(server, ConnectableClient(),IID_IConnectDemoEvents)
  61.     server.DoIt("Hello")
  62.     server.DoIt("Here is a null>"+chr(0)+"<")
  63.     # Aggressive memory leak checking (ie, do nothing!) :-)  All should cleanup OK???
  64.  
  65. if __name__=='__main__':
  66.     test()
  67.  
  68.