[gnome-keysign: 13/65] create offer to handle both avahi and BT



commit 4ce2b90f4ed7edd91793ca4b81e571095e9a18c9
Author: RyuzakiKK <aasonykk gmail com>
Date:   Wed Aug 9 16:14:43 2017 +0200

    create offer to handle both avahi and BT

 keysign/offer.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 keysign/send.py  | 24 +++++++++++++++++++-----
 2 files changed, 73 insertions(+), 5 deletions(-)
---
diff --git a/keysign/offer.py b/keysign/offer.py
new file mode 100644
index 0000000..5b92902
--- /dev/null
+++ b/keysign/offer.py
@@ -0,0 +1,54 @@
+import logging
+from twisted.internet.defer import returnValue
+
+from .bluetoothoffer import BluetoothOffer
+from .avahioffer import AvahiHTTPOffer
+
+log = logging.getLogger(__name__)
+
+
+class Offer:
+    def __init__(self, key):
+        self.key = key
+        self.a_offer = None
+        self.bt_offer = None
+        self.b_data = ""
+
+    def allocate_code(self):
+        self.a_offer = AvahiHTTPOffer(self.key)
+        a_info = self.a_offer.start()
+        code, a_data = a_info
+        self.bt_offer = BluetoothOffer(self.key)
+        _, self.b_data = self.bt_offer.generate_code()
+        discovery_data = a_data + ";" + self.b_data
+        # As design when we use both avahi and wormhole we only display
+        # the wormhole code
+        return code, discovery_data
+
+    def start(self):
+        # With the current workflow avahi needs to be started
+        # for allocate the code
+        d = []
+        # If we have a Bluetooth code, so if the Bluetooth has been
+        # correctly initialized
+        if 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_bt(self):
+        if self.bt_offer:
+            self.bt_offer.stop_receive()
+            self.bt_offer = None
+
+    def stop(self):
+        self.stop_avahi()
+        self.stop_bt()
diff --git a/keysign/send.py b/keysign/send.py
index 974101f..7f3080a 100755
--- a/keysign/send.py
+++ b/keysign/send.py
@@ -28,9 +28,8 @@ if  __name__ == "__main__" and __package__ is None:
 
 from .keylistwidget import KeyListWidget
 from .KeyPresent import KeyPresentWidget
-from .avahioffer import AvahiHTTPOffer
+from .offer import Offer
 from . import gpgmh
-from .bluetoothoffer import BluetoothOffer
 # We import i18n to have the locale set up for Glade
 from .i18n import _
 
@@ -47,7 +46,7 @@ class SendApp:
     call deactivate().
     """
     def __init__(self, builder=None):
-        self.avahi_offer = None
+        self.offer = None
         self.stack = None
         self.stack_saved_visible_child = None
         self.klw = None
@@ -80,14 +79,21 @@ class SendApp:
         fakekey = gpgmh.Key("","","")
         kpw = KeyPresentWidget(fakekey, builder=builder)
 
+        self.key = None
 
     @inlineCallbacks
     def on_key_activated(self, widget, key):
+        self.key = key
         log.info("Activated key %r", key)
         ####
         # Start network services
-        self.avahi_offer = AvahiHTTPOffer(key)
-        discovery_data = self.avahi_offer.start()
+        self.offer = Offer(self.key)
+        info = yield self.offer.allocate_code()
+        code, discovery_data = info
+        self.create_keypresent(code, discovery_data)
+        # We ignore the result of the defer because we don't have
+        # a result page
+        self.offer.start()
         log.info("Use this for discovering the other key: %r", discovery_data)
         ####
         # Create and show widget for key
@@ -99,6 +105,7 @@ class SendApp:
         self.kpw = kpw
 
     def deactivate(self):
+        self._deactivate_offer()
         ####
         # Stop network services
         avahi_offer = self.avahi_offer
@@ -112,6 +119,13 @@ class SendApp:
         self.kpw = None
         self.stack_saved_visible_child = None
 
+    def _deactivate_offer(self):
+        # Stop network services
+        if self.offer:
+            self.offer.stop()
+            self.offer = None
+        log.debug("Stopped network services")
+
 
 class App(Gtk.Application):
     def __init__(self, *args, **kwargs):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]