From 0474cc18d5e370d8744bf69247082029018aba46 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sat, 14 Feb 2015 18:45:29 -0800 Subject: [PATCH] add list-channels API to relay, use it in receiver --- bin/receive_text.py | 6 ++++-- src/wormhole/relay.py | 10 ++++++++++ src/wormhole/transcribe.py | 7 +++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/bin/receive_text.py b/bin/receive_text.py index 0e5d9b9..e4497c4 100644 --- a/bin/receive_text.py +++ b/bin/receive_text.py @@ -1,12 +1,14 @@ from __future__ import print_function import time, json -from wormhole.transcribe import Receiver +from wormhole.transcribe import Receiver, list_channels from wormhole.codes import input_code_with_completion APPID = "lothar.com/wormhole/text-xfer" # we're receiving -code = input_code_with_completion("Enter receive-text wormhole code: ", []) +channel_ids = list_channels() +code = input_code_with_completion("Enter receive-text wormhole code: ", + channel_ids) start = time.time() data = json.dumps({"message": "ok"}).encode("utf-8") r = Receiver(APPID, data, code) diff --git a/src/wormhole/relay.py b/src/wormhole/relay.py index 6a5fc6e..9e5c274 100644 --- a/src/wormhole/relay.py +++ b/src/wormhole/relay.py @@ -68,6 +68,13 @@ class Allocated(resource.Resource): request.setHeader("content-type", "application/json; charset=utf-8") return json.dumps({"channel-id": self.channel_id})+"\n" +class ChannelList(resource.Resource): + def __init__(self, channel_ids): + resource.Resource.__init__(self) + self.channel_ids = channel_ids + def render_GET(self, request): + request.setHeader("content-type", "application/json; charset=utf-8") + return json.dumps({"channel-ids": self.channel_ids})+"\n" class Relay(resource.Resource): def __init__(self): @@ -85,6 +92,9 @@ class Relay(resource.Resource): log.msg("allocated %d, now have %d channels" % (channel_id, len(self.channels))) return Allocated(channel_id) + if path == "list": + channel_ids = sorted(self.channels.keys()) + return ChannelList(channel_ids) if not re.search(r'^\d+$', path): return resource.ErrorPage(http.BAD_REQUEST, "invalid channel id", diff --git a/src/wormhole/transcribe.py b/src/wormhole/transcribe.py index 3e68cc1..6967269 100644 --- a/src/wormhole/transcribe.py +++ b/src/wormhole/transcribe.py @@ -115,6 +115,13 @@ class Initiator(Common): self._deallocate() return inbound_data + +def list_channels(relay=RELAY): + r = requests.get(relay + "list") + r.raise_for_status() + channel_ids = r.json()["channel-ids"] + return channel_ids + class Receiver(Common): def __init__(self, appid, data, code, relay=RELAY): self.appid = appid