xous-core/tools/app_server.py
Vihaan Jim 2d74a44a89 Add almost working server/client
This creates:
1) An `xtask' command that allows someone to compile individual apps
without compiling the entire image (note that this is required because
xtask generates files that contain gam context names, and if you just
do a regular `cargo build' building the app will fail)
2) A python file app_server.py that acts as a server to give the
app-loader a list of the apps and actually send it the
3) A basic UX that allows app-loader to set the server, get the list
of apps, and load apps.

Currently, this mostly works, except that there is some random
`ShareViolation' when the app-loader loads the app.
2023-08-08 18:08:25 -04:00

64 lines
2.4 KiB
Python

#! /usr/bin/env python3
import argparse
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import unquote
class XousAppServer(BaseHTTPRequestHandler):
def __init__(self, profile, target, context_to_app, *args):
self.profile = profile
self.target = target
self.context_to_app = context_to_app
BaseHTTPRequestHandler.__init__(self, *args)
def do_GET(self):
# an empty path should return a list of apps
path = self.path.split('/')[-1]
if path == '':
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
l = json.dumps(list(self.context_to_app.keys()))
self.wfile.write(l.encode())
else:
name = self.context_to_app[unquote(path)]
with open('target/{}/{}/{}'.format(self.target, self.profile, name), 'rb') as f:
data = f.read()
self.send_response(200)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Length', str(len(data)))
self.end_headers()
self.wfile.write(data)
def main():
parser = argparse.ArgumentParser(description="Xous App Server")
parser.add_argument('apps', metavar='APP', nargs='+',
help='Specifies which apps to serve')
parser.add_argument('-p', '--port',
help='Specifies the port that the server runs on',
required=True, type=int)
parser.add_argument('--profile', default="release",
help="Either debug or release depending on how the apps were compiled. Defaults to release")
parser.add_argument('--target', default="riscv32imac-unknown-xous-elf",
help="The target that the apps were compile to. Defaults to riscv32imac-unknown-xous-elf")
args = parser.parse_args()
# get the GAM names for each of the apps
with open('apps/manifest.json', 'r') as f:
manifest = json.loads(f.read())
context_to_app = {}
for app in args.apps:
context_name = manifest[app]['context_name']
context_to_app[context_name] = app
server = HTTPServer(('0.0.0.0', args.port), lambda *server_args: XousAppServer(args.profile, args.target, context_to_app, *server_args))
server.serve_forever()
if __name__ == "__main__":
main()