0001 """Utilities for Server Side connections. 0002 0003 A collection of helpers for server side connection points. 0004 """ 0005 import pythoncom 0006 from exception import Exception 0007 import winerror 0008 from win32com import olectl 0009 import win32com.server.util 0010 0011 # Methods implemented by the interfaces. 0012 IConnectionPointContainer_methods = ["EnumConnectionPoints","FindConnectionPoint"] 0013 IConnectionPoint_methods = ["EnumConnections","Unadvise","Advise","GetConnectionPointContainer","GetConnectionInterface"] 0014 0015 class ConnectableServer: 0016 _public_methods_ = IConnectionPointContainer_methods + IConnectionPoint_methods 0017 _com_interfaces_ = [pythoncom.IID_IConnectionPoint, pythoncom.IID_IConnectionPointContainer] 0018 # Clients must set _connect_interfaces_ = [...] 0019 def __init__(self): 0020 self.cookieNo = 0 0021 self.connections = {} 0022 # IConnectionPoint interfaces 0023 def EnumConnections(self): 0024 raise Exception(winerror.E_NOTIMPL) 0025 def GetConnectionInterface(self): 0026 raise Exception(winerror.E_NOTIMPL) 0027 def GetConnectionPointContainer(self): 0028 return win32com.server.util.wrap(self) 0029 def Advise(self, pUnk): 0030 # Creates a connection to the client. Simply allocate a new cookie, 0031 # find the clients interface, and store it in a dictionary. 0032 try: 0033 interface = pUnk.QueryInterface(self._connect_interfaces_[0],pythoncom.IID_IDispatch) 0034 except pythoncom.com_error: 0035 raise Exception(scode=olectl.CONNECT_E_NOCONNECTION) 0036 self.cookieNo = self.cookieNo + 1 0037 self.connections[self.cookieNo] = interface 0038 return self.cookieNo 0039 def Unadvise(self, cookie): 0040 # Destroy a connection - simply delete interface from the map. 0041 try: 0042 del self.connections[cookie] 0043 except KeyError: 0044 raise Exception(scode=winerror.E_UNEXPECTED) 0045 # IConnectionPointContainer interfaces 0046 def EnumConnectionPoints(self): 0047 raise Exception(winerror.E_NOTIMPL) 0048 def FindConnectionPoint(self, iid): 0049 # Find a connection we support. Only support the single event interface. 0050 if iid in self._connect_interfaces_: 0051 return win32com.server.util.wrap(self) 0052 0053 def _BroadcastNotify(self, broadcaster, extraArgs): 0054 # Broadcasts a notification to all connections. 0055 # Ignores clients that fail. 0056 for interface in self.connections.values(): 0057 try: 0058 apply(broadcaster, (interface,)+extraArgs) 0059 except pythoncom.com_error, details: 0060 self._OnNotifyFail(interface, details) 0061 0062 def _OnNotifyFail(self, interface, details): 0063 print "Ignoring COM error to connection - %s" % (`details`) 0064 0065 0066
Generated by PyXR 0.9.4