connect.py :  » Windows » pyExcelerator » pywin32-214 » com » win32com » demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » com » win32com » demos » connect.py
# Implements _both_ a connectable client, and a connectable server.
#
# Note that we cheat just a little - the Server in this demo is not created
# via Normal COM - this means we can avoid registering the server.
# However, the server _is_ accessed as a COM object - just the creation
# is cheated on - so this is still working as a fully-fledged server.

import pythoncom
import win32com.server.util
import win32com.server.connect
from win32com.server.exception import Exception
from pywin32_testutil import str2bytes

# This is the IID of the Events interface both Client and Server support.
IID_IConnectDemoEvents = pythoncom.MakeIID("{A4988850-49C3-11d0-AE5D-52342E000000}")

# The server which implements
# Create a connectable class, that has a single public method
# 'DoIt', which echos to a single sink 'DoneIt'

class ConnectableServer(win32com.server.connect.ConnectableServer):
  _public_methods_ = ["DoIt"] + win32com.server.connect.ConnectableServer._public_methods_
  _connect_interfaces_ = [IID_IConnectDemoEvents]
  # The single public method that the client can call on us
  # (ie, as a normal COM server, this exposes just this single method.
  def DoIt(self,arg):
    # Simply broadcast a notification.
    self._BroadcastNotify(self.NotifyDoneIt, (arg,))

  def NotifyDoneIt(self, interface, arg):
    interface.Invoke(1000, 0, pythoncom.DISPATCH_METHOD, 1, arg)

# Here is the client side of the connection world.
# Define a COM object which implements the methods defined by the
# IConnectDemoEvents interface.                
class ConnectableClient:
  # This is another cheat - I _know_ the server defines the "DoneIt" event
  # as DISPID==1000 - I also know from the implementation details of COM
  # that the first method in _public_methods_ gets 1000.
  # Normally some explicit DISPID->Method mapping is required.
  _public_methods_ = ["OnDoneIt"]
  def __init__(self):
    self.last_event_arg = None
  # A client must implement QI, and respond to a query for the Event interface.
  # In addition, it must provide a COM object (which server.util.wrap) does.
  def _query_interface_(self, iid):
    import win32com.server.util
    # Note that this seems like a necessary hack.  I am responding to IID_IConnectDemoEvents
    # but only creating an IDispatch gateway object.
    if iid==IID_IConnectDemoEvents: return win32com.server.util.wrap(self)
  # And here is our event method which gets called.
  def OnDoneIt(self, arg):
    self.last_event_arg = arg

def CheckEvent(server, client, val, verbose):
  client.last_event_arg = None
  server.DoIt(val)
  if client.last_event_arg != val:
    raise RuntimeError("Sent %r, but got back %r" % (val, client.last_event_arg))
  if verbose:
    print "Sent and received %r" % val

# A simple test script for all this.
# In the real world, it is likely that the code controlling the server
# will be in the same class as that getting the notifications.
def test(verbose=0):
  import win32com.client.dynamic, win32com.client.connect
  import win32com.server.policy
  server = win32com.client.dynamic.Dispatch(win32com.server.util.wrap(ConnectableServer()))
  connection = win32com.client.connect.SimpleConnection()
  client = ConnectableClient()
  connection.Connect(server, client, IID_IConnectDemoEvents)
  CheckEvent(server, client, "Hello", verbose)
  CheckEvent(server, client, str2bytes("Here is a null>\x00<"), verbose)
  CheckEvent(server, client, u"Here is a null>\x00<", verbose)
  val = u"test-\xe0\xf2" # 2 extended characters.
  CheckEvent(server, client, val, verbose)
  if verbose:
    print "Everything seemed to work!"
  # Aggressive memory leak checking (ie, do nothing!) :-)  All should cleanup OK???

if __name__=='__main__':
  test(1)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.