demo.py: update to match merged file/text commands
This commit is contained in:
parent
2da94d5069
commit
fc641622ba
|
@ -5,11 +5,11 @@ from twisted.internet import reactor
|
||||||
from .transcribe import Wormhole
|
from .transcribe import Wormhole
|
||||||
from .. import public_relay
|
from .. import public_relay
|
||||||
|
|
||||||
APPID = "lothar.com/wormhole/text-xfer"
|
APPID = b"lothar.com/wormhole/text-or-file-xfer"
|
||||||
|
|
||||||
w = Wormhole(APPID, public_relay.RENDEZVOUS_RELAY)
|
w = Wormhole(APPID, public_relay.RENDEZVOUS_RELAY)
|
||||||
|
|
||||||
if sys.argv[1] == "send-text":
|
if sys.argv[1] == "send":
|
||||||
message = sys.argv[2]
|
message = sys.argv[2]
|
||||||
data = json.dumps({"message": message}).encode("utf-8")
|
data = json.dumps({"message": message}).encode("utf-8")
|
||||||
d = w.get_code()
|
d = w.get_code()
|
||||||
|
@ -22,12 +22,12 @@ if sys.argv[1] == "send-text":
|
||||||
d.addCallback(_sent)
|
d.addCallback(_sent)
|
||||||
def _got_data(them_bytes):
|
def _got_data(them_bytes):
|
||||||
them_d = json.loads(them_bytes.decode("utf-8"))
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||||
if them_d["message"] == "ok":
|
if them_d["message_ack"] == "ok":
|
||||||
print("text sent")
|
print("text sent")
|
||||||
else:
|
else:
|
||||||
print("error sending text: %r" % (them_d,))
|
print("error sending text: %r" % (them_d,))
|
||||||
d.addCallback(_got_data)
|
d.addCallback(_got_data)
|
||||||
elif sys.argv[1] == "receive-text":
|
elif sys.argv[1] == "receive":
|
||||||
code = sys.argv[2]
|
code = sys.argv[2]
|
||||||
w.set_code(code)
|
w.set_code(code)
|
||||||
d = w.get_data()
|
d = w.get_data()
|
||||||
|
@ -35,14 +35,37 @@ elif sys.argv[1] == "receive-text":
|
||||||
them_d = json.loads(them_bytes.decode("utf-8"))
|
them_d = json.loads(them_bytes.decode("utf-8"))
|
||||||
if "error" in them_d:
|
if "error" in them_d:
|
||||||
print("ERROR: " + them_d["error"], file=sys.stderr)
|
print("ERROR: " + them_d["error"], file=sys.stderr)
|
||||||
return 1
|
raise RuntimeError
|
||||||
|
if "file" in them_d:
|
||||||
|
print("they're trying to send us a file, which I don't handle")
|
||||||
|
data = json.dumps({"error": "not capable of receiving files"})
|
||||||
|
d1 = w.send_data(data.encode("utf-8"))
|
||||||
|
d1.addCallback(lambda _: RuntimeError())
|
||||||
|
return d1
|
||||||
|
if not "message" in them_d:
|
||||||
|
print("I don't know what they're offering\n")
|
||||||
|
print(them_d)
|
||||||
|
data = json.dumps({"error": "huh?"})
|
||||||
|
d1 = w.send_data(data.encode("utf-8"))
|
||||||
|
d1.addCallback(lambda _: RuntimeError())
|
||||||
|
return d1
|
||||||
print(them_d["message"])
|
print(them_d["message"])
|
||||||
data = json.dumps({"message": "ok"}).encode("utf-8")
|
data = json.dumps({"message_ack": "ok"})
|
||||||
return w.send_data(data)
|
d1 = w.send_data(data.encode("utf-8"))
|
||||||
|
d1.addCallback(lambda _: 0)
|
||||||
|
return d1
|
||||||
d.addCallback(_got_data)
|
d.addCallback(_got_data)
|
||||||
else:
|
else:
|
||||||
raise ValueError("bad command")
|
raise ValueError("bad command")
|
||||||
d.addCallback(w.close)
|
|
||||||
|
d.addBoth(w.close)
|
||||||
|
rc = []
|
||||||
|
def _success(res):
|
||||||
|
rc.append(res)
|
||||||
|
def _fail(f):
|
||||||
|
log.err(f)
|
||||||
|
rc.append(1)
|
||||||
|
d.addCallbacks(_success, _fail)
|
||||||
d.addCallback(lambda _: reactor.stop())
|
d.addCallback(lambda _: reactor.stop())
|
||||||
d.addErrback(log.err)
|
|
||||||
reactor.run()
|
reactor.run()
|
||||||
|
sys.exit(rc[0])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user