pyjamasjsonrpcmongrel2

https://www.google.com/accounts/o8/id?id=AItOawnSd-bPTPLP4S8AXXfdwRhtx5SOUYfoDOo edited this page Jan 3, 2012 · 5 revisions
  1. Pyjamas JSONROC with Mongrel2

There is now an example in the source code along with a jsonrpclib test demonstrating how to create a JSONRPC service with mongrel2 - pyjamas/pyjs/jsonrpc/mongrel2/

In Practice.

First : The Server Side.

You must create a file for serve functions and others.

File : server.py

from pyjs.jsonrpc.mongrel2 import Mongrel2JSONRPCService, jsonremote

from mongrel2 import handler

sender_id = "82209006-86FF-4982-B5EA-D1E29E55D481"

conn = handler.Connection(sender_id, "tcp://127.0.0.1:9999", "tcp://127.0.0.1:9998")

mongservice = Mongrel2JSONRPCService(conn)
@jsonremote(mongservice)
def auth(data):
    return "Connexion reussi."

while True:
    print "WAITING FOR REQUEST"
    mongservice() # handles one request

You can now run your "new pyjamas json rpc handler".. and go to the next step.

Second : The Mongrel2 server conf file.

You must declare handlers for process pyjamas jsonrpc requests.

File : server.conf

hand_auth = Handler(send_spec='tcp://127.0.0.1:9999',
                    send_ident='54c6755b-9628-40a4-9a2d-cc82a816345e',
                    recv_spec='tcp://127.0.0.1:9998', recv_ident='')


main = Server(
    uuid="2f62bd5-9e59-49cd-993c-3b6013c28f05"
    access_log="/logs/access.log"
    error_log="/logs/error.log"
    chroot="./"
    pid_file="/run/mongrel2.pid"
    default_host="localhost"
    name="main"
    port=80
    hosts=[
        Host(name="localhost", routes={
            '/scripts': hand_auth
            '/': Dir(base='html/', 
                        index_file='index.html', 
                        default_ctype='text/plain')

        })
    ]
)
settings = {"zeromq.threads": 1}

You can now load the server.conf file and start the server

Third : A bit pyjamas code

What you need to send json request to mongrel2 server.

from pyjamas.JSONService import JSONProxy

class Auth(Simple):
    def __init__(self):
        ....
        ....
        self.remote_py = AuthService()
        ....
        ....
    def onRemoteResponse(self, response, request_info):
        if response == "Connexion reussi.":
            ....
            ....
        else:
            ....

    def onRemoteError(self, code, message, request_info):
        Window.alert("Erreur lors de l'appel de la fonction d'identifiaction")


class AuthService(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "/scripts", ["auth"])

....
....
....
....

It's a firt try. i will put a full app as sample.