from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel, HorizontalPanel, ListBox
from JSONService import JSONProxy
from Timer import Timer
class JsonHelloButtonHandler:
def __init__(self, example):
self.example = example
def onClick(self, sender):
self.example.text_area.setText("sending request...")
username = self.example.login_text_area.getText()
self.example.remote_py.get_messages(username, self)
def onRemoteResponse(self, response, request_info):
txt = ''
for msg in response:
txt += msg.get("time") + " "
txt += msg.get("user_name") + ": "
txt += msg.get("message") + '\n'
self.example.text_area.setText(txt)
def onRemoteError(self, code, message, request_info):
self.example.status.setHTML("error: "+message)
class JSONRPCExample:
def onModuleLoad(self):
self.TEXT_WAITING = "Waiting for response..."
self.TEXT_ERROR = "Server Error"
self.METHOD_LOGIN = "Login"
self.METHOD_POST_MSG = "POST_MSG"
self.METHOD_GET_MSGS = "GET_MSGS"
self.METHOD_LOWERCASE = "lowercase"
self.methods = [self.METHOD_LOGIN, self.METHOD_POST_MSG, self.METHOD_GET_MSGS, self.METHOD_LOWERCASE]
self.remote_py = EchoServicePython()
self.status=Label()
self.login_text_area = TextArea()
self.login_text_area.setText(r"lkcl")
self.login_text_area.setCharacterWidth(20)
self.login_text_area.setVisibleLines(1)
self.input_area = TextArea()
self.input_area.setCharacterWidth(80)
self.input_area.setVisibleLines(2)
self.text_area = TextArea()
self.text_area.setText()
self.text_area.setCharacterWidth(80)
self.text_area.setVisibleLines(8)
self.method_list = ListBox()
self.method_list.setVisibleItemCount(1)
for method in self.methods:
self.method_list.addItem(method)
self.method_list.setSelectedIndex(0)
method_panel = HorizontalPanel()
method_panel.add(HTML("Remote string method to call: "))
method_panel.add(self.method_list)
method_panel.setSpacing(8)
self.button_py = Button("Send to Python Service", self)
self.button_handler = JsonHelloButtonHandler(self)
self.button_msg = Button("Send Message", self.button_handler)
buttons = HorizontalPanel()
buttons.add(self.button_py)
buttons.add(self.button_msg)
buttons.setSpacing(8)
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>."
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."
panel = VerticalPanel()
panel.add(HTML(info))
panel.add(self.login_text_area)
panel.add(self.input_area)
panel.add(self.text_area)
panel.add(method_panel)
panel.add(buttons)
panel.add(self.status)
RootPanel().add(panel)
self.isActive = True
self.onTimer()
def onTimer(self):
if not self.isActive:
return
Timer(5000, self)
self.button_handler.onClick(self)
def onClick(self, sender):
self.status.setText(self.TEXT_WAITING)
method = self.methods[self.method_list.getSelectedIndex()]
text = self.input_area.getText()
username = self.login_text_area.getText()
# demonstrate proxy & callMethod()
if sender == self.button_py:
if method == self.METHOD_LOGIN:
id = self.remote_py.login(username, self)
elif method == self.METHOD_POST_MSG:
id = self.remote_py.post_message(username, text, self)
self.input_area.setText("")
elif method == self.METHOD_GET_MSGS:
id = self.remote_py.get_messages(username, self)
elif method == self.METHOD_LOWERCASE:
id = self.remote_py.lowercase(text, self)
if id<0:
self.status.setText(self.TEXT_ERROR)
def onRemoteResponse(self, response, request_info):
self.status.setText(response)
def onRemoteError(self, code, message, request_info):
self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)
class EchoServicePython(JSONProxy):
def __init__(self):
JSONProxy.__init__(self, "services/EchoService.py", ["echo", "reverse", "uppercase", "lowercase"])
#JSONProxy.__init__(self, "chatservice/", ["login", "post_message", "get_messages", "lowercase"])