wslink.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. r"""wslink is a module that extends any
  2. wslink related classes for the purposes of vtkWeb.
  3. """
  4. from __future__ import absolute_import, division, print_function
  5. # import inspect, types, string, random, logging, six, json, re, base64
  6. import json, base64, logging, time
  7. from vtkmodules.web.errors import WebDependencyMissingError
  8. try:
  9. from wslink import websocket
  10. from wslink import register as exportRpc
  11. except ImportError:
  12. raise WebDependencyMissingError()
  13. from vtkmodules.web import protocols
  14. from vtkmodules.vtkWebCore import vtkWebApplication
  15. # =============================================================================
  16. application = None
  17. # =============================================================================
  18. #
  19. # Base class for vtkWeb ServerProtocol
  20. #
  21. # =============================================================================
  22. class ServerProtocol(websocket.ServerProtocol):
  23. """
  24. Defines the core server protocol for vtkWeb. Adds support to
  25. marshall/unmarshall RPC callbacks that involve ServerManager proxies as
  26. arguments or return values.
  27. Applications typically don't use this class directly, but instead
  28. sub-class it and call self.registerVtkWebProtocol() with useful vtkWebProtocols.
  29. """
  30. def __init__(self):
  31. logging.info("Creating SP")
  32. self.setSharedObject("app", self.initApplication())
  33. websocket.ServerProtocol.__init__(self)
  34. def initApplication(self):
  35. """
  36. Let subclass optionally initialize a custom application in lieu
  37. of the default vtkWebApplication.
  38. """
  39. global application
  40. if not application:
  41. application = vtkWebApplication()
  42. return application
  43. def setApplication(self, application):
  44. self.setSharedObject("app", application)
  45. def getApplication(self):
  46. return self.getSharedObject("app")
  47. def registerVtkWebProtocol(self, protocol):
  48. self.registerLinkProtocol(protocol)
  49. def getVtkWebProtocols(self):
  50. return self.getLinkProtocols()