2017-02-22 19:26:11 +00:00
|
|
|
import os
|
|
|
|
from zope.interface import implementer
|
2017-02-23 00:56:39 +00:00
|
|
|
from attr import attrs, attrib
|
|
|
|
from attr.validators import provides
|
2017-02-22 19:26:11 +00:00
|
|
|
from automat import MethodicalMachine
|
|
|
|
from . import _interfaces
|
|
|
|
from .wordlist import (byte_to_even_word, byte_to_odd_word,
|
|
|
|
#even_words_lowercase, odd_words_lowercase,
|
|
|
|
)
|
|
|
|
|
|
|
|
def make_code(nameplate, code_length):
|
|
|
|
assert isinstance(nameplate, type("")), type(nameplate)
|
|
|
|
words = []
|
|
|
|
for i in range(code_length):
|
|
|
|
# we start with an "odd word"
|
|
|
|
if i % 2 == 0:
|
|
|
|
words.append(byte_to_odd_word[os.urandom(1)].lower())
|
|
|
|
else:
|
|
|
|
words.append(byte_to_even_word[os.urandom(1)].lower())
|
|
|
|
return "%s-%s" % (nameplate, "-".join(words))
|
|
|
|
|
2017-02-23 00:56:39 +00:00
|
|
|
@attrs
|
2017-02-22 19:26:11 +00:00
|
|
|
@implementer(_interfaces.ICode)
|
|
|
|
class Code(object):
|
2017-02-23 00:56:39 +00:00
|
|
|
_timing = attrib(validator=provides(_interfaces.ITiming))
|
2017-02-22 19:26:11 +00:00
|
|
|
m = MethodicalMachine()
|
2017-02-23 00:56:39 +00:00
|
|
|
|
2017-02-22 19:26:11 +00:00
|
|
|
def wire(self, wormhole, rendezvous_connector, nameplate_lister):
|
|
|
|
self._W = _interfaces.IWormhole(wormhole)
|
|
|
|
self._RC = _interfaces.IRendezvousConnector(rendezvous_connector)
|
2017-02-22 19:28:49 +00:00
|
|
|
self._NL = _interfaces.INameplateLister(nameplate_lister)
|
2017-02-22 19:26:11 +00:00
|
|
|
|
|
|
|
@m.state(initial=True)
|
|
|
|
def S0_unknown(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S1_allocating(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S2_typing_nameplate(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S3_typing_code(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S4_known(self): pass
|
|
|
|
|
|
|
|
# from App
|
|
|
|
@m.input()
|
2017-02-23 00:56:39 +00:00
|
|
|
def allocate(self, code_length): pass
|
2017-02-22 19:26:11 +00:00
|
|
|
@m.input()
|
2017-02-23 00:56:39 +00:00
|
|
|
def input(self, stdio): pass
|
2017-02-22 19:26:11 +00:00
|
|
|
@m.input()
|
|
|
|
def set(self, code): pass
|
|
|
|
|
|
|
|
# from RendezvousConnector
|
|
|
|
@m.input()
|
|
|
|
def rx_allocated(self, nameplate): pass
|
|
|
|
|
|
|
|
# from NameplateLister
|
|
|
|
@m.input()
|
|
|
|
def got_nameplates(self, nameplates): pass
|
|
|
|
|
|
|
|
# from stdin/readline/???
|
|
|
|
@m.input()
|
|
|
|
def tab(self): pass
|
|
|
|
@m.input()
|
|
|
|
def hyphen(self): pass
|
|
|
|
@m.input()
|
|
|
|
def RETURN(self, code): pass
|
|
|
|
|
|
|
|
@m.output()
|
|
|
|
def NL_refresh_nameplates(self):
|
|
|
|
self._NL.refresh_nameplates()
|
|
|
|
@m.output()
|
2017-02-23 00:56:39 +00:00
|
|
|
def start_input_and_NL_refresh_nameplates(self, stdio):
|
|
|
|
self._stdio = stdio
|
|
|
|
self._NL.refresh_nameplates()
|
|
|
|
@m.output()
|
|
|
|
def RC_tx_allocate(self, code_length):
|
|
|
|
self._code_length = code_length
|
2017-02-22 19:26:11 +00:00
|
|
|
self._RC.tx_allocate()
|
|
|
|
@m.output()
|
|
|
|
def do_completion_nameplates(self):
|
|
|
|
pass
|
|
|
|
@m.output()
|
|
|
|
def stash_nameplates(self, nameplates):
|
|
|
|
self._known_nameplates = nameplates
|
|
|
|
pass
|
|
|
|
@m.output()
|
|
|
|
def lookup_wordlist(self):
|
|
|
|
pass
|
|
|
|
@m.output()
|
|
|
|
def do_completion_code(self):
|
|
|
|
pass
|
|
|
|
@m.output()
|
|
|
|
def generate_and_set(self, nameplate):
|
|
|
|
self._code = make_code(nameplate, self._code_length)
|
2017-02-23 00:56:39 +00:00
|
|
|
self._W_got_code()
|
2017-02-22 19:26:11 +00:00
|
|
|
|
|
|
|
@m.output()
|
2017-02-23 00:56:39 +00:00
|
|
|
def W_got_code(self, code):
|
2017-02-22 19:26:11 +00:00
|
|
|
self._code = code
|
2017-02-23 00:56:39 +00:00
|
|
|
self._W_got_code()
|
2017-02-22 19:26:11 +00:00
|
|
|
|
2017-02-23 00:56:39 +00:00
|
|
|
def _W_got_code(self):
|
|
|
|
self._W.got_code(self._code)
|
2017-02-22 19:26:11 +00:00
|
|
|
|
|
|
|
S0_unknown.upon(allocate, enter=S1_allocating, outputs=[RC_tx_allocate])
|
|
|
|
S1_allocating.upon(rx_allocated, enter=S4_known, outputs=[generate_and_set])
|
|
|
|
|
2017-02-23 00:56:39 +00:00
|
|
|
S0_unknown.upon(set, enter=S4_known, outputs=[W_got_code])
|
2017-02-22 19:26:11 +00:00
|
|
|
|
|
|
|
S0_unknown.upon(input, enter=S2_typing_nameplate,
|
2017-02-23 00:56:39 +00:00
|
|
|
outputs=[start_input_and_NL_refresh_nameplates])
|
2017-02-22 19:26:11 +00:00
|
|
|
S2_typing_nameplate.upon(tab, enter=S2_typing_nameplate,
|
|
|
|
outputs=[do_completion_nameplates])
|
|
|
|
S2_typing_nameplate.upon(got_nameplates, enter=S2_typing_nameplate,
|
|
|
|
outputs=[stash_nameplates])
|
|
|
|
S2_typing_nameplate.upon(hyphen, enter=S3_typing_code,
|
|
|
|
outputs=[lookup_wordlist])
|
|
|
|
S3_typing_code.upon(tab, enter=S3_typing_code, outputs=[do_completion_code])
|
2017-02-23 00:56:39 +00:00
|
|
|
S3_typing_code.upon(RETURN, enter=S4_known, outputs=[W_got_code])
|