clean up
This commit is contained in:
		
							parent
							
								
									2a649a749c
								
							
						
					
					
						commit
						59a26e071c
					
				| 
						 | 
				
			
			@ -25,8 +25,7 @@ import prompt_toolkit
 | 
			
		|||
import typing
 | 
			
		||||
from prompt_toolkit.patch_stdout import patch_stdout
 | 
			
		||||
from NetUtils import Endpoint
 | 
			
		||||
import WebUiServer
 | 
			
		||||
import WebUiClient
 | 
			
		||||
import WebUI
 | 
			
		||||
 | 
			
		||||
import Regions
 | 
			
		||||
import Utils
 | 
			
		||||
| 
						 | 
				
			
			@ -44,7 +43,7 @@ class Context():
 | 
			
		|||
        self.snes_address = snes_address
 | 
			
		||||
        self.server_address = server_address
 | 
			
		||||
 | 
			
		||||
        self.ui_node = WebUiClient.WebUiClient()
 | 
			
		||||
        self.ui_node = WebUI.WebUiClient()
 | 
			
		||||
        self.custom_address = None
 | 
			
		||||
        self.webui_socket_port = port
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -687,7 +686,7 @@ async def server_loop(ctx: Context, address=None):
 | 
			
		|||
                cmd, args = (msg[0], msg[1]) if len(msg) > 1 else (msg, None)
 | 
			
		||||
                await process_server_cmd(ctx, cmd, args)
 | 
			
		||||
        ctx.ui_node.log_warning('Disconnected from multiworld server, type /connect to reconnect')
 | 
			
		||||
    except WebUiClient.WaitingForUiException:
 | 
			
		||||
    except WebUI.WaitingForUiException:
 | 
			
		||||
        pass
 | 
			
		||||
    except ConnectionRefusedError:
 | 
			
		||||
        if cached_address:
 | 
			
		||||
| 
						 | 
				
			
			@ -1257,7 +1256,7 @@ async def main():
 | 
			
		|||
                if not sock.connect_ex(('localhost', port)) == 0:
 | 
			
		||||
                    break
 | 
			
		||||
        import threading
 | 
			
		||||
        WebUiServer.start_server(
 | 
			
		||||
        WebUI.start_server(
 | 
			
		||||
            port, on_start=threading.Timer(1, webbrowser.open, (f'http://localhost:5050?port={port}',)).start)
 | 
			
		||||
 | 
			
		||||
    if args.diff_file:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -40,7 +40,7 @@ def main():
 | 
			
		|||
    parser.add_argument('--meta', default=None)
 | 
			
		||||
    parser.add_argument('--log_output_path', help='Path to store output log')
 | 
			
		||||
    parser.add_argument('--loglevel', default='info', help='Sets log level')
 | 
			
		||||
    parser.add_argument('--yaml_output', default=256, type=lambda value: min(max(int(value), 0), 255),
 | 
			
		||||
    parser.add_argument('--yaml_output', default=0, type=lambda value: min(max(int(value), 0), 255),
 | 
			
		||||
                        help='Output rolled mystery results to yaml up to specified number (made for async multiworld)')
 | 
			
		||||
 | 
			
		||||
    for player in range(1, multiargs.multi + 1):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,11 @@
 | 
			
		|||
import http.server
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
import socket
 | 
			
		||||
import socketserver
 | 
			
		||||
import threading
 | 
			
		||||
import webbrowser
 | 
			
		||||
from functools import partial
 | 
			
		||||
 | 
			
		||||
from NetUtils import Node
 | 
			
		||||
from MultiClient import Context
 | 
			
		||||
| 
						 | 
				
			
			@ -6,6 +13,7 @@ import Utils
 | 
			
		|||
 | 
			
		||||
logger = logging.getLogger("WebUIRelay")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class WebUiClient(Node):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
| 
						 | 
				
			
			@ -104,3 +112,33 @@ class WebUiClient(Node):
 | 
			
		|||
 | 
			
		||||
class WaitingForUiException(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
webthread = None
 | 
			
		||||
PORT = 5050
 | 
			
		||||
Handler = partial(http.server.SimpleHTTPRequestHandler,
 | 
			
		||||
                  directory=Utils.local_path(os.path.join("data", "web", "public")))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start_server(socket_port: int, on_start=lambda: None):
 | 
			
		||||
    global webthread
 | 
			
		||||
    try:
 | 
			
		||||
        server = socketserver.TCPServer(("", PORT), Handler)
 | 
			
		||||
    except OSError:
 | 
			
		||||
        # In most cases "Only one usage of each socket address (protocol/network address/port) is normally permitted"
 | 
			
		||||
        import logging
 | 
			
		||||
 | 
			
		||||
        # If the exception is caused by our desired port being unavailable, assume the web server is already running
 | 
			
		||||
        # from another client instance
 | 
			
		||||
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
 | 
			
		||||
            if sock.connect_ex(('localhost', PORT)) == 0:
 | 
			
		||||
                logging.info("Web server is already running in another client window.")
 | 
			
		||||
                webbrowser.open(f'http://localhost:{PORT}?port={socket_port}')
 | 
			
		||||
                return
 | 
			
		||||
 | 
			
		||||
        # If the exception is caused by something else, report on it
 | 
			
		||||
        logging.exception("Unable to bind port for local web server. The CLI client should work in all cases.")
 | 
			
		||||
    else:
 | 
			
		||||
        print("serving at port", PORT)
 | 
			
		||||
        on_start()
 | 
			
		||||
        webthread = threading.Thread(target=server.serve_forever).start()
 | 
			
		||||
| 
						 | 
				
			
			@ -1,43 +0,0 @@
 | 
			
		|||
import http.server
 | 
			
		||||
import socketserver
 | 
			
		||||
import os
 | 
			
		||||
import socket
 | 
			
		||||
import threading
 | 
			
		||||
from functools import partial
 | 
			
		||||
import webbrowser
 | 
			
		||||
 | 
			
		||||
import Utils
 | 
			
		||||
 | 
			
		||||
webthread = None
 | 
			
		||||
 | 
			
		||||
PORT = 5050
 | 
			
		||||
 | 
			
		||||
Handler = partial(http.server.SimpleHTTPRequestHandler, directory=Utils.local_path(os.path.join("data", "web", "public")))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start_server(socket_port: int, on_start=lambda: None):
 | 
			
		||||
    global webthread
 | 
			
		||||
    try:
 | 
			
		||||
        server = socketserver.TCPServer(("", PORT), Handler)
 | 
			
		||||
    except OSError:
 | 
			
		||||
        # In most cases "Only one usage of each socket address (protocol/network address/port) is normally permitted"
 | 
			
		||||
        import logging
 | 
			
		||||
 | 
			
		||||
        # If the exception is caused by our desired port being unavailable, assume the web server is already running
 | 
			
		||||
        # from another client instance
 | 
			
		||||
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
 | 
			
		||||
            if sock.connect_ex(('localhost', PORT)) == 0:
 | 
			
		||||
                logging.info("Web server is already running in another client window.")
 | 
			
		||||
                webbrowser.open(f'http://localhost:{PORT}?port={socket_port}')
 | 
			
		||||
                return
 | 
			
		||||
 | 
			
		||||
        # If the exception is caused by something else, report on it
 | 
			
		||||
        logging.exception("Unable to bind port for local web server. The CLI client should work in all cases.")
 | 
			
		||||
    else:
 | 
			
		||||
        print("serving at port", PORT)
 | 
			
		||||
        on_start()
 | 
			
		||||
        webthread = threading.Thread(target=server.serve_forever).start()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    start_server(5090)
 | 
			
		||||
		Loading…
	
		Reference in New Issue