Skip to content

Commit

Permalink
finish off refactoring: all classes now interact with compression and…
Browse files Browse the repository at this point in the history
… encoding through names and generic methods rather than knowing the list of options in advance

git-svn-id: https://xpra.org/svn/Xpra/trunk@6982 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jul 28, 2014
1 parent e2ba5b1 commit 8752294
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 179 deletions.
33 changes: 11 additions & 22 deletions src/xpra/client/ui_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,14 +919,10 @@ def make_hello(self):
"encodings.core" : self.get_core_encodings(),
})
control_commands = ["show_session_info", "enable_bencode", "enable_zlib"]
for k,b in {"lz4" : compression.use_lz4,
"bz2" : compression.use_bz2,
"zlib" : compression.use_zlib,
"bencode" : packet_encoding.use_bencode,
"rencode" : packet_encoding.use_rencode,
"yaml" : packet_encoding.use_yaml}.items():
if b:
control_commands.append("enable_"+k)
for x in compression.get_enabled_compressors():
control_commands.append("enable_"+x)
for x in packet_encoding.get_enabled_encoders():
control_commands.append("enable_"+x)
capabilities["control_commands"] = control_commands
for k,v in codec_versions.items():
capabilities["encoding.%s.version" % k] = v
Expand Down Expand Up @@ -1357,21 +1353,14 @@ def _process_control(self, packet):
args = packet[2:]
log("calling show_session_info%s on server request", args)
self.show_session_info(*args)
elif command=="enable_zlib":
log.info("switching to zlib on server request")
self._protocol.enable_zlib()
elif command=="enable_lz4":
log.info("switching to lz4 on server request")
self._protocol.enable_lz4()
elif command=="enable_bencode":
log.info("switching to bencode on server request")
elif command in ("enable_%x" for x in compression.get_enabled_compressors()):
compressor = command.split("_")[1]
log.info("switching to %s on server request", compressor)
self._protocol.enable_compressor(compressor)
elif command in ("enable_%x" for x in packet_encoding.get_enabled_encoders()):
packet_encoding = command.split("_")[1]
log.info("switching to %s on server request", packet_encoding)
self._protocol.enable_bencode()
elif command=="enable_rencode":
log.info("switching to rencode on server request")
self._protocol.enable_rencode()
elif command=="enable_yaml":
log.info("switching to yaml on server request")
self._protocol.enable_yaml()
elif command=="name":
assert len(args)>=3
self.session_name = args[2]
Expand Down
29 changes: 29 additions & 0 deletions src/xpra/net/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def nocompress(packet, level):
use_bz2 = True
use_lz4 = has_lz4

#all the compressors we know about, in their default preference order:
ALL_COMPRESSORS = ["zlib", "lz4", "bz2"]

_COMPRESSORS = {
"zlib" : zcompress,
"lz4" : lz4_compress,
"bz2" : bzcompress,
"none" : nocompress,
}

def get_compression_caps():
return {
Expand All @@ -67,6 +76,26 @@ def get_compression_caps():
"zlib.version" : zlib.__version__,
}

def get_enabled_compressors():
enabled = [x for x,b in {
"lz4" : use_lz4,
"bz2" : use_bz2,
"zlib" : use_zlib,
}.items() if b]
#order them:
return [x for x in ALL_COMPRESSORS if x in enabled]

def get_compressor(c):
assert c=="none" or c in ALL_COMPRESSORS
return _COMPRESSORS[c]

def get_compressor_name(c):
assert c in _COMPRESSORS.values(), "invalid compressor: %s" % c
for k,v in _COMPRESSORS.items():
if v==c:
return k
raise Exception("impossible bug!")


class Compressed(object):
def __init__(self, datatype, data):
Expand Down
43 changes: 43 additions & 0 deletions src/xpra/net/packet_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
log("packet encoding: has_yaml=%s, use_yaml=%s, version=%s", has_yaml, use_yaml, yaml_version)


def do_bencode(data):
return bencode(data), 0

def do_rencode(data):
return rencode_dumps(data), FLAGS_RENCODE

def do_yaml(data):
return yaml_encode(data), FLAGS_YAML


def get_packet_encoding_caps():
caps = {
"rencode" : use_rencode,
Expand All @@ -73,6 +83,39 @@ def get_packet_encoding_caps():
caps["yaml.version"] = yaml_version
return caps


#all the encoders we know about, in their default preference order:
ALL_ENCODERS = ["bencode", "rencode", "yaml"]

_ENCODERS = {
"rencode" : do_rencode,
"bencode" : do_bencode,
"yaml" : do_yaml,
}

def get_enabled_encoders():
enabled = [x for x,b in {
"rencode" : use_rencode,
"bencode" : use_bencode,
"yaml" : use_yaml,
}.items() if b]
#order them:
return [x for x in ALL_ENCODERS if x in enabled]


def get_encoder(e):
assert e in ALL_ENCODERS, "invalid encoder name: %s" % e
assert e in get_enabled_encoders(), "%s is not available" % e
return _ENCODERS[e]

def get_encoder_name(e):
assert e in _ENCODERS.values(), "invalid encoder: %s" % e
for k,v in _ENCODERS.items():
if v==e:
return k
raise Exception("impossible bug!")


def get_packet_encoding_type(protocol_flags):
if protocol_flags & FLAGS_RENCODE:
return "rencode"
Expand Down
Loading

0 comments on commit 8752294

Please sign in to comment.