magic-wormhole/src/wormhole/wormhole.py

111 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import print_function, absolute_import, unicode_literals
2017-02-23 01:02:01 +00:00
import sys
from .timing import DebugTiming
2017-02-22 20:51:53 +00:00
from .journal import ImmediateJournal
2017-02-23 02:21:47 +00:00
from ._boss import Boss
2016-05-21 01:49:20 +00:00
2017-02-23 02:21:47 +00:00
class _Wormhole(object):
def __init__(self):
self._code = None
self._code_observers = []
self._verifier = None
self._verifier_observers = []
def _set_boss(self, boss):
self._boss = boss
# from above
def when_code(self):
if self._code:
return defer.succeed(self._code)
d = defer.Deferred()
self._code_observers.append(d)
return d
def when_verifier(self):
if self._verifier:
return defer.succeed(self._verifier)
d = defer.Deferred()
self._verifier_observers.append(d)
return d
def send(self, phase, plaintext):
self._boss.send(phase, plaintext)
def close(self):
self._boss.close()
# from below
def got_code(self, code):
self._code = code
for d in self._code_observers:
d.callback(code)
self._code_observers[:] = []
def got_verifier(self, verifier):
self._verifier = verifier
for d in self._verifier_observers:
d.callback(verifier)
self._verifier_observers[:] = []
def received(self, phase, plaintext):
print(phase, plaintext)
def closed(self, result):
print("closed", result)
def wormhole(appid, relay_url, reactor,
tor_manager=None, timing=None,
journal=None,
stderr=sys.stderr,
):
timing = timing or DebugTiming()
2017-02-23 02:21:47 +00:00
code_length = 2
side = bytes_to_hexstr(os.urandom(5))
journal = journal or ImmediateJournal()
w = _Wormhole()
b = Boss(w, side, relay_url, appid, reactor, journal, timing)
w._set_boss(b)
# force allocate for now
b.start()
b.allocate(code_length)
2017-01-06 16:25:32 +00:00
w = _Wormhole(appid, relay_url, reactor, tor_manager, timing, stderr)
2016-05-21 01:49:20 +00:00
w._start()
return w
#def wormhole_from_serialized(data, reactor, timing=None):
# timing = timing or DebugTiming()
# w = _Wormhole.from_serialized(data, reactor, timing)
# return w
2017-02-14 07:12:57 +00:00
# considerations for activity management:
# * websocket to server wants to be a t.a.i.ClientService
# * if Wormhole is a MultiService:
# * makes it easier to chain the ClientService to it
# * implies that nothing will happen before w.startService()
# * implies everything stops upon d=w.stopService()
# * if not:
# *
2017-02-23 00:56:39 +00:00
class _JournaledWormhole(object):
2017-02-14 07:12:57 +00:00
def __init__(self, reactor, journal_manager, event_dispatcher,
event_dispatcher_args=()):
pass
class _Wormhole(_JournaledWormhole):
# send events to self, deliver them via Deferreds
def __init__(self, reactor):
2017-02-22 20:51:53 +00:00
_JournaledWormhole.__init__(self, reactor, ImmediateJournal(), self)
2017-02-14 07:12:57 +00:00
2017-02-23 01:02:01 +00:00
def wormhole2(reactor):
2017-02-14 07:12:57 +00:00
w = _Wormhole(reactor)
w.startService()
return w
def journaled_from_data(state, reactor, journal,
event_handler, event_handler_args=()):
pass
2017-02-22 20:51:53 +00:00
def journaled(reactor, journal, event_handler, event_handler_args=()):
2017-02-14 07:12:57 +00:00
pass