summaryrefslogtreecommitdiff
path: root/src/leap/common/events/README.rst
blob: 2e7f254e4e27c07920ece45d69c978b3ffd2c648 (plain)
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
Events mechanism
================

The events mechanism allows for clients to send signal events to each
other by means of a centralized server. Clients can register with the
server to receive signals of certain types, and they can also send signals to
the server that will then redistribute these signals to registered clients.


Listening daemons
-----------------

Both clients and the server listen for incoming messages by using a
listening daemon that runs in its own thread. The server daemon has to be
started explicitly, while clients daemon will be started whenever a
client registers with the server to receive messages.


How to use it
-------------

To start the events server:

>>> from leap.common.events import server
>>> server.ensure_server(port=8090)

To register a callback to be called when a given signal is raised:

>>> from leap.common.events import (
>>>     register,
>>>     events_pb2 as proto,
>>> )
>>>
>>> def mycallback(sigreq):
>>>     print str(sigreq)
>>>
>>> events.register(signal=proto.CLIENT_UID, callback=mycallback)

To signal an event:

>>> from leap.common.events import (
>>>     signal,
>>>     events_pb2 as proto,
>>> )
>>> signal(proto.CLIENT_UID)

Adding events
-------------

* Add the new event under enum ``Event`` in ``events.proto`` 
* Compile the new protocolbuffers file::

  make