Skip to content

Commit

Permalink
add 'tanco bind [port]' to serve websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tangentstorm committed Jan 26, 2024
1 parent 433c058 commit e2c8686
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ dependencies = [
"jwt ~= 1.3.1",
"requests ~= 2.31.0",
"quart ~= 0.19.4",
"hypercorn ~= 0.16.0" ]
"hypercorn ~= 0.16.0",
"websockets ~= 12.0"]
maintainers = [
{ name = "Michal J Wallace", email = "[email protected]" }]
classifiers = [
Expand Down
48 changes: 48 additions & 0 deletions tanco/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
"""
command-line driver for tanco client.
"""
import asyncio
import cmd as cmdlib
import os
import sqlite3
import subprocess
import sys
import webbrowser
import shlex

import jwt as jwtlib
import websockets as w

from . import database as db
from . import model as m
from . import orgtest, runner
from .client import TancoClient
from .model import Config, TestDescription


class TancoDriver(cmdlib.Cmd):

Check failure on line 24 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (I001)

tanco/driver.py:5:1: I001 Import block is un-sorted or un-formatted
prompt = 'tanco> '
completekey = ''
Expand Down Expand Up @@ -256,6 +260,50 @@ def do_next(self, _arg):

self.do_show()

async def ws_talk(self, ws: w.WebSocketCommonProtocol):
"""communicate with a websocket for 'share' and 'bind' commands"""
tgt = runner.spawn(runner.load_config())
end_cmd = ';'
end_out = ''
async for msg in ws:
if msg.endswith(end_cmd): msg = msg[:-1]
if end_cmd in msg:
await ws.send("WARNING: ';' in message, ignoring")
continue
toks = shlex.split(msg)
if toks[0] == 'send':
cmd = shlex.join(toks[1:])
tgt.stdin.write(cmd + f'\n{end_cmd}\n')
tgt.stdin.flush()
lines = []
for line in tgt.stdout:
line = line.rstrip()
if line == end_out:
break
else:
lines.append(line)
res = '\n'.join(lines)
else:
res = "RECV: " + msg

Check failure on line 287 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:287:23: Q000 Double quotes found but single quotes preferred
await ws.send(res)

def do_share(self, arg):
"""hand control of your working directory over to the tanco server"""
print("TODO: implement `tanco share`")

Check failure on line 292 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:292:15: Q000 Double quotes found but single quotes preferred

def do_bind(self, arg):
"""serve target program on a given port (default 1234)"""
port = int(arg) if arg else 1234

async def serve():
async with w.serve(self.ws_talk, "localhost", port):

Check failure on line 299 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:299:46: Q000 Double quotes found but single quotes preferred
print("serving websocket on port", port)

Check failure on line 300 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:300:23: Q000 Double quotes found but single quotes preferred
print("you can talk to it with:")

Check failure on line 301 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:301:23: Q000 Double quotes found but single quotes preferred
print(f"python -m websockets ws://localhost:{port}/")

Check failure on line 302 in tanco/driver.py

View workflow job for this annotation

GitHub Actions / test

Ruff (Q000)

tanco/driver.py:302:23: Q000 Double quotes found but single quotes preferred
await asyncio.Future()

asyncio.run(serve())

@staticmethod
def do_test(arg):
"""Run the tests."""
Expand Down

0 comments on commit e2c8686

Please sign in to comment.