File: offer.py

package info (click to toggle)
gnome-keysign 1.3.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,460 kB
  • sloc: python: 5,143; xml: 126; makefile: 33; sh: 16
file content (79 lines) | stat: -rw-r--r-- 2,421 bytes parent folder | download | duplicates (3)
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
import logging
from twisted.internet.defer import inlineCallbacks, returnValue

from .wormholeoffer import WormholeOffer
from .avahioffer import AvahiHTTPOffer
try:
    from .bluetoothoffer import BluetoothOffer
except ImportError:
    BluetoothOffer = None

log = logging.getLogger(__name__)


class Offer:
    def __init__(self, key, app_id=None, w_code=None):
        self.key = key
        self.app_id = app_id
        self.w_code = w_code
        self.w_offer = None
        self.a_offer = None
        self.bt_offer = None
        self.b_data = None

    @inlineCallbacks
    def allocate_code(self, worm=True):
        self.a_offer = AvahiHTTPOffer(self.key)
        code, a_data = self.a_offer.allocate_code()
        discovery_data = [a_data]
        if worm:
            self.w_offer = WormholeOffer(self.key)
            w_info = yield self.w_offer.allocate_code()
            code, w_data = w_info
            if w_data:
                discovery_data.append(w_data)
        if BluetoothOffer:
            self.bt_offer = BluetoothOffer(self.key)
            self.b_data = yield self.bt_offer.allocate_code()
            if self.b_data:
                discovery_data.append(self.b_data)
        discovery_data = ";".join(discovery_data)
        # As design when we use both avahi and wormhole we only display
        # the wormhole code
        returnValue((code, discovery_data))

    def start(self):
        avahi_defers = self.a_offer.start()
        d = [avahi_defers] if avahi_defers else []
        if self.w_offer:
            w_d = self.w_offer.start()
            d.append(w_d)
        # If we have a Bluetooth code, so if the Bluetooth has been
        # correctly initialized
        if not self.b_data:
            log.info("Bluetooth as been skipped")
        else:
            bt_d = self.bt_offer.start()
            d.append(bt_d)
        return d

    def stop_avahi(self):
        if self.a_offer:
            self.a_offer.stop()
            # We need to deallocate the avahi object or the used port will never be released
            self.a_offer = None

    def stop_wormhole(self):
        if self.w_offer:
            self.w_offer.stop()
            self.w_offer = None

    def stop_bt(self):
        if self.bt_offer:
            self.bt_offer.stop()
            self.bt_offer = None

    def stop(self):
        self.stop_avahi()
        self.stop_wormhole()
        self.stop_bt()