Add configuration entries for setting the compression level used by ESock.
On the ST client and on the server, compression_level defaults to 2, on the Wallaby client to 0 because of limited resources and longer battery-life.
This commit is contained in:
parent
a6de0790dd
commit
306d629d0b
5 changed files with 33 additions and 22 deletions
|
@ -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
|
||||
pass
|
||||
|
|
|
@ -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}})
|
||||
|
|
Reference in a new issue