File: rpc.rst

package info (click to toggle)
kaa-base 0.6.0%2Bsvn4596-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,348 kB
  • ctags: 3,068
  • sloc: python: 11,094; ansic: 1,862; makefile: 74
file content (93 lines) | stat: -rw-r--r-- 2,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Remote Procedure Calls with kaa.rpc
===================================

This module defines an alternative way for InterProcessCommunication
with less features than the ipc.py module. It does not keep
references, return values are only given back as a callback and it is
only possible to access functions. The difference between client and
server is only important on connect. Both can call functions for the
other.

Start a server::

    kaa.rpc.Server(address, secret)

When a new client connects to the server, the 'client_connected'
signals will be emitted with a Channel object as parameter. This
object can be used to call functions on client side the same way the
client calls functions on server side. The client and the channel
objects have a signal 'disconnected' to be called when the connection
gets lost.

Start a client::

    kaa.rpc.Client(address, secret)

Since everything is async, the challenge response is done in the background
and you can start using it right away. If the authentication is wrong, it
will fail without notifing the user (I know this is bad, but it is designed
to work internaly where everything is correct).

.. kaaclass:: kaa.rpc.Server
   :synopsis:

   .. automethods::
   .. autoproperties::
   .. autosignals::


.. kaaclass:: kaa.rpc.Client
   :synopsis:

   .. automethods::
      :inherit:
   .. autoproperties::
      :inherit:
   .. autosignals::
      :inherit:


Expose Functions
----------------

Next you need to define functions the remote side is allowed to call
and give it a name. Use use expose for that. Connect the object with
that function to the server/client. You can connect as many objects as
you want. The client can now call do_something (not my_function, this
is the internal name). If the internal name should be exposed the
expose decorator does not need its first argument::

    class MyClass(object)

        @kaa.rpc.expose("do_something")
        def my_function(self, foo):
            ...

        @kaa.rpc.expose()
        def name(self, foo):
            ...

    server.connect(MyClass())

.. autofunction:: kaa.rpc.expose

Calling Remote Functions
------------------------

A remote function call be called with the rpc method in the client or
the server. The result is an InProgress object. Connect to it to get
the result::

    x = client.rpc('do_something', 6) or
    x = client.rpc('do_something', foo=4)

Using Python 2.5 and coroutines the asynchronous call can be wrapped
in a yield statement, hiding the delay of the RPC. If the server
raises an exception, it will be raised on client side. This makes
remote functions look like local functions. Note: Python will jump
back to the mainloop in each yield::

    @kaa.coroutine()
    def foo():
        name = yield client.rpc('name')
        print name