/examples/jsonrpc/JSONRPCExample.py

http://pyjamas.googlecode.com/ · Python · 95 lines · 76 code · 18 blank · 1 comment · 12 complexity · 89162b67b570eb935c1ac44278675ee6 MD5 · raw file

  1. from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox
  2. from JSONService import JSONProxy
  3. class JSONRPCExample:
  4. def onModuleLoad(self):
  5. self.TEXT_WAITING = "Waiting for response..."
  6. self.TEXT_ERROR = "Server Error"
  7. self.METHOD_ECHO = "Echo"
  8. self.METHOD_REVERSE = "Reverse"
  9. self.METHOD_UPPERCASE = "UPPERCASE"
  10. self.METHOD_LOWERCASE = "lowercase"
  11. self.methods = [self.METHOD_ECHO, self.METHOD_REVERSE, self.METHOD_UPPERCASE, self.METHOD_LOWERCASE]
  12. self.remote_php = EchoServicePHP()
  13. self.remote_py = EchoServicePython()
  14. self.status=Label()
  15. self.text_area = TextArea()
  16. self.text_area.setText(r"{'Test'} [\"String\"]")
  17. self.text_area.setCharacterWidth(80)
  18. self.text_area.setVisibleLines(8)
  19. self.method_list = ListBox()
  20. self.method_list.setVisibleItemCount(1)
  21. for method in self.methods:
  22. self.method_list.addItem(method)
  23. self.method_list.setSelectedIndex(0)
  24. method_panel = HorizontalPanel()
  25. method_panel.add(HTML("Remote string method to call: "))
  26. method_panel.add(self.method_list)
  27. method_panel.setSpacing(8)
  28. self.button_php = Button("Send to PHP Service", self)
  29. self.button_py = Button("Send to Python Service", self)
  30. buttons = HorizontalPanel()
  31. buttons.add(self.button_php)
  32. buttons.add(self.button_py)
  33. buttons.setSpacing(8)
  34. info = r"<h2>JSON-RPC Example</h2><p>This example demonstrates the calling of server services with <a href=\"http://json-rpc.org/\">JSON-RPC</a>."
  35. info += "<p>Enter some text below, and press a button to send the text to an Echo service on your server. An echo service simply sends the exact same text back that it receives."
  36. panel = VerticalPanel()
  37. panel.add(HTML(info))
  38. panel.add(self.text_area)
  39. panel.add(method_panel)
  40. panel.add(buttons)
  41. panel.add(self.status)
  42. RootPanel().add(panel)
  43. def onClick(self, sender):
  44. self.status.setText(self.TEXT_WAITING)
  45. method = self.methods[self.method_list.getSelectedIndex()]
  46. text = self.text_area.getText()
  47. # demonstrate proxy & callMethod()
  48. if sender == self.button_php:
  49. if method == self.METHOD_ECHO:
  50. id = self.remote_php.echo(text, self)
  51. elif method == self.METHOD_REVERSE:
  52. id = self.remote_php.callMethod("reverse", [text], self)
  53. elif method == self.METHOD_UPPERCASE:
  54. id = self.remote_php.uppercase(text, self)
  55. elif method == self.METHOD_LOWERCASE:
  56. id = self.remote_php.lowercase(text, self)
  57. else:
  58. if method == self.METHOD_ECHO:
  59. id = self.remote_py.echo(text, self)
  60. elif method == self.METHOD_REVERSE:
  61. id = self.remote_py.reverse(text, self)
  62. elif method == self.METHOD_UPPERCASE:
  63. id = self.remote_py.uppercase(text, self)
  64. elif method == self.METHOD_LOWERCASE:
  65. id = self.remote_py.lowercase(text, self)
  66. if id<0:
  67. self.status.setText(self.TEXT_ERROR)
  68. def onRemoteResponse(self, response, request_info):
  69. self.status.setText(response)
  70. def onRemoteError(self, code, message, request_info):
  71. self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)
  72. class EchoServicePHP(JSONProxy):
  73. def __init__(self):
  74. JSONProxy.__init__(self, "services/EchoService.php", ["echo", "reverse", "uppercase", "lowercase"])
  75. class EchoServicePython(JSONProxy):
  76. def __init__(self):
  77. JSONProxy.__init__(self, "services/EchoService.py", ["echo", "reverse", "uppercase", "lowercase"])