Commit ffefb533 authored by Antoine Millet's avatar Antoine Millet
Browse files

Added a basic usage

parent 8db42ff2
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
@@ -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
------------------------