summaryrefslogtreecommitdiff
path: root/src/leap/common/events/__init__.py
blob: c949080ed02e90a2a54c4e15981d88581c9f67b7 (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
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
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
# __init__.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
An events mechanism that allows for signaling of events between components.
"""

import logging
import socket


from leap.common.events import (
    events_pb2,
    server,
    component,
    daemon,
)


logger = logging.getLogger(__name__)


def register(signal, callback, uid=None, replace=False, reqcbk=None,
             timeout=1000):
    """
    Register a callback to be called when the given signal is received.

    Will timeout after timeout ms if response has not been received. The
    timeout arg is only used for asynch requests. If a reqcbk callback has
    been supplied the timeout arg is not used. The response value will be
    returned for a synch request but nothing will be returned for an asynch
    request.

    @param signal: the signal that causes the callback to be launched
    @type signal: int (see the `events.proto` file)
    @param callback: the callback to be called when the signal is received
    @type callback: function
    @param uid: a unique id for the callback
    @type uid: int
    @param replace: should an existent callback with same uid be replaced?
    @type replace: bool
    @param reqcbk: a callback to be called when a response from server is
        received
    @type reqcbk: function
        callback(leap.common.events.events_pb2.EventResponse)
    @param timeout: the timeout for synch calls
    @type timeout: int

    @return: the response from server for synch calls or nothing for asynch
        calls
    @rtype: leap.common.events.events_pb2.EventsResponse or None
    """
    return component.register(signal, callback, uid, replace, reqcbk, timeout)


def signal(signal, content="", mac_method="", mac="", reqcbk=None,
           timeout=1000):
    """
    Send `signal` event to events server.

    Will timeout after timeout ms if response has not been received. The
    timeout arg is only used for asynch requests.  If a reqcbk callback has
    been supplied the timeout arg is not used. The response value will be
    returned for a synch request but nothing will be returned for an asynch
    request.

    @param signal: the signal that causes the callback to be launched
    @type signal: int (see the `events.proto` file)
    @param content: the contents of the event signal
    @type content: str
    @param mac_method: the method used to auth mac
    @type mac_method: str
    @param mac: the content of the auth mac
    @type mac: str
    @param reqcbk: a callback to be called when a response from server is
        received
    @type reqcbk: function
        callback(leap.common.events.events_pb2.EventResponse)
    @param timeout: the timeout for synch calls
    @type timeout: int

    @return: the response from server for synch calls or nothing for asynch
        calls
    @rtype: leap.common.events.events_pb2.EventsResponse or None
    """
    return component.signal(signal, content, mac_method, mac, reqcbk, timeout)