home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / connect.py < prev    next >
Text File  |  2000-11-11  |  2KB  |  66 lines

  1. """Utilities for Server Side connections.
  2.  
  3.   A collection of helpers for server side connection points.
  4. """
  5. import pythoncom
  6. from exception import Exception
  7. import winerror
  8. from win32com import olectl
  9. import win32com.server.util
  10.  
  11. # Methods implemented by the interfaces.
  12. IConnectionPointContainer_methods = ["EnumConnectionPoints","FindConnectionPoint"]
  13. IConnectionPoint_methods = ["EnumConnections","Unadvise","Advise","GetConnectionPointContainer","GetConnectionInterface"]
  14.  
  15. class ConnectableServer:
  16.     _public_methods_ = IConnectionPointContainer_methods + IConnectionPoint_methods
  17.     _com_interfaces_ = [pythoncom.IID_IConnectionPoint, pythoncom.IID_IConnectionPointContainer]
  18.     # Clients must set _connect_interfaces_ = [...]
  19.     def __init__(self):
  20.         self.cookieNo = 0
  21.         self.connections = {}
  22.     # IConnectionPoint interfaces
  23.     def EnumConnections(self):
  24.         raise Exception(winerror.E_NOTIMPL)
  25.     def GetConnectionInterface(self):
  26.         raise Exception(winerror.E_NOTIMPL)
  27.     def GetConnectionPointContainer(self):
  28.         return win32com.server.util.wrap(self)
  29.     def Advise(self, pUnk):
  30.         # Creates a connection to the client.  Simply allocate a new cookie,
  31.         # find the clients interface, and store it in a dictionary.
  32.         try:
  33.             interface = pUnk.QueryInterface(self._connect_interfaces_[0],pythoncom.IID_IDispatch)
  34.         except pythoncom.com_error:
  35.             raise Exception(scode=olectl.CONNECT_E_NOCONNECTION)
  36.         self.cookieNo = self.cookieNo + 1
  37.         self.connections[self.cookieNo] = interface
  38.         return self.cookieNo
  39.     def Unadvise(self, cookie):
  40.         # Destroy a connection - simply delete interface from the map.
  41.         try:
  42.             del self.connections[cookie]
  43.         except KeyError:
  44.             raise Exception(scode=winerror.E_UNEXPECTED)
  45.     # IConnectionPointContainer interfaces
  46.     def EnumConnectionPoints(self):
  47.         raise Exception(winerror.E_NOTIMPL)
  48.     def FindConnectionPoint(self, iid):
  49.         # Find a connection we support.  Only support the single event interface.
  50.         if iid in self._connect_interfaces_:
  51.             return win32com.server.util.wrap(self)
  52.  
  53.     def _BroadcastNotify(self, broadcaster, extraArgs):
  54.         # Broadcasts a notification to all connections.
  55.         # Ignores clients that fail.
  56.         for interface in self.connections.values():
  57.             try:
  58.                 apply(broadcaster, (interface,)+extraArgs)
  59.             except pythoncom.com_error, details:
  60.                 self._OnNotifyFail(interface, details)
  61.  
  62.     def _OnNotifyFail(self, interface, details):
  63.         print "Ignoring COM error to connection - %s" % (`details`)
  64.         
  65.  
  66.