hack args till they work, add ALLOW_CLOSE

the diagram is a lot simpler if the only way to shut things down is to
terminate the whole process
This commit is contained in:
Brian Warner 2016-12-16 17:26:06 -08:00
parent d136028fa8
commit b826e8c73c

View File

@ -223,6 +223,7 @@ from automat import MethodicalMachine
class _ConnectionMachine(object): class _ConnectionMachine(object):
m = MethodicalMachine() m = MethodicalMachine()
ALLOW_CLOSE = True
def __init__(self, ws_url): def __init__(self, ws_url):
self._f = f = WSFactory(ws_url) self._f = f = WSFactory(ws_url)
@ -238,20 +239,21 @@ class _ConnectionMachine(object):
def first_time_connecting(self): pass def first_time_connecting(self): pass
@m.state() @m.state()
def negotiating(self): pass def negotiating(self): pass
@m.state(terminal=True)
def failed(self): pass
@m.state() @m.state()
def open(self): pass def open(self): pass
@m.state() @m.state()
def waiting(self): pass def waiting(self): pass
@m.state() @m.state()
def connecting(self): pass def connecting(self): pass
@m.state() if ALLOW_CLOSE:
def disconnecting(self): pass @m.state()
@m.state() def disconnecting(self): pass
def disconnecting2(self): pass @m.state()
@m.state(terminal=True) def disconnecting2(self): pass
def failed(self): pass @m.state(terminal=True)
@m.state(terminal=True) def closed(self): pass
def closed(self): pass
@m.input() @m.input()
@ -265,11 +267,12 @@ class _ConnectionMachine(object):
@m.input() @m.input()
def onOpen(self, ws): pass def onOpen(self, ws): pass
@m.input() @m.input()
def onClose(self): pass def onClose(self, f): pass
@m.input() @m.input()
def expire(self): pass def expire(self): pass
@m.input() if ALLOW_CLOSE:
def close(self): pass @m.input()
def close(self): pass
@m.output() @m.output()
def ep_connect(self): def ep_connect(self):
@ -280,7 +283,7 @@ class _ConnectionMachine(object):
def handle_connection(self, ws): def handle_connection(self, ws):
pass pass
@m.output() @m.output()
def start_timer(self): def start_timer(self, f):
pass pass
@m.output() @m.output()
def cancel_timer(self): def cancel_timer(self):
@ -295,22 +298,27 @@ class _ConnectionMachine(object):
initial.upon(start, enter=first_time_connecting, outputs=[ep_connect]) initial.upon(start, enter=first_time_connecting, outputs=[ep_connect])
first_time_connecting.upon(d_callback, enter=negotiating, outputs=[]) first_time_connecting.upon(d_callback, enter=negotiating, outputs=[])
first_time_connecting.upon(d_errback, enter=failed, outputs=[notify_fail]) first_time_connecting.upon(d_errback, enter=failed, outputs=[notify_fail])
first_time_connecting.upon(close, enter=disconnecting2, outputs=[d_cancel]) if ALLOW_CLOSE:
disconnecting2.upon(d_errback, enter=closed, outputs=[]) first_time_connecting.upon(close, enter=disconnecting2, outputs=[d_cancel])
disconnecting2.upon(d_errback, enter=closed, outputs=[])
negotiating.upon(onOpen, enter=open, outputs=[handle_connection]) negotiating.upon(onOpen, enter=open, outputs=[handle_connection])
negotiating.upon(close, enter=disconnecting, outputs=[dropConnection]) if ALLOW_CLOSE:
negotiating.upon(close, enter=disconnecting, outputs=[dropConnection])
negotiating.upon(onClose, enter=failed, outputs=[notify_fail]) negotiating.upon(onClose, enter=failed, outputs=[notify_fail])
open.upon(onClose, enter=waiting, outputs=[start_timer]) open.upon(onClose, enter=waiting, outputs=[start_timer])
open.upon(close, enter=disconnecting, outputs=[dropConnection]) if ALLOW_CLOSE:
open.upon(close, enter=disconnecting, outputs=[dropConnection])
connecting.upon(d_callback, enter=negotiating, outputs=[]) connecting.upon(d_callback, enter=negotiating, outputs=[])
connecting.upon(d_errback, enter=waiting, outputs=[start_timer]) connecting.upon(d_errback, enter=waiting, outputs=[start_timer])
connecting.upon(close, enter=disconnecting2, outputs=[d_cancel]) if ALLOW_CLOSE:
connecting.upon(close, enter=disconnecting2, outputs=[d_cancel])
waiting.upon(expire, enter=connecting, outputs=[ep_connect]) waiting.upon(expire, enter=connecting, outputs=[ep_connect])
waiting.upon(close, enter=closed, outputs=[cancel_timer]) if ALLOW_CLOSE:
disconnecting.upon(onClose, enter=closed, outputs=[]) waiting.upon(close, enter=closed, outputs=[cancel_timer])
disconnecting.upon(onClose, enter=closed, outputs=[])
class _Wormhole: class _Wormhole:
DEBUG = False DEBUG = False