From abe63b54e51162647770690c1410a412896f4c30 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Mon, 21 Sep 2020 21:08:28 +0900 Subject: [PATCH] bluepy_scratch_link: Add __main__.py Add __main__.py to the bluepy-scratch-link package to invoke its feature using the package name. Signed-off-by: Shin'ichiro Kawasaki --- bluepy_scratch_link/__main__.py | 6 ++++ bluepy_scratch_link/scratch_link.py | 49 +++++++++++++++-------------- 2 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 bluepy_scratch_link/__main__.py diff --git a/bluepy_scratch_link/__main__.py b/bluepy_scratch_link/__main__.py new file mode 100644 index 0000000..1b2f731 --- /dev/null +++ b/bluepy_scratch_link/__main__.py @@ -0,0 +1,6 @@ +#!/usr/bin/python + +from scratch_link import main as _main + +if __name__ == '__main__': + _main() diff --git a/bluepy_scratch_link/scratch_link.py b/bluepy_scratch_link/scratch_link.py index 9d9945a..c927f5d 100755 --- a/bluepy_scratch_link/scratch_link.py +++ b/bluepy_scratch_link/scratch_link.py @@ -651,16 +651,6 @@ class BLESession(Session): self.delegate.restart_notification_event.set() return self.status == self.DONE -# Prepare certificate of the WSS server -gencert.prep_cert() - -# kick start WSS server -ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) -localhost_cer = gencert.cert_file_path -localhost_key = gencert.key_file_path -ssl_context.load_cert_chain(localhost_cer, localhost_key) -sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession } - async def ws_handler(websocket, path): try: logger.info(f"Start session for web socket path: {path}") @@ -671,10 +661,6 @@ async def ws_handler(websocket, path): logger.error(f"Failure in session for web socket path: {path}") logger.error(e) -start_server = websockets.serve( - ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context -) - def stack_trace(): print("in stack_trace") code = [] @@ -689,14 +675,29 @@ def stack_trace(): for line in code: print(line) -while True: - try: - asyncio.get_event_loop().run_until_complete(start_server) - logger.info("Started scratch-link") - asyncio.get_event_loop().run_forever() - except KeyboardInterrupt as e: - stack_trace() - break - except Exception as e: - logger.info("Restarting scratch-link...") +def main(): + # Prepare certificate of the WSS server + gencert.prep_cert() + + # kick start WSS server + ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + localhost_cer = gencert.cert_file_path + localhost_key = gencert.key_file_path + ssl_context.load_cert_chain(localhost_cer, localhost_key) + sessionTypes = { '/scratch/ble': BLESession, '/scratch/bt': BTSession } + + start_server = websockets.serve( + ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context + ) + + while True: + try: + asyncio.get_event_loop().run_until_complete(start_server) + logger.info("Started scratch-link") + asyncio.get_event_loop().run_forever() + except KeyboardInterrupt as e: + stack_trace() + break + except Exception as e: + logger.info("Restarting scratch-link...")