Replacement for #220

This commit is contained in:
cclauss 2017-07-23 23:14:01 +02:00 committed by GitHub
parent 54036c231f
commit ed7962f7d8

View File

@ -12,15 +12,24 @@ from wormhole import journal, wormhole
# to use for everything. App should only hold objects that are active
# (Services, subscribers, etc). App must wire up these objects each time.
def parse(args):
raise NotImplementedError
def update_my_state():
raise NotImplementedError
class State(object):
@classmethod
def create_empty(klass):
self = klass()
# to avoid being tripped up by state-mutation side-effect bugs, we
# hold the serialized state in RAM, and re-deserialize it each time
# someone asks for a piece of it.
# someone asks for a piece of it. # iid->invitation_stat
empty = {"version": 1,
"invitations": {}, # iid->invitation_state
"invitations": {},
"contacts": [],
}
self._bytes = json.dumps(empty).encode("utf-8")
@ -55,13 +64,16 @@ class State(object):
def get_all_invitations(self):
return self._as_data()["invitations"]
def add_invitation(self, iid, invitation_state):
with self._mutate() as data:
data["invitations"][iid] = invitation_state
def update_invitation(self, iid, invitation_state):
with self._mutate() as data:
assert iid in data["invitations"]
data["invitations"][iid] = invitation_state
def remove_invitation(self, iid):
with self._mutate() as data:
del data["invitations"][iid]
@ -71,23 +83,26 @@ class State(object):
data["contacts"].append(contact)
class Root(resource.Resource):
pass
class Status(resource.Resource):
def __init__(self, c):
resource.Resource.__init__(self)
self._call = c
def render_GET(self, req):
data = self._call()
req.setHeader(b"content-type", "text/plain")
return data
class Action(resource.Resource):
def __init__(self, c):
resource.Resource.__init__(self)
self._call = c
def render_POST(self, req):
req.setHeader(b"content-type", "text/plain")
try:
@ -98,6 +113,7 @@ class Action(resource.Resource):
data = self._call(args)
return data
class Agent(service.MultiService):
def __init__(self, basedir, reactor):
service.MultiService.__init__(self)
@ -131,7 +147,6 @@ class Agent(service.MultiService):
self._wormholes[iid] = w
w.setServiceParent(self)
def _save_state(self):
self._state.save_to_filename(self._state_fn)
@ -141,7 +156,7 @@ class Agent(service.MultiService):
return b"\n".join(lines) + b"\n"
def _invite(self, args):
print "invite", args
print("invite", args)
petname = args["petname"]
# it'd be better to use a unique object for the event_handler
# correlation, but we can't store them into the state database. I'm
@ -166,7 +181,7 @@ class Agent(service.MultiService):
return b"ok"
def _accept(self, args):
print "accept", args
print("accept", args)
petname = args["petname"]
code = args["code"]
iid = random.randint(1, 1000)
@ -209,6 +224,7 @@ class Agent(service.MultiService):
def wormhole_dispatch_got_verifier(self, verifier, iid):
pass
def wormhole_dispatch_verified(self, _, iid):
pass
@ -227,7 +243,6 @@ class Agent(service.MultiService):
del self._wormholes[iid]
self._state.remove_invitation(iid)
def handle_app_event(self, args, ack_f): # sample function
# Imagine here that the app has received a message (not
# wormhole-related) from some other server, and needs to act on it.
@ -236,26 +251,27 @@ class Agent(service.MultiService):
# client to send a DELETE message. If the process dies before ack_f()
# delivers whatever it needs to deliver, then in the next launch,
# handle_app_event() will be called again.
stuff = parse(args)
stuff = parse(args) # noqa
with self._jm.process():
update_my_state()
self._jm.queue_outbound(ack_f)
def create(reactor, basedir):
os.mkdir(basedir)
s = State.create_empty()
s.save(os.path.join(basedir, "state.json"))
return defer.succeed(None)
def run(reactor, basedir):
a = Agent(basedir, reactor)
a.startService()
print "agent listening on http://localhost:8220/"
print("agent listening on http://localhost:8220/")
d = defer.Deferred()
return d
if __name__ == "__main__":
command = sys.argv[1]
basedir = sys.argv[2]
@ -264,7 +280,5 @@ if __name__ == "__main__":
elif command == "run":
task.react(run, (basedir,))
else:
print "Unrecognized subcommand '%s'" % command
print("Unrecognized subcommand '%s'" % command)
sys.exit(1)