diff --git a/Server/AsyncServer.py b/Server/AsyncServer.py index 48356b7..8e4235f 100644 --- a/Server/AsyncServer.py +++ b/Server/AsyncServer.py @@ -11,7 +11,7 @@ import _thread class Server: - def __init__(self, host_port_pair, debug=False): + def __init__(self, host_port_pair, debug=False, compression_level=None): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.sock.bind(host_port_pair) @@ -21,13 +21,18 @@ class Server: self.sock.listen(2) self.handlers = [] self.debug = debug + self.compression_level = compression_level def run(self, handler, handler_args={}): self.handler = handler while 1: sock, info = self.sock.accept() - sock = ESock(sock) if not self.debug else ESock(sock, debug=self.debug) + if self.compression_level: + sock = ESock(sock, debug=self.debug, compression_level=self.compression_level) + else: + sock = ESock(sock, debug=self.debug) + handler = self.handler(sock, info, **handler_args) self.handlers.append(handler) _thread.start_new_thread(self.controller, (handler, )) @@ -60,11 +65,11 @@ class Server: handler.finish() except Exception: self.print_trace(sock) - finally: + finally: if handler in self.handlers: del self.handlers[self.handlers.index(handler)] handler.sock.close() - + class Handler: def __init__(self, sock, info, **kwargs): @@ -79,4 +84,4 @@ class Server: pass def finish(self): - pass \ No newline at end of file + pass diff --git a/Server/Server.py b/Server/Server.py index aa1cec8..49ae8f8 100644 --- a/Server/Server.py +++ b/Server/Server.py @@ -23,7 +23,7 @@ class Info(Routing.ServerRoute): def run(self, data, handler): handler.sock.send("Connected ST clients: %d\nConnected Wallabies: %d\nAvaliable routes: %s" % ( len(handler.broadcast.channels[Handler.Channels.SUBLIME]), - len(handler.broadcast.channels[Handler.Channels.WALLABY]), + len(handler.broadcast.channels[Handler.Channels.WALLABY]), ", ".join([route for route in list(handler.routes.keys())])), "info") @@ -45,7 +45,7 @@ class StdStream(Routing.ServerRoute): class WallabyControl(Routing.ServerRoute): def __init__(self): self.actions_with_params = {"run" : self.run_program} - self.actions_without_params = {"disconnect" : self.disconnect, + self.actions_without_params = {"disconnect" : self.disconnect, "reboot" : self.reboot, "shutdown" : self.shutdown, "stop" : self.stop_programs} self.programs = [] @@ -93,7 +93,7 @@ class WallabyControl(Routing.ServerRoute): def run_program(self, program, wallaby_handler, handler): handler.routes["std_stream"].stream_to.update({wallaby_handler : handler}) - wallaby_handler.sock.send({"run" : program}, "wallaby_control") + wallaby_handler.sock.send({"run" : program}, "wallaby_control") def stop_programs(self, wallaby_handler, handler): @@ -158,9 +158,9 @@ class GetInfo(Routing.ServerRoute): handler.broadcast.add(handler, handler.channel) if "name" in data: handler.name = data["name"] - Logging.info("'%s:%d' has identified as a %s client." % (handler.info[0], handler.info[1], - "Sublime Text" if handler.channel == Handler.Channels.SUBLIME else - "Wallaby" if handler.channel == Handler.Channels.WALLABY else + Logging.info("'%s:%d' has identified as a %s client." % (handler.info[0], handler.info[1], + "Sublime Text" if handler.channel == Handler.Channels.SUBLIME else + "Wallaby" if handler.channel == Handler.Channels.WALLABY else "Unknown (will not subscribe to broadcast)")) def start(self, handler): @@ -179,7 +179,7 @@ class Handler(Server.Handler): self.routes = Routing.create_routes(routes, self) self.name = "Unknown" - + def handle(self, data, route): if route in self.routes: self.routes[route].run(data, self) @@ -213,6 +213,7 @@ config.add(Config.Option("server_address", ("127.0.0.1", 3077))) config.add(Config.Option("debug", True, validator=lambda x: True if True or False else False)) config.add(Config.Option("binary_path", "Binaries", validator=folder_validator)) config.add(Config.Option("source_path", "Source", validator=folder_validator)) +config.add(Config.Option("compression_level", 0, validator=lambda x: x >= 0 and x <= 9)) try: config = config.read_from_file(CONFIG_PATH) @@ -220,7 +221,7 @@ except FileNotFoundError: config.write_to_file(CONFIG_PATH) config = config.read_from_file(CONFIG_PATH) -server = Server(config.server_address, debug=config.debug) +server = Server(config.server_address, debug=config.debug, compression_level=config.compression_level) broadcast = Broadcast() # Populating broadcast channels with all channels defined in Handler.Channels @@ -234,9 +235,9 @@ s_sync = SyncServer(config.source_path, Handler.Channels.SUBLIME, debug=config.d try: Logging.header("fl0w server started on '%s:%d'" % (config.server_address[0], config.server_address[1])) - server.run(Handler, + server.run(Handler, {"broadcast" : broadcast, - "routes" : {"info" : Info(), "wallaby_control" : WallabyControl(), + "routes" : {"info" : Info(), "wallaby_control" : WallabyControl(), "get_info" : GetInfo(), "compile" : compile, "std_stream" : StdStream(), WALLABY_SYNC_ROUTE : w_sync, SUBLIME_SYNC_ROUTE : s_sync}}) diff --git a/Sublime/fl0w/fl0w.py b/Sublime/fl0w/fl0w.py index 05a4eb4..7d73563 100644 --- a/Sublime/fl0w/fl0w.py +++ b/Sublime/fl0w/fl0w.py @@ -157,10 +157,13 @@ class Fl0w: def connect(self, connect_details): connect_details_raw = connect_details connect_details = connect_details.split(":") + compression_level = int(sublime.load_settings("fl0w.sublime-settings").get("compression_level")) + if len(connect_details) == 2: try: # Establish connection to the server - self.sock = ESock(socket.create_connection((connect_details[0], int(connect_details[1]))), disconnect_callback=self.invoke_disconnect, debug=False) + self.sock = ESock(socket.create_connection((connect_details[0], int(connect_details[1]))), + disconnect_callback=self.invoke_disconnect, debug=False, compression_level=compression_level) sublime.status_message("Connected to %s:%s." % (connect_details[0], connect_details[1])) # Initialize all routes error_report = Fl0w.ErrorReport() diff --git a/Sublime/fl0w/fl0w.sublime-settings b/Sublime/fl0w/fl0w.sublime-settings index 0d384c2..51a7f3b 100644 --- a/Sublime/fl0w/fl0w.sublime-settings +++ b/Sublime/fl0w/fl0w.sublime-settings @@ -1,3 +1,4 @@ { - "server_address": "" // Last server address -} \ No newline at end of file + "server_address": "", // Last server address + "compression_level": 2 +} diff --git a/Wallaby/Wallaby.py b/Wallaby/Wallaby.py index 6291a1d..0120db8 100644 --- a/Wallaby/Wallaby.py +++ b/Wallaby/Wallaby.py @@ -26,7 +26,7 @@ class WallabyControl(Routing.ClientRoute): def __init__(self, output_unbuffer): self.output_unbuffer = output_unbuffer self.actions_with_params = {"run" : self.run_program} - self.actions_without_params = {"disconnect" : self.disconnect, + self.actions_without_params = {"disconnect" : self.disconnect, "reboot" : self.reboot, "shutdown" : self.shutdown, "stop" : self.stop} self.currently_running_program = None @@ -60,7 +60,7 @@ class WallabyControl(Routing.ClientRoute): self.currently_running_program.kill() else: Logging.info("No program started by fl0w.") - + def reboot(self, handler): self.disconnect(handler) @@ -82,7 +82,7 @@ def get_wallaby_hostname(): class GetInfo(Routing.ClientRoute): def run(self, data, handler): if data == "": - handler.sock.send({"type" : CHANNEL, + handler.sock.send({"type" : CHANNEL, "name" : platform.node() if not IS_WALLABY else get_wallaby_hostname()}, "get_info") elif "name" in data: if IS_WALLABY: @@ -125,6 +125,7 @@ config = Config.Config() config.add(Config.Option("server_address", ("127.0.0.1", 3077))) config.add(Config.Option("debug", True, validator=lambda x: True if True or False else False)) config.add(Config.Option("output_unbuffer", "stdbuf")) +config.add(Config.Option("compression_level", 0, validator=lambda x: x >= 0 and x <= 9)) try: config = config.read_from_file(CONFIG_PATH) @@ -134,7 +135,7 @@ except FileNotFoundError: exit(1) config = config.read_from_file(CONFIG_PATH) -wallaby_client = WallabyClient(config.server_address, +wallaby_client = WallabyClient(config.server_address, {"wallaby_control" : WallabyControl(config.output_unbuffer), "get_info" : GetInfo()}, debug=config.debug) try: