Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connect.py 2.7KB

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