From ffefb533254bb77390054e6e99c2fe1dfc8d5483 Mon Sep 17 00:00:00 2001 From: Antoine Millet Date: Mon, 3 Oct 2011 14:45:33 +0200 Subject: [PATCH] Added a basic usage --- doc/fundamentals.rst | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/doc/fundamentals.rst b/doc/fundamentals.rst index 55729b8..25861ac 100644 --- a/doc/fundamentals.rst +++ b/doc/fundamentals.rst @@ -18,6 +18,60 @@ Features * **Fallback mode:** for compatibility with olders sjRpc. +Basic usage +----------- + +Server side, create the handler +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The handler is a simple object with a dict interface (getitem) responsible of +the association between the remote-function name and the locally executed +callable. The :class:`sjrpc.utils.Handler` class allow you to define +an handle with a simple class extending this one:: + + >>> class MyHandler(RpcHandler): + + ... def random(self, min=0, max=100): + ... return random.randint(min, max) + ... + >>> handler = MyHandler() + +But if you want to use a standard dictionnary, this is exactly the same:: + + >>> handler = {'random': lambda min, max: random.randint(min, max)} + +Server side, create the :class:`~sjrpc.core.RpcServer` instance +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The last thing to do on the server side is to launch the server itself:: + + >>> from sjrpc.server import RpcServer + >>> serv = RpcServer.from_addr('127.0.0.1', 1337n conn_kw=dict(handler=handler)) + >>> serv.run() + +.. note:: + + `conn_args` and `conn_kw` are the arguments which are automatically passed + to each client :class:`~sjrpc.core.RpcConnection` instanciation. In this + example, we just pass a default handler. + +Client side, just connect ! +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +For a basic client usage, the only thing you have to do is to create the +:class:`RpcConnection` instance to the server, nothing more simple:: + + >>> conn = RpcConnection.from_addr('127.0.0.1', 1337) + >>> print conn.call('random') + 42 + +You can also use a proxy to simplify remote calls:: + + >>> from sjrpc.utils import ConnectionProxy + >>> proxy = ConnectionProxy(conn) + >>> print proxy.random(min=42, max=1000) + 587 + Multiplexing & protocols ------------------------ -- GitLab